What is the first step in the execution flow of a for loop in Java?
The initialization step is executed first, where the loop variable is set to its starting value.
Click to reveal answer
beginner
In a while loop, when is the condition checked?
The condition is checked before each iteration. If the condition is false initially, the loop body does not execute at all.
Click to reveal answer
beginner
What happens after the loop body executes in a for loop?
After the loop body executes, the update step runs (e.g., incrementing the loop variable), then the condition is checked again.
Click to reveal answer
intermediate
How does a do-while loop differ in execution flow from a while loop?
A do-while loop executes the loop body first, then checks the condition. This guarantees the loop body runs at least once.
Click to reveal answer
beginner
What causes a loop to terminate in Java?
A loop terminates when its condition evaluates to false or when a break statement is executed inside the loop body.
Click to reveal answer
In a for loop, which part runs after each iteration of the loop body?
AInitialization
BUpdate
CLoop termination
DCondition check
✗ Incorrect
After the loop body executes, the update step runs (like incrementing the loop variable).
When does the condition check happen in a while loop?
ABefore the loop body executes
BAfter the loop body executes
COnly once at the start
DNever
✗ Incorrect
The condition is checked before each iteration; if false initially, the loop body does not run.
Which loop guarantees the body runs at least once?
A<code>for</code> loop
B<code>while</code> loop
CNone of the above
D<code>do-while</code> loop
✗ Incorrect
A do-while loop runs the body first, then checks the condition.
What happens if the loop condition is false at the start of a while loop?
AThe loop body executes once
BThe loop body executes multiple times
CThe loop body does not execute
DThe loop runs infinitely
✗ Incorrect
If the condition is false initially, the while loop body does not execute at all.
Which statement can immediately exit a loop regardless of the condition?
Abreak
Bcontinue
Creturn
Dexit
✗ Incorrect
The break statement exits the loop immediately.
Explain the step-by-step execution flow of a for loop in Java.
Think about what happens before, during, and after each loop cycle.
You got /5 concepts.
Describe the difference in execution flow between a while loop and a do-while loop.
Focus on when the condition is checked relative to the loop body.
You got /4 concepts.
Practice
(1/5)
1. What does a for loop in Java primarily do?
easy
A. Executes code only once
B. Stops the program immediately
C. Repeats a block of code a specific number of times
D. Creates a new variable
Solution
Step 1: Understand the purpose of a for loop and compare options
A for loop is designed to repeat a block of code multiple times based on a condition. Only "Repeats a block of code a specific number of times" correctly describes it.
Final Answer:
Repeats a block of code a specific number of times -> Option C
Quick Check:
for loop = repeat code [OK]
Hint: For loops repeat code a set number of times [OK]
Common Mistakes:
Thinking for loops run only once
Confusing loops with variable declarations
Believing loops stop the program
2. Which of the following is the correct syntax for a basic while loop in Java?
easy
A. while (condition); { // code }
B. while condition { // code }
C. while: (condition) { // code }
D. while (condition) { // code }
Solution
Step 1: Recall Java while loop syntax and check options
The correct syntax requires parentheses around the condition and braces for the code block. Only "while (condition) { // code }" matches: while (condition) { // code }.
Final Answer:
while (condition) { // code } -> Option D
Quick Check:
while loop syntax = parentheses + braces [OK]
Hint: While loops need parentheses and braces [OK]
Common Mistakes:
Omitting parentheses around condition
Adding semicolon after while condition
Using colon instead of parentheses
3. What will be the output of this Java code?
int count = 0;
while (count < 3) {
System.out.print(count + " ");
count++;
}
medium
A. 0 1 2
B. 1 2 3
C. 0 1 2 3
D. 3 2 1
Solution
Step 1: Analyze the loop condition and trace iterations
The loop runs while count is less than 3, starting from 0. It prints 0, then increments to 1; prints 1, increments to 2; prints 2, increments to 3; then stops because 3 is not less than 3.
Final Answer:
0 1 2 -> Option A
Quick Check:
Prints 0,1,2 then stops [OK]
Hint: Count starts at 0 and stops before 3 [OK]
Common Mistakes:
Including 3 in output
Starting count from 1
Printing in reverse order
4. Identify the error in this loop code:
int i = 0;
while (i < 5) {
System.out.println(i);
}
medium
A. Missing increment of i inside the loop
B. Incorrect loop condition syntax
C. Missing semicolon after while
D. Variable i should be declared inside the loop
Solution
Step 1: Check loop control variable and consequences
The variable i is initialized but never changed inside the loop. Without incrementing i, the condition i < 5 is always true, causing an infinite loop.
Final Answer:
Missing increment of i inside the loop -> Option A
Quick Check:
Loop needs variable update to stop [OK]
Hint: Always update loop variable to avoid infinite loops [OK]
Common Mistakes:
Forgetting to increment loop variable
Misplacing semicolons
Declaring variable inside loop unnecessarily
5. Consider this code snippet:
int sum = 0;
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
sum += i;
}
System.out.println(sum);
What is the output and why?
hard
A. 15 because all numbers 1 to 5 are added
B. 12 because number 3 is skipped in addition
C. 10 because only numbers less than 3 are added
D. Error due to incorrect use of continue
Solution
Step 1: Trace the loop and effect of continue
The loop runs from 1 to 5. When i equals 3, continue skips the addition for that iteration. Sum = 1 + 2 + 4 + 5 = 12 because 3 is skipped.
Final Answer:
12 because number 3 is skipped in addition -> Option B
Quick Check:
continue skips 3, sum = 12 [OK]
Hint: Continue skips current loop iteration [OK]
Common Mistakes:
Adding 3 despite continue
Thinking continue causes error
Summing only numbers less than 3
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.