Bird
0
0

What is the output of this Kotlin code?

medium📝 Predict Output Q13 of 15
Kotlin - Null Safety
What is the output of this Kotlin code?
val number: Int? = 5
number?.let { println(it * 2) }
ANo output
B10
Cnull
D5
Step-by-Step Solution
Solution:
  1. Step 1: Check if number is null

    The variable number holds 5, which is not null, so ?.let runs.
  2. Step 2: Calculate inside let block

    The lambda multiplies it (5) by 2 and prints 10.
  3. Final Answer:

    10 -> Option B
  4. Quick Check:

    Non-null value runs let, prints doubled value [OK]
Quick Trick: If variable not null, let runs and prints result [OK]
Common Mistakes:
MISTAKES
  • Expecting no output if value is not null
  • Confusing printed value with original variable
  • Thinking it prints null when variable is non-null

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes