Java Topic Practice
If/Else Tracing
Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.
For practice use only.
If/Else Tracing MCQ Practice
If/Else Tracing Java practice with 15 questions, code tracing, and a worked explanation for every answer.
Answered 0 of 15
Choose one answer.
Code Tracing 01 - If/Else Tracing: track the final printed value.
int score = 18;
if (score > 36)
System.out.print("A");
else if (score > 28)
System.out.print("B");
else if (score > 20)
System.out.print("C");
else
System.out.print("D");All 15 if/else tracing questions
Work through the interactive quiz above first. This is the full if/else tracing question set with worked solutions, so students can review any question after attempting it.
1.Code Tracing 01 - If/Else Tracing: track the final printed value.
int score = 18; if (score > 36) System.out.print("A"); else if (score > 28) System.out.print("B"); else if (score > 20) System.out.print("C"); else System.out.print("D");- D
- A
- B
- C
Show worked solution
Correct answer: D
An if/else-if chain stops at the first true condition.
2.Code Tracing 12 - If/Else Tracing: follow the variable updates.
int change = -2; if (change > 0) System.out.print("up"); else if (change < 0) System.out.print("down"); else System.out.print("same");- The code does not compile.
- down
- up
- same
Show worked solution
Correct answer: down
Only one branch of the if/else-if/else chain executes.
3.Code Tracing 28 - If/Else Tracing: trace the branch and loop path.
int total = 38; int bonus = 3; int penalty = 10; if (total % 2 == 0) total += bonus; else total -= penalty; System.out.print(total);- 42
- 41
- 31
- 38
Show worked solution
Correct answer: 41
The parity test chooses exactly one update to total.
4.Code Tracing 39 - If/Else Tracing: watch the index changes.
int value = 46; int x = 5; int y = 5; if (value > 30) { value -= x; } else { value += y; } System.out.print(value);- The code does not compile.
- A runtime exception is thrown.
- 41
- 46
Show worked solution
Correct answer: 41
The condition determines which block changes value before it is printed.
5.Code Tracing 50 - If/Else Tracing: evaluate the state change step by step.
int a = 54; int b = 7; int c = 7; int d = 3; if (a > c) if (b > d) System.out.print("X"); else System.out.print("Y"); else System.out.print("Z");- Y
- Z
- The code does not compile.
- X
Show worked solution
Correct answer: X
The inner if is considered only when the outer condition is true.
6.What is printed by the following code segment?
int n = 15; String s = ""; if (n > 20) { s = "a"; } else if (n > 10) { s = "b"; } if (n > 5) { s += "c"; } else { s += "d"; } System.out.print(s);- ac
- b
- bc
- bcd
Show worked solution
Correct answer: bc
The chain assigns b, and the separate if that follows appends c because it is tested independently.
7.What is printed by the following code segment?
int a = 5; int b = 10; if (a > b) { System.out.print("x"); } else if (a == b) { System.out.print("y"); } else if (a < b) { System.out.print("z"); } else { System.out.print("w"); }- w
- y
- zw
- z
Show worked solution
Correct answer: z
The three conditions cover every case, so the trailing else is unreachable for any pair of int values.
8.What is printed by the following code segment?
int score = 85; String g; if (score >= 90) { g = "A"; } else { g = "B"; } if (score >= 80) { g = g + "+"; } System.out.print(g);- B+
- A+
- B
- A
Show worked solution
Correct answer: B+
The chain assigns B, and the independent if then appends the plus sign because 85 clears 80.
9.What is printed by the following code segment?
int x = 0; if (x > 0) { x = 1; } else if (x < 0) { x = -1; } else { x = 100; } if (x == 100) { x /= 4; } System.out.print(x);- 100
- 25
- 0
- 1
Show worked solution
Correct answer: 25
The final else runs because 0 is neither positive nor negative, and the later if then divides the updated value.
10.What is printed by the following code segment?
int a = 7; int b = 0; if (a % 2 == 1) { b += 1; } if (a % 3 == 1) { b += 2; } if (a % 5 == 2) { b += 4; } System.out.print(b);- 5
- 3
- 7
- 1
Show worked solution
Correct answer: 7
All three remainders match, so every independent if fires and the values 1, 2, and 4 accumulate.
11.What is printed by the following code segment?
String s = "hello"; if (s.length() > 3) { if (s.charAt(0) == 'h') { s = s.substring(1); } } if (s.length() > 3) { s = s.substring(0, 3); } System.out.print(s);- hel
- ello
- hello
- ell
Show worked solution
Correct answer: ell
The first block drops the leading character, and the second then tests the shortened string and keeps three characters.
12.What is printed by the following code segment?
int n = 4; int r = 0; if (n > 0) r = 1; r = 2; System.out.print(r);- 2
- 1
- 0
- The code does not compile.
Show worked solution
Correct answer: 2
Without braces the if controls only the first statement, so the second assignment always runs regardless of the condition.
13.What is printed by the following code segment?
int a = 2; if (a == 1) { a = 10; } if (a == 2) { a = 20; } if (a == 20) { a = 30; } System.out.print(a);- 20
- 30
- 2
- 10
Show worked solution
Correct answer: 30
Each independent if tests the value as it stands at that moment, so the second change cascades into the third.
14.What is printed by the following code segment?
int t = 70; String s; if (t < 60) { s = "cold"; } else if (t < 80) { s = "mild"; } else { s = "hot"; } System.out.print(s.length() + s.substring(0, 1));- 4mild
- 4c
- 4m
- 3m
Show worked solution
Correct answer: 4m
The chain selects mild, whose length is 4, and the first character is appended as text.
15.What is printed by the following code segment?
int v = 3; int out = v > 5 ? 1 : v > 2 ? 2 : 3; System.out.print(out);- 3
- 1
- The code does not compile.
- 2
Show worked solution
Correct answer: 2
Conditional operators nest from the right, so the second test runs when the first fails and selects 2.
