MCQs on Control Flow Statements | Java Conditional Statements

1. If, Else, and Switch Statements

  1. Which of the following is the correct syntax for an if statement in Java?
    a) if (condition) { }
    b) if (condition) : { }
    c) if { condition }
    d) if condition { }
  2. What will the following code print? int x = 5; if (x > 3) System.out.println("Hello"); else System.out.println("World");
    a) Hello
    b) World
    c) Error
    d) Nothing
  3. Which of the following is a valid syntax for an else-if statement in Java?
    a) else if(condition) {}
    b) elseif (condition) {}
    c) else if: condition {}
    d) if else (condition) {}
  4. What does the switch statement do in Java?
    a) It selects one of many code blocks to execute based on a condition.
    b) It compares two values.
    c) It loops over a condition.
    d) It performs an arithmetic operation.
  5. Which data types can be used in a switch statement?
    a) Only integers
    b) Only strings
    c) Integers, Strings, and characters
    d) Only boolean
  6. What will the following code print? int x = 3; switch (x) { case 1: System.out.println("One"); break; case 3: System.out.println("Three"); break; default: System.out.println("Other"); }
    a) One
    b) Three
    c) Other
    d) Error
  7. Which of the following is true about the break statement in a switch block?
    a) It exits the current method.
    b) It skips the next case.
    c) It terminates the entire switch statement.
    d) It is optional after a case.
  8. Which of the following code will result in a compile-time error?
    a) if (x == 5) {}
    b) switch (x) { case 1: break; default: break; }
    c) switch (x) { case "apple": break; }
    d) if (x > 10) {}
  9. How does the default keyword work in a switch statement?
    a) It is required for every switch statement.
    b) It handles the cases that are not defined.
    c) It sets the default value of the variable.
    d) It is used for looping.
  10. What is the purpose of return in an if statement?
    a) It stops the program from executing further.
    b) It exits the method and returns control to the calling method.
    c) It skips the else block.
    d) It continues the loop.

2. Ternary Operator

  1. What is the result of the expression int result = (x > 5) ? 10 : 20; when x = 3?
    a) 10
    b) 20
    c) 3
    d) 5
  2. Which of the following is the correct syntax for a ternary operator in Java?
    a) condition ? value_if_true : value_if_false;
    b) condition ? value_if_true : ;
    c) condition : value_if_true;
    d) condition = value_if_true : value_if_false;
  3. What is the purpose of the ternary operator in Java?
    a) It is used for loop control.
    b) It allows you to assign values based on a condition.
    c) It defines a variable.
    d) It performs arithmetic operations.
  4. Which of the following is a valid ternary operator expression in Java?
    a) int x = (y == 5) ? "True" : "False";
    b) int x = (y > 5) ? y;
    c) int x = (y == 5) ? 10 : 20;
    d) int x = (y < 5) ? 10 else 20;
  5. What will the following code print? int x = 5; System.out.println((x == 5) ? "Correct" : "Incorrect");
    a) Correct
    b) Incorrect
    c) Error
    d) Nothing
  6. Which statement is true about the ternary operator?
    a) It is a one-line if-else statement.
    b) It is used for defining functions.
    c) It performs complex conditions.
    d) It replaces for loops.
  7. What happens if the expression before the ? in a ternary operator is false?
    a) The left-hand side of the operator is executed.
    b) The program throws an error.
    c) The right-hand side of the operator is executed.
    d) Nothing happens.
  8. Which of the following is the correct use of a ternary operator to assign values to a variable?
    a) int result = condition ? "Yes" : "No";
    b) int result = condition ? value;
    c) int result = condition ? value : value;
    d) int result = condition : value : value;
  9. What is the ternary operator equivalent of the following if-else statement: if (x > 10) { return 100; } else { return 200; }?
    a) return (x > 10) ? 100 : 200;
    b) return (x > 10) ? 200 : 100;
    c) return (x > 10) ? return 100 : return 200;
    d) return x > 10;
  10. Which is NOT a valid use case for the ternary operator in Java?
    a) Assigning a value to a variable
    b) Assigning a value based on multiple conditions
    c) Assigning a value based on a boolean expression
    d) Defining a method signature

3. Looping: For, While, and Do-While

  1. Which loop will execute at least once, even if the condition is false?
    a) for loop
    b) while loop
    c) do-while loop
    d) foreach loop
  2. What is the correct syntax of a for loop in Java?
    a) for (initialization; condition; increment) {}
    b) for (initialization; condition) {}
    c) for (condition; increment) {}
    d) for (initialization) {}
  3. What is the output of the following code? for (int i = 0; i < 3; i++) { System.out.print(i); }
    a) 012
    b) 123
    c) 0
    d) Error
  4. Which of the following is the correct syntax for a while loop in Java?
    a) while (condition) {}
    b) while (condition) { statement;}
    c) while statement;
    d) while (condition) statement
  5. What will the following code print? int x = 0; while (x < 5) { System.out.print(x + " "); x++; }
    a) 1 2 3 4 5
    b) 0 1 2 3 4
    c) 0 1 2 3 4 5
    d) Error
  6. Which loop will repeat a block of code as long as a condition is true, but at least once?
    a) for loop
    b) while loop
    c) do-while loop
    d) foreach loop
  7. Which statement is used to exit a loop prematurely in Java?
    a) continue
    b) exit
    c) break
    d) stop
  8. Which of the following statements is true about the for loop?
    a) It is best used when the number of iterations is known.
    b) It can only be used with integers.
    c) It runs until a condition is false.
    d) It is similar to the do-while loop.
  9. What will the following code print? for (int i = 0; i < 5; i++) { if (i == 2) break; System.out.print(i); }
    a) 01
    b) 012
    c) 0123
    d) 0
  10. Which of the following is true about the do-while loop?
    a) It checks the condition before executing the loop.
    b) It is guaranteed to execute the code block at least once.
    c) It is used for infinite loops.
    d) It requires an external if statement.

Answers Table

QnoAnswer
1a) if (condition) { }
2a) Hello
3a) else if(condition) {}
4a) It selects one of many code blocks to execute based on a condition.
5c) Integers, Strings, and characters
6b) Three
7c) It terminates the entire switch statement.
8c) switch (x) { case "apple": break; }
9b) It handles the cases that are not defined.
10b) It exits the method and returns control to the calling method.
11b) 20
12a) condition ? value_if_true : value_if_false;
13b) It allows you to assign values based on a condition.
14c) int x = (y == 5) ? 10 : 20;
15a) Correct
16a) It is a one-line if-else statement.
17c) The right-hand side of the operator is executed.
18c) int result = condition ? value : value;
19a) return (x > 10) ? 100 : 200;
20d) Defining a method signature
21c) do-while loop
22a) for (initialization; condition; increment) {}
23a) 012
24a) while (condition) {}
25b) 0 1 2 3 4
26c) do-while loop
27c) break
28a) It is best used when the number of iterations is known.
29a) 01
30b) It is guaranteed to execute the code block at least once.

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top