30 Java Decision-Making Statements Quiz Questions

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?

Which decision-making statement is used for alternative paths of execution in a Java program?
A. loop
B. if
C. break
D. continue
if
The if statement is a decision-making statement in Java, which checks a condition and executes a code block only when the condition is true.
Which of the following is a valid Java decision-making statement?
A. if-else
B. for
C. while
D. do-while
if-else
  • 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");
A. A
B. A B
C. B
D. Compilation error
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.
Which data types can be used in a Java switch statement after Java 7 and above version? Select the most complete correct answer.
A. int, char, String, float
B. int, char only
C. byte, short, int, char, String, enum
D. All primitive types including float and double
byte, short, int, char, String, enum
  • 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.
Time complexity of an if-else statement is
A. O(n)
B. O(log n)
C. O(1)
D. Depends on compiler
O(1)
Decision-making statements take constant decision time.

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");
}
A. Five
B. Not Five
C. Five Not Five
D. Compilation error
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");
}
A. Two
B. Two Three
C. Two Three Default
D. Compilation error
Two Three 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);
A. 100
B. 200
C. true
D. Compilation error
100
  • 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.
What happens if no case matches and there is no default in a switch?
A. Compilation error
B. Runtime exception is thrown
C. Switch block is skipped; execution continues after it
D. First case always executes
Switch block is skipped; execution continues after it
  • 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");
}
A. Zero
B. Non-zero
C. Compilation error
D. Runtime error
Compilation error
  • 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.
Where can the default case be placed in a switch statement?
A. Only at the end
B. Only at the beginning
C. Anywhere — beginning, middle, or end
D. Only before the first case
Anywhere — beginning, middle, or end
  • 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);
A. 1
B. 1.0
C. Compilation error
D. 2.0
1.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);
A. Inside6
B. Inside 5
C. 6
D. Inside 6
Inside6
Java uses short-circuit evaluation with ||. Since x > 10 is false, the second condition x++ > 0 is evaluated — it is true (5 > 0) and x is incremented to 6. The if block executes printing “Inside”, then x (now 6) is printed, giving “Inside6”.

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");
}
A. Prints “No match”
B. Prints “Match”
C. Compilation error
D. Runtime exception
Compilation error
  • 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);
}
A. Greater
B. Not Greater, x=6
C. Not Greater, x=5
D. Compilation error
Not Greater, x=6
  • 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;
}
A. One to Three
B. Compilation error
C. No Output
D. One to Three Four
One to Three
  • 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");
}
A. One
B. Two
C. Three
D. Compile-time Error
Two

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);
A. 100
B. 205
C. 200
D. Compilation error
205
  • 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);
A. 5
B. 10
C. 20
D. Compilation error
10
  • 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);
A. String
B. Small Integer
C. NULL
D. NullPointerException
NULL
  • 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);
}
A. i = 2
B. i = 3
C. else i = 1
D. else i = 3
i = 3
  • 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));
    }
}
A. 5
B. 8
C. 13
D. 11
8
  • 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);
A. 22
B. 52
C. 32
D. 62
52
  • 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);
    }
}
A. high-true,1
B. high-true,3
C. high-false,1
D. high-false,2
high-true,1
  • 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;
A. 3
B. 4
C. 5
D. The answer cannot be determined until runtime.
4
  • 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);
A. 2
B. 3
C. 4
D. The code does not compile.
2
  • 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");
A. A
B. B
C. The code does not compile.
D. The code compiles but throws an exception at runtime.
The code does not compile.
  • 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");
A. None
B. One
C. Two
D. The code does not compile.
The code does not compile.
  • 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));
    }
}
A. Ball
B. Swim
C. The code does not compile due to p1.
D. The code does not compile due to p2.
The code does not compile due to p1.
  • 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);
A. 5 10
B. 10 20
C. 5 20
D. The code does not compile.
10 20
  • 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”.

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.