0
0
Kotlinprogramming~5 mins

Array creation and usage in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Aval arr = ["apple", "banana", "cherry"]
Bval arr = arrayOf("apple", "banana", "cherry")
Cval arr = new Array("apple", "banana", "cherry")
Dval arr = ArrayList("apple", "banana", "cherry")
What is the index of the first element in a Kotlin array?
A0
BDepends on the array size
C-1
D1
How do you find the size of an array named arr in Kotlin?
Aarr.size
Barr.count()
Carr.length
Darr.length()
What will happen if you try to access arr[5] when arr has 5 elements?
AReturns the last element
BReturns null
CThrows ArrayIndexOutOfBoundsException
DCreates a new element at index 5
Which Kotlin function creates an integer array of size 3 with all elements initialized to 7?
AArray(3, 7)
BarrayOf(7, 7, 7)
CIntArray(3, {7})
DIntArray(3) { 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.