Chapter 7: Conditionals
Multiple Choice · if-else Basics
Question 1: A Condition That Does Not Compile
For int x = 7;, which of the if lines below gives a compile error?
C: x = 5 is an assignment and produces an int value; an if condition must be a boolean ("int cannot be converted to boolean"). Equality is tested with ==.
Multiple Choice · if-else Basics
Question 2: Pass or Fail?
What is the output of the code below?
int grade = 45; if (grade >= 50) { System.out.println("Pass"); } else { System.out.println("Fail"); }
B: Since 45 >= 50 is false, the else block runs: Fail. Of the if and else blocks, only one ever runs.
Output Question · if-else Basics
Question 3: A Semicolon After if
What is the output of the code below?
int x = -3; if (x > 0); System.out.println("Positive"); System.out.println("Done");
Show answer
Positive Done
The ; in the line if (x > 0); is the body of the if (an empty statement). The indented println is no longer tied to the if and always runs.
Output Question · if-else Basics
Question 4: if Without Braces
What is the output of the code below?
int x = 2; if (x > 5) System.out.println("A"); System.out.println("B"); System.out.println("C");
Show answer
B C
Without braces, an if controls only the single statement right after it. The indentation of the "B" line is misleading; the compiler does not look at indentation. 2 > 5 is false → only A is skipped.
if (x > 5) { System.out.println("A"); System.out.println("B"); // if this is what you meant }
Multiple Choice · else-if Chain
Question 5: Thresholds in Reverse Order
A student wrote the letter-grade chain starting from the smallest threshold. For grade = 85, what is the output?
if (grade >= 60) System.out.println("D"); else if (grade >= 70) System.out.println("C"); else if (grade >= 80) System.out.println("B"); else if (grade >= 90) System.out.println("A"); else System.out.println("F");
B: The first true branch runs: 85 >= 60 is true → D; the rest of the chain is skipped. With this order everyone at 60 or above gets D; the C, B and A branches can never be reached. Thresholds must be written from largest to smallest.
Output Question · else-if Chain
Question 6: Independent ifs
What is the output of the code below?
int grade = 85; if (grade >= 90) System.out.println("A"); if (grade >= 80) System.out.println("B"); if (grade >= 70) System.out.println("C"); if (grade >= 60) System.out.println("D");
Show answer
B C D
All independent ifs are checked. For a single letter you need an else if chain.
Multiple Choice · else-if Chain
Question 7: Two Separate ifs
What is the output of the code below?
int x = 15; if (x > 10) System.out.print("A"); if (x > 5) System.out.print("B");
D: The two ifs are independent of each other, and both are checked: 15 > 10 is true → A, 15 > 5 is true → B. Since print does not start a new line, AB is written on the same line.
Multiple Choice · else-if Chain
Question 8: Between 10 and 20
Numbers "greater than 10 and less than 20" are wanted. Which is the correct condition for number?
C: Both conditions must be true together: &&. A is true for every number. B does not compile: 10 < number produces a boolean, and boolean < 20 is meaningless. D includes the boundaries (10 and 20) as well.
Step-by-Step Trace · Nested Decisions
Question 9: The Dangling else
Indentation can be misleading. Which if does the else belong to? What does x print?
a = 3, b = −2, x = 0. The indentation of the else makes it look as if it belongs to the outer if.
a > 0 → 3 > 0 → true: the body of the outer if (the inner if + else) is entered.
b > 0 → −2 > 0 → false.
The else is tied to the nearest if (b > 0); since the inner condition is false, it runs: x = 2.
2 is printed. To avoid ambiguity, always use braces in nested ifs.
int a = 3, b = -2, x = 0; if (a > 0) if (b > 0) x = 1; else x = 2; System.out.println(x);
Multiple Choice · Nested Decisions
Question 10: The Dangling else: a Negative
The same structure runs with different values. What is the output?
int a = -4, b = 6, x = 9; if (a > 0) if (b > 0) x = 1; else x = 2; System.out.println(x);
A: The else belongs to the inner if. Since the outer condition −4 > 0 is false, the inner if and its else are skipped entirely; x keeps its initial value 9. Reading the indentation and answering 2 is the typical mistake.
Multiple Choice · Logical Operators
Question 11: A Range Condition
What is the output of the code below?
int a = 4;
System.out.println(a > 3 && a < 10);A: 4 > 3 is true and 4 < 10 is true; && gives true when both sides are true. println writes the boolean value as true.
Multiple Choice · Logical Operators
Question 12: De Morgan
Which is equivalent to the condition !(a > 5 && b < 3)?
B: When ! is distributed inward, && ↔ || swap and each term is negated: !(a > 5) = a <= 5, !(b < 3) = b >= 3. In A, the negation of > was written as < (it should be <=, which includes 5), b was written as > instead of >=, and && was not changed.
Output Question · Logical Operators
Question 13: && vs. &
Before each expression, count = 0 is set. What is count after each expression? (check increases the counter by 1 on every call.)
static boolean check(boolean v) { count++; return v; } // (1) check(false) && check(true) // (2) check(false) & check(true) // (3) check(true) || check(false) // (4) check(true) | check(false)
Show answer
count: 1, 2, 1, 2
- (1) false && … → the result is already known, the right side is not called.
- (2)
&does not short-circuit: both sides are called. - (3) true || … → the right side is not called.
- (4)
|calls both sides.
The logical results are the same (false, false, true, true); the difference is only in the side effect. b != 0 & a / b > 1 gives a division-by-zero error when b = 0.
Step-by-Step Trace · Logical Operators
Question 14: Short-Circuit and ++k
k is incremented inside the conditions. Which ++k really runs? What is printed?
k = 0.
k > 0 → false; && short-circuits, ++k does not run. A is not printed.
k == 0 → true; || short-circuits, ++k does not run again.
The condition is true → B.
| does not short-circuit: even though the left side is true, ++k runs → k = 1; 1 > 0 is true. true | true → true.
C is printed.
k = 1: only one of the three ++k ran.
int k = 0; if (k > 0 && ++k > 0) System.out.println("A"); if (k == 0 || ++k > 0) System.out.println("B"); if (k == 0 | ++k > 0) System.out.println("C"); System.out.println(k);
Multiple Choice · Logical Operators
Question 15: Leap Year
According to the condition (year % 4 == 0 && year % 100 != 0) || year % 400 == 0, which is a leap year?
A: 2000: it is divisible by 100, so the parenthesis is false, but 2000 % 400 == 0 → true. 1900 and 2100 are divisible by 100 but not by 400 → false. 2023 is not divisible by 4 → false.
Multiple Choice · Conditional Operator and String
Question 16: Pick the Larger One
What is the output of the code below?
int a = 8, b = 3; int larger = (a > b) ? a : b; System.out.println(larger);
A: In condition ? x : y, x is chosen if the condition is true and y if it is false. Since 8 > 3 is true, larger = a = 8.
Output Question · Conditional Operator and String
Question 17: The Conditional Operator ?:
What is the output of the code below?
int a = 7, b = 12; int m = a < b ? a : b; String s = m % 2 == 0 ? "even" : "odd"; System.out.println(m + " " + s); int n = -5; String sign = n > 0 ? "+" : n < 0 ? "-" : "0"; System.out.println(sign);
Show answer
7 odd -
- 7 < 12 is true → m = a = 7 (the smaller one).
- 7 % 2 = 1 → "odd".
- Nested ?: groups from the right: n > 0 is false → (n < 0 ? "-" : "0") → "-".
Output Question · Conditional Operator and String
Question 18: == and equals
What is the output of the code below?
String a = "course";
String b = "course";
String c = new String("course");
System.out.println(a == b);
System.out.println(a == c);
System.out.println(a.equals(c));
System.out.println(c.equals("Course"));Show answer
true false true false
==compares references: a and b are the same object in the pool → true; c is a separate object → false.equalscompares contents; it is case-sensitive: "course" ≠ "Course".
Output Question · switch
Question 19: When break Is Forgotten
What is the output of the code below?
int day = 1; switch (day) { case 1: System.out.println("Mon"); case 2: System.out.println("Tue"); break; case 3: System.out.println("Wed"); default: System.out.println("None"); }
Show answer
Mon Tue
There is no break in case 1 → fall-through: the code of case 2 runs without its label being checked; the break there exits the switch.
Step-by-Step Trace · switch
Question 20: Fall-Through in switch: day = 3
In a classic switch, which case codes run? Where does break stop it?
day = 3.
switch (3): it jumps straight to the case 3 label; the code of case 1 and case 2 does not run.
Wed is printed. There is no break in this case!
Fall-through: the case 4 label is not checked, its code runs → Thu. Then break.
break jumps to the end of the switch; default does not run. Output: Wed, Thu. There is no fall-through in the arrow (->) form.
int day = 3; switch (day) { case 1: System.out.println("Mon"); break; case 2: System.out.println("Tue"); case 3: System.out.println("Wed"); case 4: System.out.println("Thu"); break; default: System.out.println("None"); }
Multiple Choice · switch
Question 21: default in the Middle
If day = 7, what does the switch below print?
switch (day) { case 1: System.out.println("Mon"); break; default: System.out.println("None"); case 2: System.out.println("Tue"); }
D: No case matches, so execution jumps to default; since default has no break, it falls through into the code of case 2 below. default does not have to be last, but the fall-through rule applies to it too.
Output Question · switch
Question 22: February with yield
What is the output of the code below?
int year = 2023, month = 2; int days = switch (month) { case 4, 6, 9, 11 -> 30; case 2 -> { System.out.println("February"); yield year % 4 == 0 ? 29 : 28; } default -> 31; }; System.out.println(days);
Show answer
February 28
- month = 2 → the block branch runs; first "February" is printed.
- In a block branch the value is given with
yield: 2023 % 4 = 3 ≠ 0 → 28. - days = 28 is printed. (Without yield it would be a compile error.)
Multiple Choice · Examples
Question 23: An Odd-Number Condition
Odd numbers from 1 to 100 can be selected with i % 2 == 1. Which odd-number condition works correctly for every int, including negatives?
B: In Java the sign of the remainder is the sign of the dividend: −7 % 2 = −1. So % 2 == 1 works only for positive odd numbers (correct for 1–100, wrong for −7). != 0 is correct in every case.
Output Question · Examples
Question 24: Absolute Value and MIN_VALUE
What is the output of the code below?
int number = Integer.MIN_VALUE; if (number < 0) { number = -number; } System.out.println(number); System.out.println(number < 0);
Show answer
-2147483648 true
- The condition is true →
-numberis computed: 2,147,483,648 does not fit in an int (the maximum is 2,147,483,647). - Overflow wraps around and gives −2,147,483,648 again; the "absolute value" stays negative.
- For this special case use long, or
Math.absExact(Java 15+) throws an exception.