Using NULL vs null in Java: what does this program do?\n\npublic class Test {\n public static void main (String args[]) {\n String str = NULL;\n System.out.println(str);\n }\n}

Difficulty: Easy

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; in main.
  • No NULL constant 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

More Questions from Objects and Collections

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion