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")
✗ 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?
A0
BDepends on the array size
C-1
D1
✗ 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?
Aarr.size
Barr.count()
Carr.length
Darr.length()
✗ 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?
AReturns the last element
BReturns null
CThrows ArrayIndexOutOfBoundsException
DCreates a new element at index 5
✗ 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?
AArray(3, 7)
BarrayOf(7, 7, 7)
CIntArray(3, {7})
DIntArray(3) { 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.
Practice
(1/5)
1. What is the correct way to create an array of integers with values 1, 2, and 3 in Kotlin?
easy
A. val arr = Array(3) { 1, 2, 3 }
B. val arr = arrayOf(1, 2, 3)
C. val arr = IntArray(1, 2, 3)
D. val arr = [1, 2, 3]
Solution
Step 1: Understand array creation syntax
Kotlin uses arrayOf() to create an array with given elements.
Step 2: Check each option
val arr = arrayOf(1, 2, 3) uses arrayOf(1, 2, 3) which is correct. val arr = [1, 2, 3] uses square brackets which is invalid in Kotlin. val arr = IntArray(1, 2, 3) which uses invalid constructor arguments for IntArray. val arr = Array(3) { 1, 2, 3 } tries to pass multiple values in a lambda which is incorrect.
Final Answer:
val arr = arrayOf(1, 2, 3) -> Option B
Quick Check:
Use arrayOf() for arrays with elements [OK]
Hint: Use arrayOf() to create arrays with elements [OK]
Common Mistakes:
Using square brackets [] like in other languages
Confusing IntArray with Array<Int>
Trying to pass multiple values in Array constructor lambda
2. Which of the following is the correct syntax to create an array of size 5 with all elements initialized to zero in Kotlin?
easy
A. val arr = Array(5, 0)
B. val arr = Array(0, 0, 0, 0, 0)
C. val arr = Array(5) { 0 }
D. val arr = IntArray(5, 0)
Solution
Step 1: Understand Array constructor usage
Kotlin's Array(size) { init } creates an array of given size, initializing each element with the lambda result.
Step 2: Evaluate options
val arr = Array(5) { 0 } correctly uses Array(5) { 0 } to create an array of 5 zeros. val arr = Array(0, 0, 0, 0, 0) is invalid syntax. val arr = Array(5, 0) is invalid syntax. val arr = IntArray(5, 0) is invalid because IntArray constructor takes only size or size with init lambda, not two parameters.
Final Answer:
val arr = Array(5) { 0 } -> Option C
Quick Check:
Use Array(size) { value } to initialize [OK]
Hint: Use Array(size) with lambda to initialize all elements [OK]
Common Mistakes:
Passing two parameters to Array constructor incorrectly
Confusing IntArray constructor parameters
Passing multiple values directly to Array constructor
3. What will be the output of the following Kotlin code?
val arr = arrayOf(10, 20, 30)
println(arr[1])
medium
A. 20
B. 10
C. 30
D. IndexOutOfBoundsException
Solution
Step 1: Understand array indexing
Arrays in Kotlin are zero-indexed, so arr[0] is 10, arr[1] is 20, and arr[2] is 30.
Step 2: Check the printed value
The code prints arr[1], which is 20.
Final Answer:
20 -> Option A
Quick Check:
Array index 1 holds 20 [OK]
Hint: Remember arrays start at index 0 in Kotlin [OK]
Common Mistakes:
Confusing index 1 with first element
Expecting 30 instead of 20
Thinking arrays start at 1
4. Identify the error in the following Kotlin code snippet:
val arr = arrayOf(1, 2, 3)
arr[3] = 4
println(arr.joinToString())
medium
A. arr is immutable, cannot assign new value
B. No error, output is '1, 2, 3, 4'
C. Syntax error in array declaration
D. IndexOutOfBoundsException at arr[3] assignment
Solution
Step 1: Check array size and indexing
The array has 3 elements with indices 0, 1, 2. Accessing index 3 is out of bounds.
Step 2: Understand runtime behavior
Assigning arr[3] = 4 causes an IndexOutOfBoundsException at runtime.
Final Answer:
IndexOutOfBoundsException at arr[3] assignment -> Option D
Quick Check:
Array indices must be within 0 to size-1 [OK]
Hint: Array indices go from 0 to size-1 only [OK]
Common Mistakes:
Trying to assign value outside array bounds
Thinking array size auto-expands
Confusing immutability with index errors
5. You want to create an array of size 4 where each element is its index squared (0, 1, 4, 9). Which Kotlin code correctly does this?
hard
A. val arr = Array(4) { index -> index * index }
B. val arr = arrayOf(4) { index -> index * index }
C. val arr = intArrayOf(4) { index -> index * index }
D. val arr = Array(4) { -> it * it }
Solution
Step 1: Understand array initialization with lambda
Kotlin's Array(size) { index -> value } creates an array where each element is initialized by the lambda using the index.
Step 2: Check each option
val arr = Array(4) { index -> index * index } uses explicit lambda parameter index and correctly computes index squared. val arr = arrayOf(4) { index -> index * index } is invalid because arrayOf does not take a size and lambda. val arr = intArrayOf(4) { index -> index * index } is invalid syntax for the intArrayOf factory function. val arr = Array(4) { -> it * it } has invalid lambda syntax where it is undefined.
Final Answer:
val arr = Array(4) { index -> index * index } -> Option A
Quick Check:
Use Array(size) with index lambda for computed values [OK]
Hint: Use Array(size) with index lambda for computed elements [OK]
Common Mistakes:
Confusing it with index in lambda
Using arrayOf() for dynamic initialization
Not using lambda for element computation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.