Questions and Answers - University of Maryland
1. Matching case 2 prints something and then case 3's statements also execute without a break.
Which concept best describes this situation?
A. Switch fall-through
B. Ternary operator
C. Privacy leak on input
D. Privacy leak on return
Answer: A. Switch fall-through
Rationale: In a traditional colon-style switch, execution can continue into following cases unless it is
redirected.
2. What operation creates another way to access the same object without constructing a distinct
object?
A. continue;
B. break;
C. Reference assignment
D. ready ? max : min
Answer: C. Reference assignment
Rationale: Assigning one reference to another copies the reference and ordinarily keeps the same
original object.
3. Box a = new Box(4); Box b = new Box(a); a.set(6); The final expression is b.get(). Box(Box other)
copies the other object’s int field. set writes and get reads that field. What is returned?
A. 4
B. 6
C. 0
D. 8
Answer: A. 4
Rationale: The separate copy stores the original primitive value, independent of the later source edit.
,4. Which pair of examples illustrates privacy leak on input followed by break statement?
A. A constructor stores its incoming array then caller code edits that array. / A method rejects an
invalid argument with an IllegalArgumentException object.
B. Matching case 2 prints something and then case 3's statements also execute without a break. /
A search exits its nearest loop immediately after finding a match.
C. A constructor stores its incoming array then caller code edits that array. / A search exits its
nearest loop immediately after finding a match.
D. A search exits its nearest loop immediately after finding a match. / A constructor stores its
incoming array then caller code edits that array.
Answer: C. A constructor stores its incoming array then caller code edits that array. / A search exits
its nearest loop immediately after finding a match.
Rationale: Storing a caller's mutable reference directly can let the caller change object state later. An
unlabeled break exits the nearest enclosing switch or loop.
5. Box a = new Box(10); Box b = new Box(13); a.set(15); The final expression is b.get(). Box stores its
constructor argument in an instance field, and set and get write and read it. What does the
expression return?
A. 13
B. 18
C. 15
D. 10
Answer: A. 13
Rationale: a and b denote different Box objects. Updating a does not change b.
6. Box a = new Box(6); Box b = new Box(6); The final expression is a.equals(b) + ":" + (a == b).
Box.equals(Object) compares the stored int values of two boxes. What is the result?
A. true:true
B. false:false
C. false:true
D. true:false
Answer: D. true:false
Rationale: equals tests stored values, whereas the two new expressions create separate object
identities.