Bird
0
0

In Kotlin, what happens if you declare a variable without a ? after its type?

easy📝 Conceptual Q11 of 15
Kotlin - Null Safety
In Kotlin, what happens if you declare a variable without a ? after its type?
AThe variable cannot hold <code>null</code> values.
BThe variable can hold <code>null</code> values.
CThe variable is automatically initialized to <code>null</code>.
DThe variable must be declared as <code>lateinit</code>.
Step-by-Step Solution
Solution:
  1. Step 1: Understand Kotlin's default type behavior

    Kotlin types are non-nullable by default, meaning variables cannot hold null unless specified.
  2. Step 2: Check the effect of missing ?

    Without ?, the variable type is non-nullable, so it cannot hold null.
  3. Final Answer:

    The variable cannot hold null values. -> Option A
  4. Quick Check:

    Non-nullable type = no null allowed [OK]
Quick Trick: No ? means no null allowed [OK]
Common Mistakes:
MISTAKES
  • Thinking variables are nullable by default
  • Assuming variables initialize to null automatically
  • Confusing lateinit with nullability

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes