1Z0-811 · Question #72
Given the code fragment: public static void main(String[] args) { int iterations = 100; while (count < iterations) { System.out.println("Iteration " + count); count++; } } What is the result?
The correct answer is D. An error occurs during compilation. Option D is correct because the variable count is never declared anywhere in the code - only iterations is declared. Java's compiler performs strict type checking and will refuse to compile any code that references an undeclared variable, throwing a cannot find symbol error…
Question
Options
- AThe program compiles and nothing is printed.
- BIteration plus an increasing number is printed 100 times.
- CIteration plus an increasing number is printed 99 times.
- DAn error occurs during compilation.
How the community answered
(51 responses)- A6% (3)
- B4% (2)
- C2% (1)
- D88% (45)
Explanation
Option D is correct because the variable count is never declared anywhere in the code - only iterations is declared. Java's compiler performs strict type checking and will refuse to compile any code that references an undeclared variable, throwing a cannot find symbol error before the program ever runs.
Options A, B, and C are all wrong for the same fundamental reason: they describe runtime behavior, but the program never reaches runtime. A compilation error halts everything before execution begins, so questions about what gets printed are moot.
Memory tip: In Java, declaration before use is a compile-time rule, not a runtime one. Whenever you see a variable used without a matching declaration in scope, think "compiler catches it" - the answer will always point to a compilation error, not a runtime result.
Topics
Community Discussion
No community discussion yet for this question.