Using NULL vs null in Java: what does this program do? public class Test { public static void main (String args[]) { String str = NULL; System.out.println(str); } }
-
ANULL
-
BCompile Error
-
CCode runs but no output
-
DRuntime Exception
-
Enull
Answer
Correct Answer: Compile Error
Explanation
Introduction / Context:
This question targets the correct spelling and case-sensitivity of Java’s null literal. In Java, the literal is null (all lowercase). Writing NULL attempts to reference an identifier named NULL, which does not exist by default.
Given Data / Assumptions:
- Declares
String str = NULL;inmain. - No
NULLconstant is defined anywhere.
Concept / Approach: Java is case-sensitive. Literals like null, true, and false are reserved lowercase tokens. Using uppercase NULL is interpreted as an undeclared variable or constant, causing a compile-time “cannot find symbol” error.
Step-by-Step Solution: The compiler reads NULL as an identifier, not a literal. Because it is undeclared, the compiler reports: “cannot find symbol: variable NULL” (or similar). Compilation fails; program does not run.
Verification / Alternative check: Change to String str = null; and the code compiles; it prints null because printing a null reference writes the string “null”.
Why Other Options Are Wrong: There is no runtime because compilation fails; printing “NULL” or “null” requires a successful run.
Common Pitfalls: Confusing literals with identifiers; carrying over uppercase NULL from other languages or SQL; overlooking Java’s strict case sensitivity.
Final Answer: Compile Error