Bird
0
0

Given a list of Strings, how can you create a new list where each String is converted to uppercase, respecting String immutability?

hard📝 Application Q9 of 15
Kotlin - Data Types
Given a list of Strings, how can you create a new list where each String is converted to uppercase, respecting String immutability?
Alist.replaceAll { it.toUpperCase() }
Blist.forEach { it.toUpperCase() }
Cval upperList = list.map { it.toUpperCase() }
Dlist.map { it.toUpperCase() = it }
Step-by-Step Solution
Solution:
  1. Step 1: Understand how to transform list elements immutably

    Use map to create a new list with transformed elements.
  2. Step 2: Check each option for correctness

    val upperList = list.map { it.toUpperCase() } correctly uses map to return new uppercase Strings. list.forEach { it.toUpperCase() } calls toUpperCase but ignores result. list.map { it.toUpperCase() = it } has invalid assignment inside lambda. list.replaceAll { it.toUpperCase() } uses non-existent replaceAll on list.
  3. Final Answer:

    val upperList = list.map { it.toUpperCase() } -> Option C
  4. Quick Check:

    Use map to transform list immutably [OK]
Quick Trick: Use map to create new list with transformed Strings [OK]
Common Mistakes:
MISTAKES
  • Using forEach without capturing results
  • Trying to assign inside lambda
  • Using non-existent list methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes