Recall & Review
beginner
How do you create an array of integers with values 1, 2, and 3 in Kotlin?
You can create it using
val numbers = arrayOf(1, 2, 3). This creates an array holding the integers 1, 2, and 3.Click to reveal answer
beginner
How do you access the second element of an array named
arr in Kotlin?Use
arr[1] because Kotlin arrays use zero-based indexing, so the first element is at index 0.Click to reveal answer
intermediate
What happens if you try to access an index outside the array bounds in Kotlin?
Kotlin throws an
ArrayIndexOutOfBoundsException. This means you tried to get or set a position that does not exist in the array.Click to reveal answer
beginner
How can you create an array of size 5 filled with zeros in Kotlin?
Use
val zeros = IntArray(5). This creates an integer array of size 5 where all elements are 0 by default.Click to reveal answer
beginner
How do you change the value of the third element in an array
arr to 10?Assign the value using
arr[2] = 10. Remember, arrays start at index 0, so the third element is at index 2.Click to reveal answer
Which of the following creates an array of strings in Kotlin?
✗ Incorrect
In Kotlin,
arrayOf() creates an array. Square brackets or new keyword are not used for arrays.What is the index of the first element in a Kotlin array?
✗ Incorrect
Kotlin arrays use zero-based indexing, so the first element is at index 0.
How do you find the size of an array named
arr in Kotlin?✗ Incorrect
Use
arr.size to get the number of elements in a Kotlin array.What will happen if you try to access
arr[5] when arr has 5 elements?✗ Incorrect
Accessing index 5 in a 5-element array is out of bounds (indices 0 to 4), so Kotlin throws an exception.
Which Kotlin function creates an integer array of size 3 with all elements initialized to 7?
✗ Incorrect
Use
IntArray(3) { 7 } to create an integer array of size 3 where each element is 7.Explain how to create and access elements in a Kotlin array.
Think about how you make the array and how you get values from it.
You got /3 concepts.
Describe what happens if you try to access an index outside the array's range in Kotlin.
What error does Kotlin give when you go too far?
You got /3 concepts.