Questions and Answers - University of Maryland
1. What can be tested to distinguish an existing empty array from a nonempty array?
A. The copied reference still points to the same array
B. Whether its length is zero
C. A linear scan
D. The two array elements may refer to the same mutable object
Answer: B. Whether its length is zero
Rationale: An empty array is an allocated array with length zero and no valid element positions.
2. int[] a = new int[6]; a[0] = 5; What is the value of a[1]?
A. 5
B. 1
C. -1
D. 0
Answer: D. 0
Rationale: Only cell zero was assigned. Other int array cells retain their default zero value.
3. Amount a = new Doubled(6); The final expression is a.value(). Amount declares int value(). Doubled
stores its constructor argument and returns twice that number from value(). What is returned?
A. 18
B. 12
C. 8
D. 6
Answer: B. 12
Rationale: The contract permits a call through Amount, but the implementing object determines the
execution result.
,4. int[] a = { 5, 6 }; touch(a); The final expression is a[0]. Given static void touch(int[] z) { z[0] += 5; },
what does the expression return?
A. 5
B. 10
C. 6
D. 11
Answer: B. 10
Rationale: The parameter holds a copy of the array reference, so writing the first cell affects the
shared array.
5. Which statement best characterizes autoboxing?
A. Autoboxing converts an eligible primitive value to its corresponding wrapper in an appropriate
context.
B. compareTo communicates less than, equal to and greater than through negative, zero and
positive results.
C. ArrayList.get uses a zero-based position and retrieves an element without removing it.
D. Comparable defines a natural ordering contract through compareTo.
Answer: A. Autoboxing converts an eligible primitive value to its corresponding wrapper in an
appropriate context.
Rationale: Autoboxing converts an eligible primitive value to its corresponding wrapper in an
appropriate context. Consider this example. Assigning an int value to Integer occurs without an
explicit factory call.
6. ArrayList<Integer> a = new ArrayList<>(); a.add(2); a.add(3); a.add(0, 7); What is the value of
a.get(1)?
A. 8
B. 3
C. 2
D. 7
Answer: C. 2
Rationale: Insertion at index zero shifts the previous first element to index one.