Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
Java - Wrapper Classes

Identify the error in this code snippet:

int x = null;
Integer y = x;
ACannot assign int to Integer
BCannot assign null to primitive int
CNo error, code compiles fine
DCannot assign Integer to int
Step-by-Step Solution
Solution:
  1. Step 1: Understand null assignment rules

    Primitive types like int cannot hold null values; only objects can.
  2. Step 2: Identify error

    Assigning null to int x causes a compilation error.
  3. Final Answer:

    Cannot assign null to primitive int -> Option B
  4. Quick Check:

    Primitive int cannot be null [OK]
Quick Trick: Primitives cannot be null, only wrapper objects can [OK]
Common Mistakes:
  • Thinking null can be assigned to int
  • Confusing Integer and int assignment
  • Assuming code compiles without error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes