Bird
0
0

Find the problem in this Kotlin code:

medium📝 Debug Q7 of 15
Kotlin - Null Safety
Find the problem in this Kotlin code:
val list: List? = listOf("a", "b")
println(list?.get(2)?.toUpperCase())
AIndexOutOfBoundsException possible despite safe calls
BSafe call operator missing on list
CtoUpperCase() cannot be called on String
DNo problem, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Check safe calls on nullable list

    The safe call list?.get(2) is used, but get(2) accesses index 2 which is out of bounds.
  2. Step 2: Understand exception cause

    Safe call protects against null list, but does not prevent exceptions from invalid index access.
  3. Final Answer:

    IndexOutOfBoundsException possible despite safe calls -> Option A
  4. Quick Check:

    Safe call does not protect against invalid index errors [OK]
Quick Trick: Safe call protects null, not invalid index errors [OK]
Common Mistakes:
MISTAKES
  • Assuming safe call prevents all exceptions
  • Ignoring index bounds in list access
  • Thinking toUpperCase() is invalid on String

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes