Java Operators & Control Flow
Master Java operators, if/else, switch statements, and loops with practical examples.
Questions in this quiz
20 questions on Java Operators & Control Flow. Work through them here, then start the quiz to answer them and see your score.
1.What is the output of this code?
int x = 10; System.out.println(x > 5 && x < 20);- A10
- Bfalse
- CCompilation error
- Dtrue
2.What does the modulo operator (%) do?
- AReturns the remainder after division
- BMultiplies two numbers
- CDivides two numbers
- DReturns the percentage
3.What is the output?
int i = 0; while (i < 3) { System.out.print(i + " "); i++; }- A0 1 2
- B0 1 2 3
- C1 2 3
- D1 2
4.What is the difference between == and .equals() when comparing Strings?
- A.equals() only works for numbers
- B== compares content, .equals() compares memory addresses
- CThey are identical
- D== compares memory addresses, .equals() compares content
5.What does a break statement do inside a loop?
- AImmediately exits the loop
- BPauses the loop temporarily
- CSkips to the next iteration
- DRestarts the loop from the beginning
6.What does a continue statement do inside a loop?
- AExits the loop
- BPauses execution
- CRestarts the entire loop
- DSkips the rest of the current iteration and goes to the next one
7.What is the output?
for (int i = 1; i <= 5; i++) { if (i % 2 == 0) continue; System.out.print(i + " "); }- A1 3 5
- B1 2 3
- C1 2 3 4 5
- D2 4
8.How many times does this loop execute?
for (int i = 0; i < 5; i += 2) { System.out.println(i); }- A3
- B4
- C2
- D5
9.What is the ternary operator in Java?
- AAn operator that works with three data types
- BAn operator with three operands used as a shorthand if-else
- CA method that returns three values
- DA loop that repeats three times
10.What is the output?
int x = 5; String result = (x > 3) ? "big" : "small"; System.out.println(result);- Abig
- Bsmall
- Ctrue
- DCompilation error
11.What happens if you forget a break in a switch case?
- AThe program crashes
- BCompilation error
- CFall-through: execution continues into the next case
- DThe switch is ignored
12.What is the output?
int day = 2; switch (day) { case 1: System.out.println("Mon"); case 2: System.out.println("Tue"); case 3: System.out.println("Wed"); break; default: System.out.println("Other"); }- AMon Tue Wed
- BTue
- CTue Wed
- DOther
13.What is a do-while loop different from a while loop?
- Ado-while always executes the body at least once
- Bwhile loops run forever
- Cdo-while runs faster
- DThere is no difference
14.What does the logical NOT operator (!) do?
- ASkips the next statement
- BAdds 1 to a value
- CFlips a boolean value (true becomes false, false becomes true)
- DChecks if two values are not equal
15.What is the output?
boolean a = true; boolean b = false; System.out.println(a || b); System.out.println(a && b);- Afalse false
- Btrue true
- Cfalse true
- Dtrue false
16.What is a nested loop?
- AA loop with no condition
- BA loop that runs backwards
- CA loop inside another loop
- DA loop that skips every other iteration
17.What is the output?
for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 2; j++) { System.out.print(i * j + " "); } }- A2 4 6
- B1 2 3 4 5 6
- C1 4 9
- D1 2 2 4 3 6
18.What is the difference between prefix (++i) and postfix (i++) increment?
- APostfix is faster than prefix
- BPrefix adds 2, postfix adds 1
- CThey are identical
- DPrefix increments then returns the new value; postfix returns the old value then increments
19.Can a switch statement in Java work with Strings?
- ANo, only integers
- BNo, only chars
- CYes, since Java 7
- DYes, since Java 1.0
20.What is the output?
int count = 0; do { count++; } while (count < 0); System.out.println(count);- A1
- B0
- C-1
- DInfinite loop