CS305j Fall 2008
Midterm 2 Key, Suggested Solutions, and Grading Criteria
Abbreviations:
NAP - no answer provided
ECF - error carried forward
OBOE - off by one error
BOD - benefit of the doubt
GCE - misunderstood question. Answer is way off base.
NN - Not Necessary. The code was not required. Usually no points off for this.
AIOBE - Array Index out of Bounds Exception may occur
GACK - Inelegant code or code very hard to understand even though it works.
(Generally no points off for this.)
Question 1: Array Trace
0
6
4
The code in question added up the last three elements in the array and printed
out their sum. Some ECF if it was very clear what the student was trying to do.
----------------------------------------------------------------------------
Question 2: Programming with boolean logic
Suggested Solution:
public static int points(int a, int b, int c){
int total = a + b + c;
if(a == b && b == c)
total = 20;
else if( a == b || b == c || a == c )
total = 10;
return total;
}
Grading criteria
handle case when all 3 ints equal: attempt 1 point, correct 4 points
handle case when all 2 of 3 ints equal: attempt 1 point, correct 4 points
handle case when all different: attempt 1 point, correct 3 points
return: 1 point
Common problems:
- when all three ints are different setting result to 3 instead of sum of 3 ints
- not using else's. This can result in a logic error when all three are equal.
----------------------------------------------------------------------------
Question 3: Array programming 1
Suggested Solution:
public static void mystery(int[] data){
int sum = 0;
int limit = data.length - 3;
for(int i = data.length - 1; i >= limit; i--){
sum += data[i];
}
System.out.println( sum );
}
Grading criteria:
count variable initialized to 0: 2 points
loop through array: attempt 2 points, correct 6 points
test if given element greater than or equal to target:
attempt 2 points, correct 4 points
increment count variable correctly: 2 points
return count: 2 points
No major issues. Most students did very well. Biggest problem was various off by
one errors.
----------------------------------------------------------------------------
Question 4: Array programming 2
Suggested Solution:
public static boolean isSorted(double[] nums){
boolean sorted = true;
int index = 0;
while( sorted && index < nums.length - 1){
sorted = nums[index] <= nums[index + 1];
index++;
}
return sorted;
}
Alternate solution using for loop:
public static boolean isSorted(double[] nums){
boolean sorted = true;
for(int i = 0; i < nums.length - 1; i++){
if( nums[i] > nums[i+1] )
sorted = false;
}
return sorted;
}
Grading criteria:
loop through correct elements of array: attempt 1 point, correct 4 points
test adjacent elements to ensure element to the right is greater than or equal
to the adjacent element to the left. (Lots of correct variations on this.):
attempt 1 point, correct 4 points
correctly determine if values in array are sorted: 4 points
return boolean: 1 point
Common problems:
- off by one errors.
- returning based on the result of the very first check. (early return)
- logic error where result is determined by the last pair that is checked. (Due
to toggling boolean flag back to true when it should stay false.)
----------------------------------------------------------------------------
Question 5: Programming
Suggested solution:
// start >= end
public static void fizzBang(int start, int end){
for(int i = start; i <= end; i++){
if( i % 5 == 0 && i % 3 == 0)
System.out.print("FizzBang ");
else if( i % 3 == 0)
System.out.print("Fizz ");
else if( i % 5 == 0)
System.out.print("Bang ");
else
System.out.print(i + " ");
}
}
loop through numbers: attempt 1 point, correct 3 points
handle fizz case (multiples of 3): attempt 1 point, correct 3 points
handle bang case (multiples of 5): attempt 1 point, correct 3 points
handle fizzbang case (multiples of 3 and 5): attempt 1 point, correct 3 points
handle other numbers: attempt 1 point, correct 3 points
Again, most students did well. Some students forgot about the modulus operator.
A clever alternative is this test
if( i / 3 * 3 == i ) // for fizz
The modulus operator is preferred because it is easier to deduce what is being
tested.
A few problems in printing out fizz or bang multiple times for 1 value.
----------------------------------------------------------------------------
Question 6: Program logic
Suggested solution:
num != 0 count > 0 temp == target
A A N S
B A S S
C A A A
D S S S
E N S S
F S S S
I really liked this question. Expect to see one like it on the final.
The most missed answers were num != 0 at point E and count > 0 at point B.
At point E we are just after the while loop whose condition is num != 0.
If num 1= 0 were true the program would not have skipped the loop so num != 0
must be false at that point. So it is never true.
At point B we are just inside the while loop, but remember it is a loop. We may
hit point B multiple times. So it is possible count got incremented on a
previous iteration of the loop and is now > 0.