AP Computer Science A • Unit 2.3

if Statements

Detailed answer explanations for the Unit 2.3 multiple-choice practice questions.

Q1B
Q2B
Q3D
Q4C
Q5B
Q6B
Q7A
Q8B
Q9B
Q10C
How to use this page: First try each question without looking at the explanation. Then compare your answer with the key and read why the other choices are not correct.

Answer Explanations

Q1

Consider the following code segment. What is printed as a result of executing the code segment?

boolean ready = false;
int attempts = 2;

if (ready) {
    attempts = attempts + 1;
} else {
    attempts = attempts + 3;
}

System.out.println(attempts);
A2
B5
C3
D6
Correct answer: B. Because ready is false, the else branch runs and adds 3, so attempts becomes 5.
Explanation

An if/else statement contains two branches and executes exactly one of them. The condition here is the boolean variable ready, whose value is false, so the else branch runs and the if branch is skipped entirely.

  • A is not correct: 2 is the starting value of attempts, but an if/else statement always executes one of its two branches, so the value must change.
  • C is not correct: 3 is what the if branch would produce, and that branch runs only when ready is true, which it is not.
  • D is not correct: 6 comes from tracing both branches, but after one branch finishes, execution continues after the entire if/else statement.
Q2

Consider the following code segment. What is printed as a result of executing the code segment?

int admitted = 80;
int capacity = 80;

int points = 10;
if (admitted == capacity) {
    points = points + 5;
} else {
    points = points - 2;
}
System.out.println(points);
A8
B15
C12
D10
Correct answer: B. Since admitted and capacity both hold 80, the condition is true and points becomes 15.
Explanation

The expression admitted == capacity is a relational expression, and it evaluates to true when the two values are equal. The if branch then adds 5 to points, and the else branch never runs.

  • A is not correct: 8 is the result of the else branch, which runs only when the condition is false, and here the two values are equal.
  • C is not correct: 12 matches neither branch: the if branch adds 5 to give 15, and the else branch subtracts 2 to give 8.
  • D is not correct: 10 is the starting value of points, but an if/else statement executes exactly one branch, so points cannot stay unchanged.
Q3

Consider the following code segment. What is printed as a result of executing the code segment?

int x = 6;

if (x % 2 == 0) {
    x = x + 5;
}

if (x > 10) {
    x = x * 3;
}

System.out.println(x);
A6
B11
C18
D33
Correct answer: D. 6 is even, so x becomes 11, and 11 is greater than 10, so x becomes 33.
Explanation

Two separate if statements run in order, and each condition is tested against the current value of x. The first statement changes x before the second condition is evaluated, so the second test uses 11 rather than 6.

  • A is not correct: 6 is the starting value, but the condition x % 2 == 0 is true, so the first body does execute.
  • B is not correct: 11 is the value after the first if statement, but the second condition is then tested and is also true.
  • C is not correct: 18 comes from applying x = x * 3; to the original 6, ignoring the update made by the first if statement.
Q4

Consider the following code segment. What is printed as a result of executing the code segment?

int x = 5;

if (x == 5) {
    System.out.println("A");
}

System.out.println("B");
AA only
BB only
CA followed by B
DNothing is printed.
Correct answer: C. x == 5 is true, so A prints, and the second statement sits outside the body, so B prints too.
Explanation

Braces make the entire body of an if statement one block. The second println comes after the closing brace, so it is not part of the body and executes every time the segment runs.

  • A is not correct: printing A only would require the second println to belong to the if body, but it follows the closing brace.
  • B is not correct: printing B only would require the condition to be false, and x == 5 is true when x holds 5.
  • D is not correct: printing nothing would require both statements to be skipped, yet the condition is true and the second statement is unconditional.
Q5

Consider the following code segment. What is printed as a result of executing the code segment?

int x = 3;

if (x > 5)
    System.out.println("High");
    System.out.println("Done");
AHigh only
BDone only
CHigh followed by Done
DNothing is printed.
Correct answer: B. The condition is false, and without braces the if owns only the first println, so only Done prints.
Explanation

Java permits braces to be omitted when a body contains exactly one statement, and then the if owns only the next statement. Indentation helps humans read code but does not control Java execution.

  • A is not correct: printing High only would require the condition to be true, and 3 is not greater than 5.
  • C is not correct: the indentation suggests that both lines belong to the if, but Java follows the braces, and there are none here.
  • D is not correct: the second println is not part of the body, so it executes every time regardless of the condition.
Q6

Consider the following code segment. What is printed as a result of executing the code segment?

int score = 84;

if (score + 6 >= 90) {
    score = score + 3;
} else {
    score = score - 3;
}

System.out.println(score);
A81
B87
C90
D93
Correct answer: B. Because score + 6 >= 90 is true, the if branch runs and score changes from 84 to 87.
Explanation

Evaluate the condition before reading the body. The relational expression uses the current value of score but does not change it; only the assignment inside the chosen branch does, and it adds 3 to 84.

  • A is not correct: 81 is the result of the else branch, which runs only when the condition is false, and here the condition is true.
  • C is not correct: 90 is the value of the expression tested inside the parentheses, not the value that score holds after the statement.
  • D is not correct: 93 adds 3 to the value of the condition's expression rather than to score, which still holds 84.
Q7

Consider the following code segment. What is printed as a result of executing the code segment?

int total = 7;
boolean addBonus = total < 10;

if (addBonus) {
    total = total + 4;
}

total = total * 2;
System.out.println(total);
A22
B14
C18
D28
Correct answer: A. addBonus is true, so the body adds 4 to total, and doubling afterwards makes the printed value 22.
Explanation

A boolean variable is a complete condition on its own. The expression total < 10 is evaluated first and stored in addBonus, so the body runs, and the statement after the if then executes as well.

  • B is not correct: 14 is 7 doubled with the body skipped, but addBonus holds true, so the body does execute.
  • C is not correct: 18 would require the statements to run out of order; the body adds 4 before total = total * 2; executes.
  • D is not correct: 28 is larger than this trace produces: after the body the value is 11, and doubling 11 gives 22.
Q8

What is the value of n after this code? int n = 9; if (n % 2 == 0) n += 5; else n -= 2;

A14
B7
C11
D9
Correct answer: B. 9 is not even, so n % 2 == 0 is false and the else branch subtracts, leaving 7.
Explanation

Evaluate the condition before reading either body. An if/else statement executes exactly one of its two branches, so n += 5; never runs and only the subtraction changes the value.

  • A is not correct: 14 comes from the if branch, which runs only when the condition is true, and 9 is not even.
  • C is not correct: 11 adds 2 instead of subtracting it, but the else branch runs n -= 2;, which lowers the value.
  • D is not correct: 9 leaves the value unchanged, but an if/else statement always executes one of its two branches.
Q9

What is the value of count after this code? int count = 4; if (count > 4) count *= 2; count++;

A4
B5
C8
D9
Correct answer: B. 4 is not greater than 4, so the body is skipped, and count++; still runs, giving 5.
Explanation

Without braces, an if owns only the next statement. The increment is therefore a separate statement that executes every time, while count *= 2; executes only when the condition is true.

  • A is not correct: 4 would require the increment to be skipped as well, but that statement is not part of the if body.
  • C is not correct: 8 doubles the value, which happens only when the condition is true, and count > 4 is false here.
  • D is not correct: 9 traces both the doubling and the increment, but the doubling never happens because the condition is false.
Q10

Assume score has been declared as an int variable. Which expression can be used as the condition in an if statement?

Ascore
B"true"
Cscore >= 90
D90
Correct answer: C. The condition must evaluate to a boolean, and score >= 90 is a relational expression that produces one.
Explanation

The condition between the parentheses must evaluate to a boolean. It may be a boolean variable stored earlier, or a relational expression such as a comparison written directly inside the parentheses.

  • A is not correct: score by itself is an int, and an int value is not something Java can test as a condition.
  • B is not correct: "true" is neither a boolean variable nor a relational expression, so it does not evaluate to a boolean.
  • D is not correct: 90 is a number rather than a Boolean value, and Java must decide between true and false.