Welcome to the Java decision-making statements quiz. This quiz is designed to test your understanding of conditional statements in Java from basic to advanced levels.
In this quiz, you will get 30 multiple-choice questions based on if, if-else, nested if, if-else-if ladder, and switch statements. By solving these quiz questions, you can improve your logical thinking, programming skills, and confidence in writing Java programs.
There is no time limit to solve questions. At the end of the quiz, you will get the scorecard, which will show the performance in the quiz.
Java Decision-Making Statements Quiz Questions
Let’s start the MCQ quiz and check how well you know decision-making statements in Java programming! Can you score 30/30?
- Java decision-making (conditional) statements include if, if-else, if-else-if, switch, and the ternary operator (?:).
- The others — for, while, do-while — are looping/iteration statements, not decision-making ones.
What is the output of the following code?
int x = 10;
if (x < 5)
System.out.println("A");
System.out.println("B");- Without curly braces {}, only the first statement after if is its body.
- The second System.out.println(“B”) is outside the if block and always executes regardless of the condition.
- From Java 7 onwards, switch supports: byte, short, int, char, their wrapper classes (Byte, Short, Integer, Character), String, and enum.
- The data types float, double, long, and boolean are not allowed in switch statements.
- Java 21 adds pattern matching with any type.
What will be the output of the following Java code?
int a = 5;
if (a == 5) {
System.out.println("Five");
} else {
System.out.println("Not Five");
}- The condition a == 5 evaluates to true since a is 5.
- The if block executes, printing “Five”.
- The else block is skipped entirely.
What is the output of the code when executed?
int n = 2;
switch(n) {
case 1: System.out.println("One");
case 2: System.out.println("Two");
case 3: System.out.println("Three");
default: System.out.println("Default");
}- This is the classic fall-through behavior of Java switch.
- Without a break statement, execution continues from the matched case downward through all subsequent cases.
- Execution starts at case 2 and falls through to case 3 and default.
What does the ternary operator ?: return?
int x = 10;
int y = (x > 5) ? 100 : 200;
System.out.println(y);- The ternary operator has the form: condition ? valueIfTrue : valueIfFalse.
- Since x = 10 > 5 is true, the expression evaluates to 100 and assigns it to y.
- In Java, the default clause is optional.
- If no case matches and there is no default, the entire switch block is silently skipped.
- Execution continues with the statement immediately after the closing brace — no error occurs.
What is the output of the following Java code if no errors?
int x = 0;
if (x) {
System.out.println("Zero");
} else {
System.out.println("Non-zero");
}- Unlike C or C++, Java does not allow implicit numeric-to-Boolean conversion.
- The expression if(x) where x is an int causes a compile-time error: “incompatible types: int cannot be converted to boolean”.
- You must write if(x != 0) explicitly.
- The default case can appear anywhere inside a switch block.
- If placed in the middle or beginning, fall-through rules still apply — if another case matches, execution goes there, not to default.
- Default is only chosen when no other case matches, regardless of its position; however, placing it last is the conventional style.
What is the output of the following code?
System.out.println(true ? 1 : 2.0);- When a ternary has operands of different numeric types, Java promotes both to the wider type.
- Here, 1 (int) and 2.0 (double) are both promoted to double, so even though the condition is true and 1 is returned, it comes back as 1.0.
- This is determined at compile time based on type rules, not at runtime based on which branch executes.
Predict the output of the following Java code.
int x = 5;
if (x > 10 || x++ > 0) {
System.out.print("Inside");
}
System.out.print(x);What will be the output of the following code snippet?
int x = 2, y = 3;
switch(x) {
case y: System.out.println("Match"); break;
default: System.out.println("No match");
}- Switch case labels must be compile-time constants: literal values, final variables initialized with literals, or enum constants.
- A plain variable like y is not a constant — its value can change at runtime, so the compiler rejects it.
- To fix this, declare: final int y = 3;
What is the expected output of the following code snippet?
int x = 5;
if (x++ > 5) {
System.out.println("Greater");
} else {
System.out.println("Not Greater, x=" + x);
}- The post-increment x++ returns the current value 5 for the comparison, then increments x to 6.
- Since 5 > 5 is false, the else block executes.
- But x has already been incremented to 6, so the output is “Not Greater, x=6”.
What is the output of the following code?
int x = 2;
switch(x) {
case 1:
case 2:
case 3: System.out.println("One to Three"); break;
case 4: System.out.println("Four"); break;
}- This demonstrates intentional fall-through for grouping — cases 1, 2, and 3 share the same code block.
- When x = 2, case 2 is matched and execution falls through to the println in case 3.
- This pattern is the Java switch equivalent of writing: if (x == 1 || x == 2 || x == 3).
What is the output of the following Java program?
int x = 1;
x = x++ + --x;
if(x == 1) {
System.out.print("One");
} else { if(x == 2) System.out.print("Two");
else System.out.print("Three");
}What is the output of the following code snippet?
int x = 2;
int result = switch(x) {
case 1 -> 10;
case 2 -> {
int temp = x * 100;
yield temp + 5;
}
default -> 0;
};
System.out.println(result);- When a switch expression arm has a block {} instead of a simple expression, you must use yield to return a value.
- yield is like return but for switch expressions.
- Here: temp = 2 * 100 = 200, then yield 200 + 5 = 205.
- yield was introduced in Java 13 (preview) and finalized in Java 14.
Predict the output of the following code if no error is found.
int x = 5;
if (x == 5) {
x = 10;
} else if (x == 10) {
x = 20;
}
System.out.println(x);- The condition x == 5 is evaluated first — it is true, so x = 10.
- The else if is skipped entirely because the first if was already true.
- In an if-else-if ladder, once a true branch is found and executed, all subsequent else if and else branches are ignored, so the final value of x is 10.
What is the output of the following Java program?
Object obj = null;
String result = switch (obj) {
case String s -> "String";
case Integer i when i > 10 -> "Big Integer";
case Integer i -> "Small Integer";
case null -> "NULL";
default -> "Other";
};
System.out.println(result);- In Java 21, Pattern Matching Switch supports case null.
- Since obj = null, the case null arm matches and returns “NULL”.
- Without case null, the program would throw a NullPointerException.
What is the output of the following code snippet if no errors found?
int i = 0;
if (i++ == 0 && i++ == 1 && i++ == 2) {
System.out.println("i = " + i);
} else {
System.out.println("else i = " + i);
}- i++ == 0: uses i=0 (true), then i becomes 1.
- i++ == 1: uses i=1 (true), then i becomes 2.
- i++ == 2: uses i=2 (true), then i becomes 3.
- All three are true, so the && result is true, the if block executes, and at that point i = 3. Output: “i = 3”.
Consider the following program. What will be the expected output?
public class Test {
static int f(int x) {
if (x <= 1) return x;
return f(x - 1) + f(x - 2);
}
public static void main(String[] args) {
System.out.println(f(6));
}
}- This is Fibonacci with a decision-making base case if(x <= 1) return x.
- The if statement acts as the base case of the recursion — a critical decision gate that stops infinite recursion.
- The sequence: f(0)=0, f(1)=1, f(2)=1, f(3)=2, f(4)=3, f(5)=5, f(6)=8.
- f(6) = f(5) + f(4) = 5 + 3 = 8.
What is the output of the following code snippet?
int x = 2;
switch(x) {
case 1: x += 10;
case 2: x += 20;
case 3: x += 30; break;
case 4: x += 40;
}
System.out.println(x);- case 1: skipped (no match).
- case 2: matches. x += 20 → x = 22. No break → fall-through.
- case 3: x += 30 → x = 52. break → exit switch.
- case 4: not reached. Output: 52.
Consider the following Java program. What is the output if no errors?
public class Final {
static int counter = 0;
static boolean check(int n) {
counter++;
return n > 0;
}
public static void main(String[] args) {
int x = 3;
boolean r = check(1) || check(-1) && check(2);
String s = switch(x) {
case 1, 2 -> "low";
case 3 -> (r ? "high-true" : "high-false");
default -> "other";
};
System.out.println(s + "," + counter);
}
}- Operator precedence: && binds tighter than ||, so the expression is: check(1) || (check(-1) && check(2)).
- Short-circuit: check(1) returns true (1 > 0), counter = 1. Since the left side of || is true, the right side is never evaluated; counter stays at 1.
- r = true. Switch: x = 3 → case 3 → ternary: r is true → “high-true”.
- Result: “high-true,1”.
What is the value of x after the execution of the following code snippet?
long x = 5 >= 5 ? 1+2 : 1*1;
if(++x < 4)
x += 1;- The expression 5 >= 5 is true, so the ternary evaluates 1 + 2 = 3, giving x = 3.
- Then ++x (pre-increment) makes x = 4, and 4 < 4 is false, so the x += 1 block is skipped. The final value is 4.
What is the value of y after the execution of the following code snippet?
int x = 6;
long y = 3;
if(x % 3 >= 1)
y++;
y--;
System.out.print(y);- x % 3 → 6 % 3 = 0, and 0 >= 1 is false.
- Without curly braces, only y++ belongs to the if body, so it is skipped.
- However, y– is always executed because it is outside the if block. Starting at 3, y– gives 2.
Predict the output of the following code snippet if no errors.
int a = 0;
int b = 0;
b = a++;
if(b)
System.out.print("A");
else
System.out.print("B");- Java’s if statement requires a boolean expression.
- b is an int, and writing if(b) is illegal in Java — the code fails at compile time.
How many 1s are outputted when the following code snippet is compiled and run?
int x = 100;
int y = 200;
if (x < 150) {
System.out.print("1");
} else if (y && x > 1000) {
System.out.print("2");
} if (y < 500)
System.out.print("1");
else
System.out.print("2");- The condition y && x > 1000 is invalid because y is an int, and the && operator requires boolean operands on both sides.
- Java does not implicitly convert integers to booleans, so this is a compile-time error.
What is the output of the following application?
public class Baby {
public static String play(int toy, int age) {
final String game;
if(toy < 2)
game = age > 1 ? 1 : 10; // p1
else
game = age > 3 ? "Ball" : "Swim"; // p2
return game;
}
public static void main(String[] variables) {
System.out.print(play(5, 2));
}
}- At line p1: game = age > 1 ? 1 : 10; — the ternary produces an int (either 1 or 10), but game is declared as String.
- Java does not allow assigning an int directly to a String, causing a type mismatch compile error at p1.
- The else branch (p2) is perfectly valid.
What does this print?
int x = 5;
int y = switch (x) {
case 5 -> {
x = 10;
yield x * 2;
}
default -> 0;
};
System.out.println(x + " " + y);- x = 5 → switch(5) matches case 5, enters the block.
- x = 10 runs — there is no restriction on mutating local variables inside a switch expression block.
- yield x * 2 — x is already 10 at this point, so yield produces 20; y = 20.
- System.out.println(x + ” ” + y) — x is 10, y is 20 → prints “10 20”.





