Control flow statements in Java determine the exact order in which code instructions and expressions execute during runtime. By default, code executes sequentially from top to bottom, but conditional branching constructs like if-else and switch allow programs to make decisions dynamically based on boolean evaluations. Furthermore, iteration structures including for, while, do-while, and the enhanced for-each loop handle repetitive tasks efficiently, while jump statements (break, continue, and labeled forms) provide precise control over loop execution paths and early termination.
Java Control Flow / If Else MCQs
1 min read
Correct Answer: b) The condition inside the if statement must evaluate to a boolean value.
Explanation:
In Java, the expression inside the parentheses of an if statement must strictly evaluate to a boolean type (true or false). Unlike languages like C or C++, integers or references cannot be implicitly converted to boolean values.
Correct Answer: a) A
Explanation:
Java executes if-else if ladders sequentially. Since x = 10 satisfies the first condition x > 5, it prints 'A' and skips the remaining branches of the ladder.
Correct Answer: c) long and double
Explanation:
Traditional Java switch statements support byte, short, char, int, Character, Byte, Short, Integer, String, and enum. long, float, and double are not allowed because switch values must be convertible to 32-bit integers or supported types.
Correct Answer: c) Execution falls through to the next consecutive case block.
Explanation:
Without a break statement, Java continues executing the statements of the subsequent cases regardless of whether their case labels match, a behavior known as fall-through.
Correct Answer: c) do-while loop
Explanation:
The do-while loop evaluates its termination condition at the end of the loop block, ensuring that the code inside the loop executes at least once.
Correct Answer: b) 012
Explanation:
The loop initializes i = 0, checks i < 3 (prints 0), increments i, and repeats for 1 and 2. When i becomes 3, the condition 3 < 3 is false, terminating the loop.
Correct Answer: c) 5 times
Explanation:
The loop runs for x = 5, 4, 3, 2, and 1. When x reaches 0, the condition 0 > 0 becomes false, resulting in exactly 5 iterations.
Correct Answer: b) It skips the current iteration and proceeds to the next iteration of the loop.
Explanation:
The continue statement bypasses the remaining statements in the current loop iteration and immediately evaluates the loop control expression or increment step for the next iteration.
Correct Answer: a) It can break out of any labeled block or nested loops from an inner scope.
Explanation:
A labeled break allows program control to transfer to the statement immediately following the labeled enclosing statement, making it useful for breaking out of deeply nested loops.
Correct Answer: b) 12
Explanation:
Parts of a for loop header are optional. Since i is already initialized outside, the loop starts directly with condition check i <= 2 and prints 1 and 2.
Correct Answer: b) TwoThreeDefault
Explanation:
Due to the absence of break statements after each case, execution falls through from case 2 to case 3 and the default block, printing all subsequent messages.
Correct Answer: d) Both a and c
Explanation:
Both for(;;) and for(int i=0; ; i++) create infinite loops in Java. for(true) is invalid because true is a boolean value, not an expression statement.
Correct Answer: b) 1
Explanation:
The do-while loop executes its block once, incrementing count from 0 to 1. Then the condition 1 < 0 is evaluated as false, terminating the loop and printing 1.
Correct Answer: b) throw
Explanation:
The throw statement is used to trigger an exception manually within code execution, whereas throws is used in method signatures to declare checked exceptions.
Correct Answer: b) To execute cleanup code regardless of whether an exception occurred or was caught.
Explanation:
The finally block guarantees execution after try and catch blocks complete, making it ideal for resource cleanup such as closing streams or database connections.
Correct Answer: b) The finally block executes right before the method actually returns control.
Explanation:
Even if a return statement is encountered in the try or catch block, the finally block executes before the method transfers control back to the caller.
Correct Answer: a) Any array and any class implementing java.lang.Iterable
Explanation:
The enhanced for loop works seamlessly with arrays and any collection or custom object whose class implements the java.lang.Iterable interface.
Correct Answer: b) 2
Explanation:
The enhanced for loop uses a local variable that receives a copy of each array element's value. Modifying val does not mutate the underlying elements inside the array arr.
Correct Answer: b) There is no functional difference; any for loop can be written as a while loop and vice-versa.
Explanation:
Syntactically different, for and while loops are logically interchangeable. A for loop is typically used when the number of iterations is known beforehand.
Correct Answer: c) Compile-time error
Explanation:
In Java, a single-line if statement body without curly braces cannot declare a local variable because variable declarations are declarations, not full statements.
Correct Answer: b) A break statement without a label terminates only the innermost loop containing it.
Explanation:
An unlabeled break statement exits only the innermost enclosing loop or switch statement, leaving outer loops unaffected.
Correct Answer: b) It throws a NullPointerException at runtime.
Explanation:
If a String or boxed primitive type passed as a selector expression to a Java switch statement is null, Java throws a NullPointerException at runtime.
Correct Answer: a) 012
Explanation:
The loop prints 0 and 1 while i < 2. After the loop terminates, i has been incremented to 2. The final print outputs 2, resulting in 012.
Correct Answer: b) catch
Explanation:
The catch block specifies the type of exception it can handle and contains the code executed when that exception occurs.
Correct Answer: d) 40
Explanation:
The ternary operator evaluates 10 > 20 as false, so it selects the second value (40).
Correct Answer: c) No, it causes a compile-time error.
Explanation:
Floating-point types are never permitted as switch expression selectors due to precision issues associated with floating-point comparisons.
Correct Answer: b) B
Explanation:
This is an example of the dangling-else problem. The else clause binds to the nearest preceding if (if (x > 2)). Since x = 1 is not greater than 2, the else block executes, printing 'B'.
Correct Answer: b) 0134
Explanation:
When i == 2, the continue statement skips the print statement for that iteration, resulting in 0134.
Correct Answer: b) Catching multiple exception types in a single catch block using the pipe (|) symbol.
Explanation:
Java 7 allows catching multiple exception types in a single catch block, reducing code duplication.
Correct Answer: a) Its scope is limited to the block of the for loop.
Explanation:
Variables declared in a for loop initialization statement have their scope restricted exclusively to the loop body and header.
Correct Answer: a) True
Explanation:
The expression flag = true is an assignment operator. It assigns true to flag and returns true, making the if condition evaluate to true and printing 'True'.
Correct Answer: c) return
Explanation:
The return statement immediately terminates execution of the current method and returns control to the point where the method was invoked.
Correct Answer: b) 21
Explanation:
For i=1: when j=1, breaks inner loop. For i=2: when j=1, prints 21; when j=2, breaks. Thus, it prints 21.
Correct Answer: b) They must be compile-time constants (literals or final constants).
Explanation:
Every case label value must be a compile-time constant expression; runtime variables or non-final variables cannot be used as case values.
Correct Answer: b) 012
Explanation:
The loop prints 0, 1, and 2 while x < 3, incrementing x after each print statement.
Correct Answer: b) It can be placed anywhere within the switch block, though typically placed at the end.
Explanation:
The default label is optional and can technically be placed anywhere within the switch statement, although conventions dictate placing it at the end.
Correct Answer: c) It causes a compile-time error ('exception has already been caught').
Explanation:
Java enforces strict catch block ordering. More specific exceptions must be caught before broader exceptions; otherwise, a compile-time error occurs.
Correct Answer: a) 4
Explanation:
The loop iterates for i = 1 (sum = 1) and i = 3 (sum = 1 + 3 = 4). Next iteration i = 5 fails the condition. Total sum is 4.
Correct Answer: a) Try-with-resources statement
Explanation:
Introduced in Java 7, the try-with-resources statement ensures that each resource is closed at the end of the statement, provided the resource implements AutoCloseable.
Correct Answer: b) Lesser
Explanation:
Since x = 5 is not greater than 10, the condition evaluates to false, executing the else block and printing 'Lesser'.
Correct Answer: c) 3
Explanation:
The loop increments i from 0 to 3 inside the loop body. When i becomes 3, the condition 3 < 3 fails, and 3 is printed.
Correct Answer: b) Every branch must produce a value or complete abruptly if used in an assignment context.
Explanation:
When a switch expression is used to assign values, all branches must return a valid result or throw an exception to ensure type safety.
Correct Answer: a) 531
Explanation:
The loop starts at i = 5, decrements by 2 each iteration, printing 5, then 3, then 1. When i becomes -1, the condition i > 0 fails.
Correct Answer: a) To test assumptions about program state during debugging and testing.
Explanation:
An assert statement enables developers to test boolean conditions during debugging. If disabled at runtime, assertions have no performance impact.
Correct Answer: b) An AssertionError is thrown.
Explanation:
When an assertion condition evaluates to false with assertions enabled, an AssertionError is thrown, which is a subclass of Error.
Correct Answer: c) Compile-time error
Explanation:
Local variable declarations cannot be the direct target of an if or else branch without enclosing curly braces because declarations are not statements.
Correct Answer: b) Excessive recursion without a proper base case can lead to a StackOverflowError.
Explanation:
Every recursive call allocates new stack frames. Without a terminating base case, it exhausts thread stack memory, throwing a StackOverflowError.
Correct Answer: b) Not Equal
Explanation:
The assignment a = 3 sets a to 3 and returns 3. Comparing 3 == 10 is false, so it executes the else branch and prints 'Not Equal'.
Correct Answer: a) 3
Explanation:
Iterations: i=0 -> x=0; i=1 -> x=1; i=2 -> x=3. Total final value of x is 3.
Correct Answer: b) throws
Explanation:
The throws keyword is used in a method signature to declare that the method might throw one or more checked exceptions.
Correct Answer: a) 012
Explanation:
Prints 0, 1, and 2 as i increments from 0 to 2 before stopping when i reaches 3.
Correct Answer: b) Evaluation stops as soon as the final boolean outcome is determined by the first operand.
Explanation:
In &&, if the left operand is false, the right operand is not evaluated. In ||, if the left operand is true, the right operand is not evaluated.
Correct Answer: b) 2
Explanation:
The enhanced switch expression uses multiple case labels and an arrow operator to yield 2, which is assigned to val and printed.
Correct Answer: b) Yes, as long as it has a finally block.
Explanation:
A try block must be followed by either at least one catch block, a finally block, or both.
Correct Answer: a) Inside
Explanation:
Since x = 5 satisfies both x > 3 and x < 10, it executes the innermost print statement, outputting 'Inside'.
Correct Answer: b) Both loops will execute their body successfully.
Explanation:
When the initial condition is true, both while and do-while loops execute their bodies.
Correct Answer: b) 9
Explanation:
Iterations: i=0 -> x = 10 - 0 = 10; i=1 -> x = 10 - 1 = 9. Final value of x is 9.
Correct Answer: d) try alone without catch or finally
Explanation:
A try block cannot exist by itself in Java; it must be accompanied by at least one catch clause or a finally block.
Correct Answer: b) 4
Explanation:
Numbers from 0 to 9 where i % 3 == 0 are 0, 3, 6, and 9. There are 4 such numbers, so count becomes 4.
Correct Answer: b) BC
Explanation:
Matches case 2 (prints 'B') and falls through to case 3 (prints 'C') because there is no break after case 2.
Correct Answer: b) labeled break and continue
Explanation:
While goto is a reserved keyword in Java, it is not supported. Instead, Java provides labeled break and continue statements.
Correct Answer: b) It causes a compile-time error ('variable must be effectively final').
Explanation:
Local variables accessed from within lambda expressions or inner classes must be final or effectively final.
Correct Answer: b) 43
Explanation:
Pre-decrement --i: i becomes 4 (prints 4), then i becomes 3 (prints 3). Output: 43.
Correct Answer: b) Enhanced for loop (for-each)
Explanation:
The enhanced for loop simplifies iteration over collections and arrays by eliminating explicit index management.
Correct Answer: a) OneDone
Explanation:
Arrow syntax in modern Java switch statements does not fall through. It automatically breaks after executing the matched branch.
Correct Answer: b) Yes, but only the single statement immediately following the if is bound to it.
Explanation:
Without curly braces, the scope of the if condition covers only the single statement immediately following it.
Correct Answer: b) 11
Explanation:
The post-increment expression a++ evaluates to 10 for the comparison, making the condition true. Immediately after evaluation, a increments to 11, which is printed.
Correct Answer: c) try executes, then the matching catch executes, and finally the finally block executes.
Explanation:
When an exception occurs in try, control jumps to the corresponding catch block, after which the finally block executes unconditionally.
Correct Answer: a) 3
Explanation:
Accumulation happens for i = 0 (0), i = 1 (1), and i = 2 (3). When i = 3, the loop breaks. Total sum is 3.
Correct Answer: b) It causes a compile-time error ('unhandled exception').
Explanation:
Java's checked exception mechanism requires callers to either handle checked exceptions using try-catch or declare them using throws.
Correct Answer: a) 4
Explanation:
Pre-decrement --x changes x to 4. The left side (4 == 4) evaluates to true. Because of short-circuit evaluation, the right side is never evaluated. x remains 4 and is printed.
Correct Answer: b) do-while loop
Explanation:
A do-while loop is ideal for menu-driven programs where the block must execute at least once before checking whether to repeat.
Correct Answer: a) ACBCB
Explanation:
Initialization prints A. Condition checks and prints C, then update prints B, repeating until the loop terminates. Total output: ACBCB.
Correct Answer: b) Yes, enums are fully supported selector types in Java switch statements.
Explanation:
Enums are fully supported in Java switch statements, allowing case labels to match enum constants directly.
Correct Answer: b) B
Explanation:
The inner else binds to the inner if (x > 15). Since x = 10 is not greater than 15, the inner else branch executes, printing 'B'.
Correct Answer: b) Each thread has its own call stack; exceptions must be caught within the same thread unless using Thread.UncaughtExceptionHandler.
Explanation:
Because threads execute on separate call stacks, unhandled exceptions terminate only that specific thread.
Correct Answer: b) Outside
Explanation:
Java allows labeling arbitrary blocks. The break LABEL statement terminates the labeled block immediately, outputting 'Outside'.
Correct Answer: b) It causes a compile-time error ('syntax error, insert "catch"').
Explanation:
A catch or finally block must immediately follow the try block without any intervening code.
Correct Answer: b) 23End
Explanation:
Matches case 2 (prints 2), falls through to case 3 (prints 3), and finishes by printing End.
Correct Answer: b) break exits a loop or switch, whereas continue skips the rest of the current iteration.
Explanation:
break terminates execution of the enclosing loop or switch, while continue jumps directly to the evaluation step for the next iteration.
Correct Answer: a) A
Explanation:
Since x = 5 satisfies the first condition x > 0, it prints 'A' and skips the remaining branches.
Correct Answer: b) It suppresses any previous exception thrown in the try or catch blocks.
Explanation:
If an exception is thrown in a finally block, it takes precedence and overrides any exception that was thrown in the try or catch block.
Correct Answer: b) 12
Explanation:
Inner loop runs 2 times for each outer loop iteration. i=1 adds 2; i=2 adds 4; i=3 adds 6. Total sum = 12.
Correct Answer: b) A variable whose value is never changed after its initial assignment, even without the final keyword.
Explanation:
An effectively final variable is not declared final, but its value is never modified after initialization.
Correct Answer: c) Default
Explanation:
Since x = 3 does not match cases 1 or 2, control falls through to the default branch, printing 'Default'.
Correct Answer: b) No, continue can only be used inside loops.
Explanation:
Using a continue statement outside of a loop construct causes a compile-time error.
Correct Answer: b) 4
Explanation:
Tracing the loop iterations and updates yields a final value of 4 for x.
Correct Answer: c) IOException
Explanation:
IOException is a checked exception inherited directly from Exception, requiring mandatory handling or declaration.
Correct Answer: b) B
Explanation:
Since x = 10 is not less than 5, but is less than 20, the second condition is true, printing 'B'.
Correct Answer: b) It skips the current iteration of the outer labeled loop and proceeds with its next iteration.
Explanation:
A labeled continue targets an outer enclosing loop, skipping the rest of its current iteration.
Correct Answer: b) 2
Explanation:
When i = 1, continue skips the print statement. When i = 2, it prints 2. Output is 2.
Correct Answer: b) No, booleans are not allowed in switch statements.
Explanation:
Boolean types are not permitted in switch expressions because if-else conditionals are designed for binary boolean branching.
Correct Answer: a) Even
Explanation:
Modern Java enhanced switch syntax supports multiple comma-separated case constants in a single branch, matching 4 and printing 'Even'.
Correct Answer: a) The JVM prints the exception stack trace and terminates the program.
Explanation:
If an exception reaches the top-level main thread without being caught anywhere in the call stack, the JVM prints its stack trace and exits.
Correct Answer: b) 123
Explanation:
The loop runs for x = 1, 2, and 3, printing each value before terminating when x becomes 4.
Correct Answer: a) The exception parameter in a multi-catch block is implicitly final.
Explanation:
In Java, the exception parameter in a multi-catch block is implicitly final, meaning you cannot assign a new value to it.
Correct Answer: c) The exception propagates up the call stack to the calling method.
Explanation:
If a method does not handle an exception locally, it propagates up the call stack to the calling method.
Correct Answer: b) It is not required for arrow-based branches because fall-through is prevented automatically.
Explanation:
Modern Java switch expressions using arrow syntax automatically prevent fall-through, making explicit break statements redundant.
Correct Answer: d) for (int i = 0; boolean flag = true; i++)
Explanation:
The condition part of a for loop must evaluate to a boolean expression, not a variable declaration statement.
Correct Answer: b) To ensure that resources like files or sockets are closed automatically when no longer needed.
Explanation:
The try-with-resources statement ensures automatic closure of any object implementing AutoCloseable.
Correct Answer: b) No, because System.exit() terminates the Java Virtual Machine immediately.
Explanation:
Calling System.exit() shuts down the JVM abruptly, preventing any subsequent code or finally blocks from executing.
Correct Answer: b) It is used inside code blocks in switch expressions to produce a value.
Explanation:
When a switch expression branch uses a code block rather than a single expression, the yield keyword is required to supply the value returned.
Correct Answer: b) 3
Explanation:
The condition i == j evaluates to true when (0,0), (1,1), and (2,2), running 3 times and incrementing count to 3.
Correct Answer: c) NullPointerException
Explanation:
NullPointerException is a runtime exception subclassing RuntimeException, representing programming bugs like dereferencing null.
Correct Answer: b) 8
Explanation:
Iterations: i = 1 -> 2 -> 4 -> 8. When i = 8, 8 < 5 is false, terminating and printing 8.
Correct Answer: b) 8
Explanation:
Iterations: i = 0 (2); i = 1 (4); i = 2 (8). Final value of x is 8.
Correct Answer: b) Yes, because Error subclasses Throwable, though catching errors is generally discouraged.
Explanation:
Both Exception and Error inherit from Throwable. You can technically catch Error, but errors represent abnormal, catastrophic conditions.
Correct Answer: b) It must extend Exception directly or via another checked exception.
Explanation:
To create a checked exception, a custom class must inherit from java.lang.Exception without extending RuntimeException.
Correct Answer: b) The finally block executes before the exception propagates further.
Explanation:
The finally block always executes when control leaves a try-catch construct, even if an exception was thrown inside the catch block.
Correct Answer: c) Instances of java.util.Map directly without conversion
Explanation:
Maps do not implement Iterable directly; you must call entrySet(), keySet(), or values() to iterate over them.
Correct Answer: a) 01
Explanation:
The loop body is empty. The update section prints i and increments it for i = 0 and i = 1, resulting in 01.
Correct Answer: b) The assertion statement is ignored completely with zero performance impact.
Explanation:
When assertions are disabled at runtime, assertion statements are skipped entirely by the JVM, incurring no execution overhead.
Correct Answer: b) 21
Explanation:
Pre-decrement --i: i becomes 2 (prints 2), then i becomes 1 (prints 1). Output is 21.
Related Posts

Java Operators MCQs
Java operators are special symbols and keywords used to perform operations on variables and values, governing arithmetic calculations, logical evaluations,…
October 19, 2026By MCQs Generator

JavaScript Event Handling MCQs
Event handling in JavaScript allows developers to build interactive web applications by listening for user interactions such as mouse clicks,…
August 29, 2026By MCQs Generator

C++ Classes and Objects - MCQs
In C++, a class is a fundamental building block of Object-Oriented Programming (OOP) that acts as a user-defined blueprint for…
October 19, 2026By MCQs Generator
Related Categories

AI & Data Science MCQ
5 topics
By MCQs Generator

Arts & Humanities MCQ
4 topics
By MCQs Generator

Civil Engineering MCQ
4 topics
By MCQs Generator

Commerce & Business MCQ
4 topics
By MCQs Generator

Competitive Exams MCQ
5 topics
By MCQs Generator

Electrical & Electronics Engineering MCQ
3 topics
By MCQs Generator

General Knowledge MCQ
2 topics
By MCQs Generator

General Science MCQ
4 topics
By MCQs Generator

Law & Judiciary MCQ
3 topics
By MCQs Generator

Mechanical Engineering MCQ
4 topics
By MCQs Generator

Medical & Health Sciences MCQ
4 topics
By MCQs Generator

Modern Tech Fields MCQ
3 topics
By MCQs Generator