1Z0-803 · Question #62
Given: What is the result?
The correct answer is C. Java.lang.NullPointerException. The code fragment attempts to perform an operation on an object reference that is currently null, leading to a NullPointerException at runtime.
Question
Given:
What is the result?
Exhibit
Options
- Anull
- Bcompilation fails
- CJava.lang.NullPointerException
- D0
How the community answered
(61 responses)- A5% (3)
- B8% (5)
- C85% (52)
- D2% (1)
Why each option
The code fragment attempts to perform an operation on an object reference that is currently `null`, leading to a `NullPointerException` at runtime.
The output `null` would occur if the program explicitly printed a null value or returned null, not if a null object was dereferenced in an invalid operation.
Compilation fails if there is a syntax error or a type mismatch, not typically for a runtime `NullPointerException` unless the compiler can detect a definite null dereference.
A `NullPointerException` occurs at runtime when an application attempts to use an object reference that has a `null` value in a context where an object is required, such as calling an instance method or accessing an instance field. In this specific case, the missing code fragment must contain an attempt to dereference a `null` object.
The output `0` would occur if the program explicitly printed the integer zero, which is unrelated to dereferencing a null object.
Concept tested: NullPointerException (NPE) and object dereferencing
Source: https://docs.oracle.com/javase/8/docs/api/java/lang/NullPointerException.html
Topics
Community Discussion
No community discussion yet for this question.
