Bird
0
0

What will be the output of the following Kotlin code? val list: List = listOf("apple", null, "banana") println(list.size) println(list[1] == null)

medium📝 Predict Output Q4 of 15
Kotlin - Null Safety
What will be the output of the following Kotlin code? val list: List = listOf("apple", null, "banana") println(list.size) println(list[1] == null)
ACompilation error
B2 false
C3 true
D3 false
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the list declaration

    The list is of type List, so it can hold null values. The list has three elements: "apple", null, and "banana".
  2. Step 2: Evaluate the print statements

    list.size returns 3 because there are three elements. list[1] is null, so list[1] == null evaluates to true.
  3. Final Answer:

    3 true -> Option C
  4. Quick Check:

    Nullable list size and null check = 3 and true [OK]
Quick Trick: Nullable list can hold nulls; size counts all elements [OK]
Common Mistakes:
MISTAKES
  • Assuming null elements are excluded from size
  • Expecting false for null equality check
  • Thinking code causes compilation error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes