Bird
0
0

How can you create a new array by doubling each element of an existing IntArray named numbers in Kotlin?

hard📝 Application Q9 of 15
Kotlin - Collections Fundamentals
How can you create a new array by doubling each element of an existing IntArray named numbers in Kotlin?
Aval doubled = numbers.forEach { it * 2 }
Bval doubled = numbers.map { it * 2 }
Cval doubled = numbers.map { it * 2 }.toIntArray()
Dval doubled = numbers * 2
Step-by-Step Solution
Solution:
  1. Step 1: Use map to transform each element

    map applies a function to each element and returns a List.
  2. Step 2: Convert List back to IntArray

    toIntArray() converts the List to IntArray.
  3. Final Answer:

    val doubled = numbers.map { it * 2 }.toIntArray() -> Option C
  4. Quick Check:

    Use map + toIntArray() to transform IntArray [OK]
Quick Trick: Use map and toIntArray() to transform IntArray [OK]
Common Mistakes:
MISTAKES
  • Using forEach which returns Unit
  • Trying to multiply array directly
  • Not converting List back to IntArray

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes