Bird
Raised Fist0
Kotlinprogramming~15 mins

Array creation and usage in Kotlin - Mini Project: Build & Apply

Choose your learning style10 modes available
Array creation and usage
📖 Scenario: You are helping a small shop owner keep track of the prices of some fruits they sell. You will create an array to store the prices, then work with it to find useful information.
🎯 Goal: Build a Kotlin program that creates an array of fruit prices, sets a price threshold, finds which fruits are more expensive than that threshold, and prints those prices.
📋 What You'll Learn
Create an array with exact fruit prices
Create a price threshold variable
Use a loop or array function to find prices above the threshold
Print the filtered prices
💡 Why This Matters
🌍 Real World
Arrays are used to store lists of related data, like prices of items in a shop. Filtering arrays helps find items that meet certain conditions, such as expensive products.
💼 Career
Understanding arrays and how to filter them is essential for many programming jobs, including data analysis, app development, and backend services.
Progress0 / 4 steps
1
Create an array of fruit prices
Create an array called fruitPrices with these exact values: 50, 30, 70, 90, 20.
Kotlin
Hint

Use arrayOf() to create an array with the given numbers.

2
Set a price threshold
Create an integer variable called priceThreshold and set it to 60.
Kotlin
Hint

Use val priceThreshold = 60 to create the variable.

3
Find prices above the threshold
Create a new array called expensiveFruits that contains only the prices from fruitPrices which are greater than priceThreshold. Use the filter function.
Kotlin
Hint

Use fruitPrices.filter { price -> price > priceThreshold } and convert the result to an array with toTypedArray().

4
Print the expensive fruit prices
Print the expensiveFruits array using println. Use joinToString() to display the prices separated by commas.
Kotlin
Hint

Use println(expensiveFruits.joinToString(", ")) to print the prices separated by commas.

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

  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]
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

  1. Step 1: Understand Array constructor usage

    Kotlin's Array(size) { init } creates an array of given size, initializing each element with the lambda result.
  2. 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.
  3. Final Answer:

    val arr = Array(5) { 0 } -> Option C
  4. 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

  1. 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.
  2. Step 2: Check the printed value

    The code prints arr[1], which is 20.
  3. Final Answer:

    20 -> Option A
  4. 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

  1. Step 1: Check array size and indexing

    The array has 3 elements with indices 0, 1, 2. Accessing index 3 is out of bounds.
  2. Step 2: Understand runtime behavior

    Assigning arr[3] = 4 causes an IndexOutOfBoundsException at runtime.
  3. Final Answer:

    IndexOutOfBoundsException at arr[3] assignment -> Option D
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    val arr = Array(4) { index -> index * index } -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard