Test Corrections
2014 MCQ Practice
Question 1
public static int myster(int[] arr) {
int x = 0;
for (int k=0; k<arr.length; k=k+2) {
x = x + arr[k];
}
return x;
}
Assume that the array nums has been declared and initialized as follows.
int [ ] nums = { 3, 6, 1, 0, 1, 4, 2};
What value will be returned as a result of the call mystery(nums) ?
My Answer
B) 6
Actual Answer
C) 7
The for loop initializes k
at 0. It will increment by 2 until it is greater than or equal to arr.length
. k will take on the values of 0, 2, 3, and 6 until the loop stops. The loops will continue iterating until k is 6, at which x+arr[k]
is 7.
Question 10
private int[] data;
public int seqSearchRec(int target) {
return seqSearchRecHelper(target, data.length -1);
}
private int seqSearchRecHelper (int target, int last) {
// Line 1
if (data[last] == target) {
return last;
} else {
return seqSearchRecHelper(target, last-1)l
}
}
Consider the following instance variable and methods. You may assume that data has been initialized with length > 0. The methods are intended to return the index of an array element equal to target, or -1 if no such element exists.
For which of the following test cases will the call seqSearchRec(5) always result in an error?
I. data contains only one element.
II. data does not contain the value 5.
III. data contains the value 5 multiple times.
My Answer
C) III Only
Actual Answer
B) II Only
If the data does not contain the value of 5, an ArrayIndexOutOfBoundsException
will be thrown. Since there is no 5, the loop will eventually come to data[-1]
.
Question 12
public String mystery(String input) {
String output = "";
for(int k=1; k < input.length(); k = k+2) {
output += input.substring(k, k+1);
}
return output;
}
What is returned as a result of he call mystery("computer")?
My Answer
E) Nothing is returned because IndexOutOfBoundsException is thrown
Actual Answer
C) "optr"
The loops starts at k=1, and k is incremented by 2. This means that every other letter is inputted: "o" "p" "t"
Question 15
Consider the following method, isSorted, which is inteded to return true if an array of integers is sorted in nondecreasing order and to return false otherwise.
public static boolean isSorted(int[] data) {
/ * missing code * /
}
Which of the following can be used to replace /missing code/ so that isSorted will work as inteded?
My Answer
D) I and II only
Actual Answer
A) I only
I is the only correct answer. It starts at the second value of the array, then checks if the previous values are greater. If not, the array is not increasing, and false should be returned.
Question 16
Consider the following incomplete method that is intended to return an array that contains the contents of its first array parameter followed by the contents of its second array parameter.
public static int[] append(int[] a1, int[] a2) {
int[] result = new int[a1.length + a2.length];
for(int j = 0; j < a1.length; j++) {
result[j] = a1[j];
}
for(int k = 0; k < a2.length; k++) {
result[/ * index * /] = a2[k];
}
return result;
}
Which of the following expressions can be used to replace / index / so that append will work as intended?
My Answer
C) k + a1.length - 1
Actual Answer
D) k + a1.length
Since the loop already starts at k=0, the second array will start at k + a1.length
Question 21
Consider the following method, which is intended to return the element of a 2-dimensional array that is closest in value to a specified number, val.
public double findCloset(double[][] mat, double val) {
double answer = mat[0][0];
double minDiff = Math.abs(answer - val);
for(double[] row : mat) {
for(double num : row) {
if(/ * missing code */) {
answer = num;
minDiff = Math.abs(num - val);
}
}
}
return answer;
}
Which of the following could be used to replace / missing code / so that findClosest will work as intended?
My Answer
B) Math.abs(num-minDiff) < minDiff
Actual Answer
D) Math.abs(num-val) < minDiff
Math.abs() is used to find the distance. The smallest difference between the number and the given value should be set to minDiff.