Final Exam Java
Which is the correct code to declare a catch-all handler?
catch (Throwable excpt) {...}
Which construct is the exception handler? try { if (val < 0) { throw new Exception("Error!"); } } catch (exceptionType excpt) { System.out.println(excpt.getMessage()); }
catch {...}
Which XXX will lead to stack overflow? public static void main(String[] args) { reverseCount(3); } public class RecursionExample { public static void reverseCount(int locNum) { XXX { locNum = locNum-1; System.out.println("hello"); reverseCount(locNum); } }
if(locNum <= 3)
When using outer and inner methods for recursion, typically, the outer method should check for a valid _____ and then call the inner method.
input value
Which statement declares a two-dimensional integer array called myArray with 3 rows and 4 columns?
int[][] myArray = new int[3][4];
Post-Order Traversal
left, right, root
In-Order Traversal
left, root, right
In the Stack data structure, which method removes the top element?
pop()
Which of the following recursive methods will result in a stack overflow when main() calls fact(10)?
public static int fact(int n) { if (n == 100) return 1; else return n*fact(n-1); }
What is the right code for the boolean empty() method using a Linked List?
return front==null
Pre-Order Traversal
root, left, right
Consider AVL tree. After inserting a new element as a right child of a right subtree, your tree's balance is upset, and you are required to perform rotation. What rotation is that?
single left rotation
What is output? public class RecursionExample { public static void printer() { System.out.println("hello"); printer(); } public static void main(String[] args) { printer(); } }
stack overflow
In a list implementation of a queue, the end of the list at which elements are added is called:
the rear of the queue
Which XXX and YYY will find the minimum value of all the elements in the array? Choices are in the form XXX / YYY. int[][] userVals = new int[NUM_ROWS][NUM_COLS]; minVal = userVals[0][0]; for (i = 0; i < NUM_ROWS; ++i) { for (j = 0; j < NUM_COLS; ++j) { if (XXX) { YYY; } } }
userVals[i][j] < minVal / minVal = userVals[i][j]
public class Main { public static void main(String[] args) { int[][] array = new int[3][3]; System.out.println(array[1][1]); } }
0
What is the output of the following code snippet? int[][] array = new int[2][3]; System.out.println(array.length);
2
public static void numberOfStudents(int num) { if (num >= 10) { System.out.println(num); } else { System.out.print("Bye"); numberOfStudents(num+1); } }
2
What is the output of the following code snippet? int[][] array = {{1, 2, 3}, {4, 5, 6}}; System.out.println(array[1][2]);
6
How many elements are in the array? int[][] userVals = new int[2][4];
8
try { int[] numbers = new int[5]; System.out.println(numbers[10]); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Array index out of bounds!"); }
Array index out of bounds!
The _____ case defined in a function stops recursion.
Base
What does this code do? public static int someMethod(int n) { if (n == 0) { return 0; } else { return n + someMethod(n - 1); } }
Calculates the sum of all integers from 1 to n.
A stack overflow typically causes a program to _____.
Crash
A binary search algorithm can be best described as what?
Divide and conquer technique
Which of the following statements about binary trees is NOT true?
Every binary tree has at least one node
What is the purpose of the finally block in exception handling?
It contains code that always executes, regardless of whether an exception occurs or not.
What is the worst case complexity for insertion sort?
O(n^2)
What is the worst time complexity for bubble sort?
O(n^2)
Which of the following uses First-In-First-Out (FIFO) method?
Queue
Identify the true statement about choosing a recursive method.
The problem naturally has a recursive solution.
Which of the following errors may lead to infinite recursion, causing the program to fail?
Writing a recursive method that does not always reach a base case
In a 2D array, how do you access an element at row 2, column 3?
array[1][2]