1Z0-803 · Question #171
Given: What is the result?
The correct answer is C. Compilation fails. The program will fail to compile because Java requires local variables to be explicitly initialized before they are used.
Question
Given:
What is the result?
Exhibit
Options
- AInitialized
- BInitialized
- CCompilation fails
- DAn exception is thrown at runtime
How the community answered
(34 responses)- A3% (1)
- C94% (32)
- D3% (1)
Why each option
The program will fail to compile because Java requires local variables to be explicitly initialized before they are used.
If a local variable is uninitialized, the code will not successfully execute to produce an output like 'Initialized' because it will not compile.
Successful execution and output are not possible if the program fails to compile due to an uninitialized local variable.
Java enforces a rule that local variables must be explicitly assigned a value before they are accessed or used in any operation. Failing to initialize a local variable before its first use results in a compile-time error, preventing the program from being built.
An uninitialized local variable is a compile-time error, not a runtime exception; the compiler detects and flags this issue during the build process.
Concept tested: Java local variable initialization
Source: https://docs.oracle.com/javase/specs/jls/se11/html/jls-4.html#jls-4.12.5
Topics
Community Discussion
No community discussion yet for this question.
