Questions and Answers - University of Maryland
1. What can happen if a nonnull object cannot be treated as the requested subtype?
A. super(...)
B. A ClassCastException
C. Inheritance using extends
D. Composition
Answer: B. A ClassCastException
Rationale: A checked reference cast can fail if the referenced object is not an instance of the
requested type.
2. int n = 7; The final expression is stepCount(n). Given static int stepCount(int n) { return n <= 0 ? 0 : 1
+ stepCount(n - 2); }, what is returned?
A. 9
B. 7
C. 4
D. 3
Answer: C. 4
Rationale: Every positive invocation contributes one and recurs with n smaller by two.
3. What makes a method call recursive in the direct form?
A. After that recursive call returns
B. Method overriding
C. StackOverflowError
D. The method calls itself
Answer: D. The method calls itself
Rationale: A recursive call invokes the same method directly or through another recursive path.
,4. A team compares iterator with interface extension. Which examples fit in the same order?
A. A program requests successive values through hasNext and next. / A new interface adds a
method to an existing smaller interface contract.
B. The file imports java.util.Scanner and can then use Scanner in a declaration. / A new interface
adds a method to an existing smaller interface contract.
C. A new interface adds a method to an existing smaller interface contract. / A program requests
successive values through hasNext and next.
D. A program requests successive values through hasNext and next. / A method's comment
explains the meaning of its input count.
Answer: A. A program requests successive values through hasNext and next. / A new interface adds a
method to an existing smaller interface contract.
Rationale: An iterator provides sequential traversal of a collection through a cursor-like object. One
interface can use extends to include and build on other interface contracts.
5. Which statement best characterizes Javadoc return tag?
A. An import can make a type available by its simple name, subject to naming rules.
B. Step into follows execution into a method call where source is available.
C. hasNext reports whether a following element is available without returning it.
D. A Javadoc @return tag describes the meaning of a returned result.
Answer: D. A Javadoc @return tag describes the meaning of a returned result.
Rationale: A Javadoc @return tag describes the meaning of a returned result. Consider this example.
An API comment explains when a search method returns its failure sentinel.
6. What command should be used to follow control into a helper method?
A. Step into
B. Use @param with the parameter's name and meaning
C. A Javadoc documentation comment
D. Step over
Answer: A. Step into
Rationale: Step into follows execution into a method call where source is available.