Difficulty: Easy
Correct Answer: String s1 = null;
Explanation:
Introduction / Context:
Java treats java.lang.String as a reference type. A declaration must follow Java’s literal and casting rules. This question checks your understanding of valid String initializations and the difference between character literals, Unicode escapes, and reference nulls.
Given Data / Assumptions:
Concept / Approach:
A String variable may be assigned either a String object (e.g., a double-quoted literal) or the null reference. Casting a char to String is not legal without calling an API (such as Character.toString or String.valueOf). Single quotes are for char, not String.
Step-by-Step Solution:
Verification / Alternative check:
Compile the snippets individually; only option A compiles without extra helper methods.
Why Other Options Are Wrong:
They either misuse single quotes (char literal) or perform an invalid cast from char to String.
Common Pitfalls:
Confusing ' ' (char) with "" "" (String), and assuming casts can magically convert primitives to reference types.
Final Answer:
String s1 = null;
Discussion & Comments