Questions and Answers - University of Maryland
1. What should a programmer assert to reason about the running total before each pass?
A. A bounded quantity changes consistently toward its stopping condition
B. Its state never changes in a way that can end the loop
C. An invariant relating the total to the processed inputs
D. An accumulator
Answer: C. An invariant relating the total to the processed inputs
Rationale: A loop invariant is a property that holds at a specified point on every iteration.
2. String s = "orbit"; What is the value of s.substring(2, 4)?
A. it
B. o
C. bi
D. bit
Answer: C. bi
Rationale: The starting index is included and the ending index is excluded.
3. Which method compares text content rather than asking whether two references are identical?
A. As a local variable inside that method
B. String.equals
C. The method receives a copy of the int argument value
D. Declare the static helper private
Answer: B. String.equals
Rationale: String.equals compares character content when invoked on a nonnull String.
,4. int n = 13; int c = 0; while (n >= 3) { n -= 3; c++; } What is the value of n + ":" + c?
A. 1:4
B. 4:3
C. 1:5
D. 4:1
Answer: A. 1:4
Rationale: The loop subtracts three until n is below three. The two outputs count the leftover and
the number of subtractions.
5. Why might a loop with an initially true test never exit?
A. Loop initialization
B. Inside the outer loop before each row's inner loop starts
C. Use a column loop inside a row loop
D. Its state never changes in a way that can end the loop
Answer: D. Its state never changes in a way that can end the loop
Rationale: An infinite loop continues because no reachable state makes its condition stop and no exit
is taken.
6. A team compares loop initialization with off-by-one error. Which examples fit in the same order?
A. A count starts at zero before any values are read. / The program stops upon reading a specific
value reserved as a terminator.
B. A counter's value is tested against a limit before processing. / A loop intended to repeat five
times repeats six because it uses an inclusive endpoint.
C. A count starts at zero before any values are read. / A loop intended to repeat five times repeats
six because it uses an inclusive endpoint.
D. A loop intended to repeat five times repeats six because it uses an inclusive endpoint. / A count
starts at zero before any values are read.
Answer: C. A count starts at zero before any values are read. / A loop intended to repeat five times
repeats six because it uses an inclusive endpoint.
Rationale: Initialization establishes the starting state before iteration. An off-by-one error includes or
excludes an unintended boundary element.