LearnFrenzy
Sign in Take a free test

Control Flow Statements · Exercise 1 of 4

Control Flow Statements Control Flow MCQ

  • 20 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Twenty multiple-choice questions on Java control flow: if-else, classic switch and fall-through, switch expressions and yield, pattern matching switch, while, do-while, for, break, continue and labels. Pick an option to check your answer instantly.

Question 11 Easy

What does continue do inside a loop?

A Leaves the loop
B Skips the rest of the current iteration and goes to the next one
C Restarts the loop from the beginning
D Pauses the loop
Question page & discussion
Question 12 Medium

In nested loops, what does break outer; do, where outer labels the outer loop?

A Leaves only the inner loop
B Leaves the outer loop (and therefore both loops)
C Jumps to the label and restarts
D Compile error
Question page & discussion
Question 13 Medium

for (int x : arr) x = 0; – what happens to arr?

A All elements become 0
B Nothing – x is a copy of each element
C A compile error
D An exception
Question page & discussion
Question 14 Easy

How many times does for (int i = 2; i <= 20; i += 4) run?

A 4
B 5
C 6
D 18
Question page & discussion
Question 15 Easy

Which loop header runs forever (until break or return)?

A for (;;)
B for (int i = 0;;)
C while (1)
D do while ()
Question page & discussion
Question 16 Medium

What does if (x > 0); { System.out.println("positive"); } do?

A Prints only when x > 0
B Always prints – the semicolon is an empty if body and the block runs unconditionally
C Compile error
D Never prints
Question page & discussion
Question 17 Medium

Without braces, in if (a) if (b) x(); else y();, which if does the else belong to?

A The first if (a)
B The nearest if (b)
C Both
D It is a compile error
Question page & discussion
Question 18 Medium

What does javac do with a statement written directly after return; in the same block?

A Ignores it
B Reports an "unreachable statement" error
C Runs it before returning
D Warns but compiles
Question page & discussion
Question 19 Easy

When is a switch usually clearer than an if-else ladder?

A When comparing ranges like marks > 90
B When comparing one value against many constants (codes, enum constants, strings)
C Never
D Only for booleans
Question page & discussion
Question 20 Hard

Why must case Integer i when i > 0 come before case Integer i?

A Alphabetical order
B The unguarded case would match every Integer first, so a later more specific case would be unreachable (a compile error)
C Guards run last
D It does not matter
Question page & discussion
Up next · Exercise 2Predict the Output20 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links