nerdexam
Oracle

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…

Java Basics

Question

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?

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)
  • A
    6% (3)
  • B
    4% (2)
  • C
    2% (1)
  • D
    88% (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

#variable declaration#compilation errors#variable scope#control flow

Community Discussion

No community discussion yet for this question.

Full 1Z0-811 Practice