Questions and Answers - University of Maryland
1. How is Javadoc comment most accurately defined?
A. A breakpoint pauses a debugger at a selected executable location when reached.
B. An import can make a type available by its simple name, subject to naming rules.
C. Step into follows execution into a method call where source is available.
D. A Javadoc documentation comment can describe API contracts and produce generated
documentation.
Answer: D. A Javadoc documentation comment can describe API contracts and produce generated
documentation.
Rationale: A Javadoc documentation comment can describe API contracts and produce generated
documentation. Consider this example. A public method's documentation is written before its
declaration using /** ... */.
2. Stack<Integer> s = new Stack<>(); s.push(6); s.push(8); s.pop(); What is the value of s.peek()?
A. 10
B. 6
C. 0
D. 8
Answer: B. 6
Rationale: The most recently pushed item was removed, exposing the original item.
3. What expression yields the old value of i and also increments i?
A. Multiplication has higher precedence than addition
B. ++i
C. i++
D. Sum each digit times its corresponding power of two
Answer: C. i++
Rationale: A postfix increment yields the variable's previous value and updates the variable as a side
effect.
,4. int n = 5; boolean ok = n < 5 && ++n > 0; What is the value of n?
A. 4
B. 5
C. 6
D. 7
Answer: B. 5
Rationale: The equality boundary makes the left comparison false. Conditional AND skips the
increment.
5. Box a = new Box(9); Box b = a; b.set(13); The final expression is a.get(). Box stores an int field
initialized by its constructor. Its set and get methods write and read it. What is returned?
A. 10
B. 13
C. 0
D. 9
Answer: B. 13
Rationale: The assignment copies a reference, so both variables denote the instance updated by set.
6. How can code explicitly signal an unsuitable argument through an exception?
A. Throw an IllegalArgumentException
B. break;
C. Control falls through unless a transfer such as break stops it
D. continue;
Answer: A. Throw an IllegalArgumentException
Rationale: A throw statement transfers control by raising a particular exception object.
,7. A part of a string begins at index 2 and ends just before index 5. Which course concept gives the
most specific explanation?
A. String equals
B. Pass by value
C. Substring
D. Character indexing
Answer: C. Substring
Rationale: String.substring with two indices includes the start index and excludes the end index.
8. int[] a = new int[11]; int x = a[11]; What happens when the final statement runs?
A. NumberFormatException
B. ArrayIndexOutOfBoundsException
C. Completes normally
D. NullPointerException
Answer: B. ArrayIndexOutOfBoundsException
Rationale: An array of length n has last valid index n - 1.
9. int n = 8; The final expression is stepCount(n). Given static int stepCount(int n) { return n <= 0 ? 0 : 1
+ stepCount(n - 2); }, what is returned?
A. 5
B. 8
C. 4
D. 10
Answer: C. 4
Rationale: Every positive invocation contributes one and recurs with n smaller by two.
,10. What property makes the initialized grid rectangular?
A. grid.length
B. Each nonnull row has the same number of cells
C. grid[r].length
D. Row by row
Answer: B. Each nonnull row has the same number of cells
Rationale: A rectangular representation has nonnull rows with the same length.
11. Which observations give valid examples of interface extension and iterator next, respectively?
A. A new interface adds a method to an existing smaller interface contract. / A loop tests eligibility
for another next() call.
B. The file imports java.util.Scanner and can then use Scanner in a declaration. / A traversal
retrieves the next name and moves forward.
C. A new interface adds a method to an existing smaller interface contract. / A traversal retrieves
the next name and moves forward.
D. A traversal retrieves the next name and moves forward. / A new interface adds a method to an
existing smaller interface contract.
Answer: C. A new interface adds a method to an existing smaller interface contract. / A traversal
retrieves the next name and moves forward.
Rationale: One interface can use extends to include and build on other interface contracts. next
returns a further element and advances iteration if an element remains.
12. A class with no written constructors is instantiated with no arguments under appropriate access.
Which concept most directly fits this example?
A. toString
B. Getter
C. Implicit default constructor
D. Instance method
Answer: C. Implicit default constructor
Rationale: Java provides a default constructor when a class declares no constructor.