Bird
0
0

What will be the output of the following Kotlin code?

medium📝 Predict Output Q4 of 15
Kotlin - Collections Fundamentals
What will be the output of the following Kotlin code?
val cities = listOf("Paris", "Rome", "Oslo")
cities.forEach { println(it.first()) }
AParis Rome Oslo
B[P, R, O]
CP R O
DError: first() is not a function
Step-by-Step Solution
Solution:
  1. Step 1: Understand the code

    The list contains strings: "Paris", "Rome", "Oslo".
  2. Step 2: forEach with println(it.first())

    For each string, it.first() returns the first character.
  3. Step 3: Output

    Printing first characters results in:
    P
    R
    O
  4. Final Answer:

    P R O -> Option C
  5. Quick Check:

    String.first() returns first character [OK]
Quick Trick: first() gets first char of string elements [OK]
Common Mistakes:
MISTAKES
  • Expecting full strings to print
  • Confusing first() with first element of list
  • Thinking output is a list instead of separate lines

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes