Challenge - 5 Problems
Array Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Kotlin code?
Consider the following Kotlin code snippet. What will it print when run?
Kotlin
val arr = arrayOf(1, 2, 3, 4) println(arr[2])
Attempts:
2 left
💡 Hint
Remember that Kotlin arrays are zero-indexed.
✗ Incorrect
Arrays in Kotlin start at index 0, so arr[2] accesses the third element, which is 3.
❓ Predict Output
intermediate2:00remaining
What does this Kotlin code print?
Look at this Kotlin code. What is the output?
Kotlin
val arr = IntArray(3) { it * 2 } println(arr.joinToString(","))
Attempts:
2 left
💡 Hint
The lambda uses the index to calculate each element.
✗ Incorrect
IntArray(3) creates an array of size 3. Each element is set to index * 2, so elements are 0, 2, 4.
❓ Predict Output
advanced2:00remaining
What is the output of this Kotlin code with array mutation?
What will this Kotlin code print?
Kotlin
val arr = arrayOf("a", "b", "c") arr[1] = "z" println(arr.joinToString())
Attempts:
2 left
💡 Hint
Arrays in Kotlin are mutable by default.
✗ Incorrect
The second element is changed from 'b' to 'z', so the array prints 'a, z, c'.
❓ Predict Output
advanced2:00remaining
What error does this Kotlin code produce?
What happens when this Kotlin code runs?
Kotlin
val arr = arrayOf(1, 2, 3) println(arr[3])
Attempts:
2 left
💡 Hint
Check the valid indices for the array.
✗ Incorrect
The array has indices 0,1,2. Accessing arr[3] is out of bounds and throws an exception.
🧠 Conceptual
expert2:00remaining
How many elements does this Kotlin array contain?
Given this Kotlin code, how many elements are in the array 'arr'?
Kotlin
val arr = Array(5) { i -> i * i }
Attempts:
2 left
💡 Hint
The first argument to Array() is the size.
✗ Incorrect
Array(5) creates an array with 5 elements indexed 0 to 4.