Bird
0
0

What is the correct way to create an array of integers with values 1, 2, and 3 in Kotlin?

easy📝 Conceptual Q11 of 15
Kotlin - Collections Fundamentals
What is the correct way to create an array of integers with values 1, 2, and 3 in Kotlin?
A<code>val arr = Array(3) { 1, 2, 3 }</code>
B<code>val arr = arrayOf(1, 2, 3)</code>
C<code>val arr = IntArray(1, 2, 3)</code>
D<code>val arr = [1, 2, 3]</code>
Step-by-Step Solution
Solution:
  1. Step 1: Understand array creation syntax

    Kotlin uses arrayOf() to create an array with given elements.
  2. 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.
  3. Final Answer:

    val arr = arrayOf(1, 2, 3) -> Option B
  4. Quick Check:

    Use arrayOf() for arrays with elements [OK]
Quick Trick: Use arrayOf() to create arrays with elements [OK]
Common Mistakes:
MISTAKES
  • Using square brackets [] like in other languages
  • Confusing IntArray with Array
  • Trying to pass multiple values in Array constructor lambda

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes