Define recursion.
a method that calls itself.
Which is not a valid way to initialise a 1D array?
(A) int[] list = new list[];
(B) int list = new list[];
(C) int[] list = new list[5];
(D) int[] list = {1, 2, 3, 4, 5};
(B) int list = new list[];
What characteristics do both String and int[] share?
(A) they share the same methods
(B) they are primitive
(C) they are immutable
(D) nothing
(C) they are immutable
What is required for a Binary Search?
(A) a sorted list
(B) a long list
(C) a short list
(D) an unsorted list
(A) a sorted list
System.out.println(5/0);
(A) DivideByZeroException
(B) ZeroException
(C) OutOfBoundsException
(D) ArithmeticException
(A) DivideByZeroException
Identify the base case and the base case's result of the following recursive method.
public int mystery(int n) {
if (n == 1) {
return 1;
} else {
return n * (mystery(n)) + 3;
}
}
(A) 1, 4
(B) n == 1, 1
(C) n == 0, 4
(D) n, 1
(B) n == 1, 1
Replace the following line to complete a Column-Major order traversal.
int[][] list = /* correctly initialised 2D array */
// missing code
for (int r = 0; r < list.length; r++) {
System.out.println(list[r][c]);
}
}
(A) for (int r = 0; r< list.length-1; r++) {
(B) for (int c = 0; c < list.length; c++) {
(C) for (int c = 0; c < list[0].length; c++) {
(D) for (int r = 0; r< list.length; r++) {
(C) for (int c = 0; c < list[0].length; c++) {
From which class is everything a subclass?
(A) Class
(B) object
(C) Granddaddy
(D) Object
(D) Object
What is each iteration through a list during a sort method called?
(A) an iteration
(B) a run
(C) a pass
(D) a loop
(C) passes
What type of error will the following result in?
System.out.println("Hello World")
(A) Logic Error
(B) Debugging Error
(C) Compile Time Error
(D) There is no error
(C) Compile Time Error
What is returned when mystery(7, 6) is called?
public int mystery(int x, int y) {
if (x <= y) {
return x + y;
} else {
return mystery(x, x + y) + 2;
}
}
(A) 22
(B) 33
(C) 20
(D) 13
(A) 22
Which of the following is an incorrect way to declare an ArrayList?
(A) ArrayList<Integer> = new ArrayList();
(B) ArrayList list = new ArrayList();
(C) ArrayList<int> = new ArrayList();
(D) ArrayList<Int> = new ArrayList();
(D) ArrayList<Int> = new ArrayList();
What does the following return?
System.out.println(21 + 6 + "3"+ "Hello World");
(A) 24HelloWorld
(B) 273HelloWorld
(C) 2163HelloWorld
(D) 219HelloWorld
(B) 273HelloWorld
What will list look like after two passes of a Selection Sort?
int[] list = {5, 2, 3, 1, 4};
(A) 1 5 2 3 4
(B) 1 2 3 5 4
(C) 1 2 5 3 4
(D) 1 2 3 4 5
(D) 1 2 3 4 5
What type of exception will the following result in?
int[] list = new list[6]
System.out.println(list[6]);
(A) ArrayIndexOutOfBoundsException
(B) IndexOutOfBoundsException
(C) DivisionByZeroException
(D) OutOfBoundsError
(A) ArrayIndexOutOfBoundsException
Which of the following situations would be best suited for a recursive method?
(A) sorting a list
(B) assigning a random student ID
(C) multiplication
(D) finding the sum of a list
(C) multiplication
Which of the following would work to remove all the 3s from a given ArrayList?
I.
public static ArrayList<Integer> removeThree(ArrayList<Integer> arr) {
for (Integer num : arr) {
if (num == 3) {
arr.remove(num);
}
}
return arr;
}
II.
public static ArrayList<Integer> removeThree(ArrayList<Integer> arr) {
for (int i = 0; i < arr.size()-1; i++) {
if (arr.get(i) == 3) {
arr.remove(i);
}
}
return arr;
}
III.
public static ArrayList<Integer> removeThree (ArrayList<Integer> arr) {
for (int i = 0; i < arr.size()-1; i++) {
if (arr[i] == 3) {
arr.remove(i);
}
}
return arr;
}
(A) I only
(B) II only
(C) III only
(D) I, II, and II
(B) II only
Which of the following is a string literal?
(A) String str = 'Hello World';
(B) String str = ("Hello World");
(C) String str = Hello World;
(D) String str = "Hello World";
(D) String str = "Hello World";
What kind of sort is shown below?
public static int mystery(int[] arr, int n) {
int low = 0;
int high = arr.length-1;
while (low <= high) {
int mid = (high + low) / 2;
if (a < array[mid]) {
high = mid - 1;
} else if (a > array[mid]) {
low = mid + 1;
} else {
return mid;
}
}
return -1;
}
(A) Insertion Sort
(B) Merge Sort
(C) Linear Search
(D) Binary Search
(D) Binary Search
Fill in the missing section with a declaration of a String that, when called, would result in a NullPointerException.
// missing code
System.out.println(str);
(A) String str = new String("");
(B) String str = "";
(C) String str = new String();
(D) String str;
(D) String str;
How many stars will the following print?
public void printStars(int n) {
if (n == 1) {
System.out.println("*");
} else {
System.out.println("*");
printStars(n-1);
}
}
printStars(20);
(A) 20
(B) 15
(C) 19
(D) none
Fill in the missing line to correctly finish the method sum that is designed to return the sum of a given 2D array.
public static int sum(int[][] nums) {
int count = 0;
// missing code
for (int c : row) {
count += c;
}
}
return count;
}
(A) for (int i = 0; i < nums.length; i++) {
(B) for (row : nums) {
(C) for (int[] row : nums) {
(D) for (int row : nums) {
(C) for (int[] row : nums) {
What are Integer, Double, and Character all examples of?
(A) Capital class
(B) Wrapper class
(C) Primitive objects
(D) Advanced object
What sort does the following method show?
public static void mysterySort(int[] arr) {
for (int j = 1; j < arr.length; j++) {
int temp = arr[j];
int index = j;
while (index > 0 && temp < arr[index-1]) {
arr[index] = arr[index-1';
index--;
}
arr[index] = temp;
}
}
(A) Merge Sort
(B) Selection Sort
(C) Quick Sort
(D) Insertion Sort
(D) Insertion Sort
What error does the following method result in?
public int recurse() {
return recurse();
}
(A) Compile time error
(B) Infinite loop error
(C) Logic error
(D) Stack overflow error
(D) Stack overflow error