Questions and Answers - University of Maryland
1. A campus event calculator reads a whole number of tickets and a one-word zone from Scanner in.
All numeric variables are int unless stated otherwise. A regular ticket costs nine dollars. The entry "7
north" is valid input. What should read the ticket count as an int?
A. in.nextInt()
B. in.hasNext()
C. System.out.print(in)
D. in.nextLine()
Answer: A. in.nextInt()
Rationale: nextInt converts the next valid token to an int.
2. A campus event calculator reads a whole number of tickets and a one-word zone from Scanner in.
All numeric variables are int unless stated otherwise. A regular ticket costs nine dollars. The entry "7
north" is valid input. After reading the count with nextInt(), what does in.next() return?
A. 7
B. north
C. An empty string
D. 7 north
Answer: B. north
Rationale: The integer token was consumed. next reads the following token under the default
delimiter.
3. A campus event calculator reads a whole number of tickets and a one-word zone from Scanner in.
All numeric variables are int unless stated otherwise. A regular ticket costs nine dollars. The entry "7
north" is valid input. For int count = 7, what is count * 9?
A. 63
B. 72
C. 79
D. 16
Answer: A. 63
Rationale: Seven tickets at nine dollars each cost 63 dollars.
,4. A campus event calculator reads a whole number of tickets and a one-word zone from Scanner in.
All numeric variables are int unless stated otherwise. A regular ticket costs nine dollars. The entry "7
north" is valid input. If seven tickets are divided into groups of three using int division, what are
quotient and remainder?
A. 3 and 1
B. 2 and 0
C. 1 and 2
D. 2 and 1
Answer: D. 2 and 1
Rationale: Seven divided by three yields quotient two with one ticket left.
5. A campus event calculator reads a whole number of tickets and a one-word zone from Scanner in.
All numeric variables are int unless stated otherwise. A regular ticket costs nine dollars. The entry "7
north" is valid input. Which check requires at least one ticket and allows at most ten?
A. count <= 10
B. count > 1 && count < 10
C. count >= 1 && count <= 10
D. count == 1 && count == 10
Answer: C. count >= 1 && count <= 10
Rationale: Both inclusive bounds must hold at the same time.
6. A fee calculator uses ordered if branches. A late return with more than five days costs 12 dollars. A
late return with one to five days costs four dollars. A non-late item has cost zero. Why should a
condition for more than five days precede a broader condition for positive days in an else-if chain?
A. The later true branch always overrides an earlier true branch
B. Java always reorders conditions from most specific to least specific
C. Positive day counts cannot be compared with a literal
D. Otherwise the broader true condition would take the branch that also covers more than five
days
Answer: D. Otherwise the broader true condition would take the branch that also covers more than
five days
Rationale: An else-if chain chooses the first true branch, so order matters when conditions overlap.
,7. A fee calculator uses ordered if branches. A late return with more than five days costs 12 dollars. A
late return with one to five days costs four dollars. A non-late item has cost zero. What is the fee if
lateDays is exactly five?
A. 0 dollars
B. 12 dollars
C. 5 dollars
D. 4 dollars
Answer: D. 4 dollars
Rationale: Five is at the inclusive top end of the lower late-fee bucket.
8. A fee calculator uses ordered if branches. A late return with more than five days costs 12 dollars. A
late return with one to five days costs four dollars. A non-late item has cost zero. Which structure
permits a program to display both an overdue message and an unrelated member reminder?
A. Two independent if statements when both messages can apply
B. One else-if chain for the two messages
C. A single branch with the two conditions combined solely by negation
D. A do-while loop without any condition check on each message
Answer: A. Two independent if statements when both messages can apply
Rationale: Independent tests can both execute when both are true.
9. A fee calculator uses ordered if branches. A late return with more than five days costs 12 dollars. A
late return with one to five days costs four dollars. A non-late item has cost zero. What happens when
Java evaluates boolean ok = denominator != 0 && 20 / denominator > 1 with denominator equal to 0?
A. Java evaluates the right operand and throws ArithmeticException
B. Short-circuiting prevents the comparison of denominator with zero
C. The right operand is skipped, so there is no division
D. The quotient is rounded before division occurs
Answer: C. The right operand is skipped, so there is no division
Rationale: Conditional AND stops after a false left operand.