Consider the following code segment. What is printed as a result of executing the code segment?
int spots = 4;
int notes = 2;
while (spots < 4) {
notes++;
spots++;
}
System.out.println(notes + spots);
67false at the first test, so the body is skipped and the sum 6 is printed.
Explanation
A while condition is checked before every iteration, including the first. Because spots already equals 4, the test fails immediately and neither variable changes. The statement after the loop still runs and prints the original values added together.
- B is not correct: this value would require the body to run once and increase
notes, but the condition fails before any iteration begins. - C is not correct: the print statement sits after the loop rather than inside it, so skipping the body does not prevent output.
- D is not correct: the loop cannot be infinite because its condition is already
false, and a body that never runs cannot repeat forever.