| CIT 590 Final Exam |
Name ______________________________ |
Spring 2009
I. Writing code
Write only the code requested. Where I ask for a statement,
give me a statement, not a complete method. Where I ask
for a method, give me a method, not a class. Don't print results
unless I ask you to. And so forth.
- (5 points) Write a statement or statements to set
sum to be the sum of the numbers in the array
int[ ] scores.
int sum = 0;
for (int i = 0; i < scores.length; i++) {
sum += scores[i];
}
- (5 points) A program contains the statement
Person p = new Person("John Smith");
Write the constructor you would put in the Person
class to handle this statement.
Person(String name) {
this.name = name;
}
- (5 points) Declare and define a variable
names to be an
ArrayList of Strings.
ArrayList<String> names = new ArrayList<String>();
- (5 points) The statement
FileReader reader = new FileReader("foo.txt");
may throw a FileNotFoundException. Put this statement in a
try statement, and print a stack trace if the exception occurs.
try {
FileReader reader = new FileReader("foo.txt");
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
- (5 points) Write a method
countParameters that takes any number of
int parameters, and returns how many parameters
it was called with.
int countParameters(int...parameters) {
return parameters.length;
}
- (5 points) Suppose
quitButton is a JButton.
Use an anonymous inner class to attach a listener to quitButton
that will end the program when the button is clicked.
quitButton.addActionListener(new ActionListener( )
{
@Override // this line can be omitted
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
- (5 points) Suppose
Season is defined as
enum Season { WINTER, SPRING, SUMMER, FALL }
and season is a variable of type Season.
Write the shortest possible statement to print out the value
of season.
System.out.print(season); //
println is also acceptable
- (5 points) Assume that the
Sprite class extends
Thread.
- Declare and define a variable
frog of type
Sprite.
Sprite frog = new Sprite();
- Write a statement that will cause the
frog Thread
to begin execution.
frog.start();
- Write the header (first line) of the method that
will execute when the
frog Thread is started.
public void run() //
also okay if marked with @Override
- (5 points) The method
public static average(double d1, double d2)
defined in class Arithmetic, is supposed to return the
average of its two parameters. Write a complete JUnit 4 test method to make sure that it returns the correct average of 2.3 and 2.7.
@Test
public void testAverage() {
assertEquals(2.5, Arithmetic.average(2.3, 2.7), 0.00001);
}
II. Short answer
Keep all answers brief and to the point. Do not include information
that was not asked for--it may cost you points. You may be penalized for
excesssively verbose answers.
- (10 points) Tell the result of each of the following expressions:
100 << 2
400
100 >> 2
25
0xFF & 0x2A
0x2A
0xFF | 0x2A
0xFF
10 % 3 == 2 ? 'a' : 17 //
yes, this is legal!
17
- (2 points) Style: Which binary operator should not be surrounded
by spaces?
The dot operator
- (2 points) Style: An open parenthesis,
(, should normally
be preceded by a space. State one exception to this rule.
When it is a method or constructor call or definition, or when
preceded by a unary operator.
- (6 points) Mark each statement as either true (
T) or false (F).
- __F__ In an abstract class, all methods are abstract.
- __T__ The
Serializable interface doesn't declare
any methods.
- __F__ The
@author tag is used for classes, interfaces,
and methods.
- __T__ Style: Methods should not contain both computation and
printing.
- __T__ Style: JUnit tests should not print anything.
- __T__ Objects of an inner class
type can be used in unrelated classes (other than the enclosing
class).
- (5 points) What do each of the following acronyms stand for?.
- DRY
Don't Repeat Yourself
- TDD
Test-Driven Design
- API
Application Programmer's Interface
- JRE
Java Runtime Environment
- XP (not the Microsoft OS)
eXtreme Programming
III. Object-oriented concepts
- (5 points) When a constructor is called, what is the first thing that
it does (whether you tell it to or not)?
Calls a constructor for its superclass.
- (6 points) Some methods inherited from
Object by your own classes
should normally be overridden. Give the complete signature of two
such methods, and for each, tell why overriding it is a good idea.
- Signature of one method:
public String toString()
- Benefit of overriding it:
Simplifies printing, simplifies debugging
- Signature of another method:
public boolean equals(Object o)
- Benefit of overriding it:
Lets you test whether two distinct objects are equal
Makes it possible to find an object in an array or collection
- (4 points) Give two reasons why writing tests first tends to improve the quality of code.
Encourages small, single-purpose methods
Discourages mixing computation and I/O
Helps clarify what the method should do
Ensures code is actually testable
Helps keep methods relatively independent of one another, and
of context
- (5 points) In general, instance variables should be
private. Why?
So that each object is responsible for maintaining its own validity.
Helps localize debugging.
- (5 points) What should the fields (instance variables) of an object describe,
and what should they not be used for?
They should describe the state of the object. They should not
be used for temporary values used in computation, or for passing
information between methods within the class.
- (5 points) Tell, for each of the following kinds of comments, who your intended
audience should be, and what kind of information you should convey.
- Javadoc comments
Audience: Other programmers/classes that may want to use your
methods
Information: Everything needed to use the methods correctly
- Comments inside a method
Audience: Other programmers who may need to update, modify, debug,
or maintain the method
Information: Explanations of any code whose working isn't obvious
to an average programmer