| CIT 590 Midterm Exam |
Name ______________________________ |
Spring 2009
I. Writing code
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. And so forth.
- (5 points) Write a declaration of variable
foo to be of
type double with an initial value of 17.76.
double foo = 17.76;
- (5 points) Declare a variable
students to be an array of
Student objects, but don't give it an initial value.
Student[] students;
- (5 points) Declare and define a variable
books to
be an ArrayList of Book objects. Use the correct
syntax for generics (that is, make sure your ArrayList will hold
Books and nothing else).
ArrayList<Book> books = new ArrayList<Book>();
- (5 points) Write a public "stub" method (one that does nothing) named
fiddle that takes no arguments and returns no value.
public void fiddle() {
}
- (5 points) Write a public "stub" method named
faddle that takes two
integer arguments and returns a String. Although this method does
nothing, it should be complete enough to compile without error.
public String faddle(int x, int y) {
return null;
}
-3 points if your method has compile errors
- (5 points) Write the shortest possible statement to add one to the
variable
count.
count++; OR, ++count;
Graded as all or nothing
- (5 points) Write the shortest possible statement to add five to the
variable
count.
count += 5;
Graded as all or nothing
- (5 points) Write the shortest possible statement to return
true
from a method if x is greater than 5, and false otherwise.
return x > 5;
Graded as all or nothing
- (5 points) Write the shortest possible statement that adds 1 to
index, but resets index to zero if adding 1 would
make it equal to length.
index = (++index) % length;
Also marked as okay: index = (index + 1) % length;
- (5 points) Write a loop, and any additional necessary statements,
to set the integer variable
sum to the sum of all the values
in the one-dimensional array scores of integers.
sum = 0; // OR, int
sum = 0;
for (int i = 0; i < scores.length; i++) {
sum += scores[i]; // OR, sum
= sum + scores[i];
}
If you said: int[] scores = new int[10]; your sum will be zero.
- (5 points) Write a
switch statement to set the String variable
word to either "one", "two", or
"many", depending on whether the integer variable
count is 1, 2, or some other value,
respectively.
switch (count) {
case 1:
word = "one";
break;
case 2:
word = "two";
break;
default:
word = "many";
break; //
this break is optional
}
- (5 points) Write a single statement to create and throw an
RuntimeException if the variable k is negative.
if (k < 0) throw new RuntimeException();
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.
- (5 points) Instance variables should usually be
private. Why?
To help ensure that, if these variables get illegal values, the error
is within this class.
OR
To help maintain the modifiability of this class.
2 points if you just
defined "private"
3 points if you said "to prevent changes
from outside"
4 points if you said "to make them safe"
5 points if you said the class is responsible
for its variables
- (10 points) Suppose you have a method which only works correctly if a
certain
double parameter is between 0.1 and 1.0. As the
first step of your method, you want to ensure that this parameter is
in the legal range. There are two ways to do this:
- If the method is
public, instead of using an
assert statement, you should throw some kind
of Exception for illegal values of the parameter. Why?
Being public, the method can be called from outside the class,
so you have no guarantee that the parameter is in the correct
range. If it is not, it is the caller's responsibility to do something
about it.
- If the method is
private, instead of throwing an
Exception, you probably should assert
that the value of the parameter is in the legal range. Why?
Being private, the method can only be called
from within this class, so any bad calls represent an error in
this class, which you should find and fix.
- (5 points) Point out the syntax error in the following code, and tell
how to correct it.
Random rand = new Random();
do {
int row = rand.nextInt(10);
int col = rand.nextInt(10);
} while (row == col);
The variables row and col are declared (and only
available) within the do block; they are not available
in the while condition. The declarations have to be moved
above the do loop.
- (5 points) The keyword
this cannot be used in a static method.
Why not?
The keyword this refers to the object that is executing the method.
Static methods are executed by the class itself, not by any particular
object.
- (5 points) When a constructor is called, what is the first thing that
it does (whether you tell it to or not)?
Calls the constructor for its superclass.
- (5 points) If a class
implements an interface, what is
that class "promising" to do?
Implement (provide bodies for) all the methods declared in the
interface.
- Consider the following method:
public String toString() {
top = (top & 0x3FF) % 900 + 100;
left = (left & 0x3FF) % 900 + 100;
right = (right & 0x3FF) % 900 + 100;
bottom = (bottom & 0x3FF) % 900 + 100;
System.out.println("[" + top + " " + left + " " + bottom + " " + right + "]");
}
- (5 points) What style rule or rules does this method violate? How could
we improve the style?
The DRY (Don't Repeat Yourself) principle.
The same complex formula occurs four times. It should be moved
into a method, and the method called four times, once with each
of the four variables (top, left, right, and bottom).
- (5 points) Besides poor style, this method is committing a serious
logic error. What is it, and how could we fix it?
While the apparent purpose of the method is
to return a printable string, it also changes the values of the
four variables being printed. We could solve this by copying those
into temporary variables, or (better) we could put the formula
into a separate method (as above), which automatically copies its
parameters into local variables.
I made a syntax error in the above question.
You got 4 points for mentioning this error, even though I asked
for the logic error.