Questions and Answers - University of Maryland
1. Which observations give valid examples of null row and row-major traversal, respectively?
A. A nested loop visits all column indices of row zero before row one. / Creating new int[4][]
allocates four reference slots without creating individual rows.
B. A value is read from the third row and second position using indices 2 and 1. / A nested loop
visits all column indices of row zero before row one.
C. Creating new int[4][] allocates four reference slots without creating individual rows. / One row
contains two entries while another contains four.
D. Creating new int[4][] allocates four reference slots without creating individual rows. / A nested
loop visits all column indices of row zero before row one.
Answer: D. Creating new int[4][] allocates four reference slots without creating individual rows. / A
nested loop visits all column indices of row zero before row one.
Rationale: When only the outer array is allocated, an unassigned row slot can be null. A row-first
nested traversal completes the selected row before advancing to the next.
2. Base a = new Derived(6); The final expression is a.getClass().getSimpleName(). Derived extends
Base, and both class names are as shown. What is returned?
A. Derived
B. ClassCastException
C. Object
D. Base
Answer: A. Derived
Rationale: getClass observes the receiver’s runtime class rather than the variable’s declared type.
3. int[][] a = { { 9, 10 }, { 12, 15 } }; What is the value of a[1][0]?
A. 12
B. 9
C. 15
D. 10
Answer: A. 12
Rationale: The first index selects the second row. The second index selects its first entry.
,4. Why can each active recursive call have a different value for the same named parameter?
A. Each recursive step moves toward a reachable base case
B. Add the current value to the sum of the remainder
C. StackOverflowError
D. Each invocation has its own frame
Answer: D. Each invocation has its own frame
Rationale: Each active method invocation has its own execution frame for parameters and local
state.
5. Which statement best characterizes two-dimensional array?
A. The selected nonnull row's length gives its individual cell count.
B. A Java two-dimensional array is an array whose elements are references to arrays.
C. The length of the outer array gives the number of row slots.
D. In grid[r][c], the first index selects a row, and the second selects a cell in that row.
Answer: B. A Java two-dimensional array is an array whose elements are references to arrays.
Rationale: A Java two-dimensional array is an array whose elements are references to arrays.
Consider this example. Storing a grid uses one outer array and a row array for each initialized row.
6. When does a statement following a recursive call normally run?
A. After that recursive call returns
B. The base case
C. Each recursive step moves toward a reachable base case
D. Each invocation has its own frame
Answer: A. After that recursive call returns
Rationale: Unwinding returns control from deeper recursive calls to their callers.