Questions and Answers - University of Maryland
1. Which account accurately describes copy constructor?
A. A setter updates object state under the conditions defined by the class.
B. A getter returns information from an object without needing to update it.
C. Reference identity asks if references denote the same object, or are both null.
D. A copy constructor is a user-declared constructor that initializes an object from another
instance.
Answer: D. A copy constructor is a user-declared constructor that initializes an object from another
instance.
Rationale: A copy constructor is a user-declared constructor that initializes an object from another
instance. Consider this example. A new appointment receives selected state from an existing
appointment.
2. Which pair of examples illustrates narrowing conversion followed by prefix increment?
A. Casting 6.9 to int yields 6. / A declared final limit is used instead of repeating a raw number.
B. Multiplication groups more tightly than addition in an arithmetic expression. / A counter
advances and the resulting value is assigned to another variable.
C. A counter advances and the resulting value is assigned to another variable. / Casting 6.9 to int
yields 6.
D. Casting 6.9 to int yields 6. / A counter advances and the resulting value is assigned to another
variable.
Answer: D. Casting 6.9 to int yields 6. / A counter advances and the resulting value is assigned to
another variable.
Rationale: An explicit narrowing conversion can lose fractional information or change the
representable value. A prefix increment updates the variable first and yields its incremented value.
3. What strategy can be appropriate when comparing measured doubles with tiny rounding errors?
A. Three characters
B. s.charAt(0)
C. That + operation is string concatenation
D. Use a justified tolerance appropriate to the calculation
Answer: D. Use a justified tolerance appropriate to the calculation
Rationale: Many decimal fractions have no exact finite representation as binary floating-point values.
,4. A team compares private static helper with substring. Which examples fit in the same order?
A. A part of a string begins at index 2 and ends just before index 5. / A class factors internal
arithmetic into a helper not intended for callers.
B. Two calls use separate variables both named answer. / A part of a string begins at index 2 and
ends just before index 5.
C. A class factors internal arithmetic into a helper not intended for callers. / A part of a string
begins at index 2 and ends just before index 5.
D. A class factors internal arithmetic into a helper not intended for callers. / A caller saves a
returned average in a local variable.
Answer: C. A class factors internal arithmetic into a helper not intended for callers. / A part of a string
begins at index 2 and ends just before index 5.
Rationale: A private static helper restricts direct use outside its permitted declaring context and
needs no receiver. String.substring with two indices includes the start index and excludes the end
index.
5. What should be incremented when one more input meets the qualification rule?
A. A count of qualifying inputs
B. An invariant relating the total to the processed inputs
C. The sentinel value
D. The loop condition
Answer: A. A count of qualifying inputs
Rationale: A counter tracks how many times an event or condition occurs.
6. Box a = new Box(10); Box b = a; b.set(14); 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. 14
C. 11
D. 0
Answer: B. 14
Rationale: The assignment copies a reference, so both variables denote the instance updated by set.
,7. Which account accurately describes compound assignment?
A. An explicit narrowing conversion can lose fractional information or change the representable
value.
B. A positional numeral is evaluated by multiplying its digits by powers of its base.
C. A widening primitive conversion can occur without an explicit cast in an appropriate
assignment.
D. A compound assignment performs an operation using the old value and stores the result in the
left operand.
Answer: D. A compound assignment performs an operation using the old value and stores the result
in the left operand.
Rationale: A compound assignment performs an operation using the old value and stores the result
in the left operand. Consider this example. A total is increased by the next item's cost using a
shorthand operator.
8. int n = 3; int count = 1; if (n >= 3) count += 4; if (n < 5) count *= 2; What is the value of count?
A. 5
B. 6
C. 2
D. 10
Answer: D. 10
Rationale: Both conditions are true. The second statement doubles the updated value.
9. How should a local integer limit be declared if its value must never be reassigned?
A. final int LIMIT = 20;
B. String label = null;
C. n % 2 == 0
D. score = 12;
Answer: A. final int LIMIT = 20;
Rationale: A final primitive variable can receive its permitted initial value and cannot later be
reassigned.