The repeat() function runs a block of code a specified number of times. It's a simple way to repeat actions without writing loops manually.
Click to reveal answer
beginner
How do you use repeat() to print "Hello" 3 times?
You write repeat(3) { println("Hello") }. This runs the print statement 3 times.
Click to reveal answer
beginner
What parameter does the repeat() function take?
It takes an integer that tells how many times to repeat the code block.
Click to reveal answer
intermediate
Can you access the current iteration index inside repeat()?
Yes, repeat() provides the current iteration index (starting from 0) as a parameter to the lambda block.
Click to reveal answer
beginner
Write a Kotlin repeat() example that prints numbers 0 to 4.
Use repeat(5) { println(it) }. The it is the current index from 0 to 4.
Click to reveal answer
What does the repeat(4) { println("Hi") } code do?
APrints "Hi" 4 times
BPrints "Hi" once
CPrints numbers 1 to 4
DCauses an error
✗ Incorrect
The repeat() function runs the code block 4 times, printing "Hi" each time.
What is the type of the parameter passed to repeat()?
AString
BInt
CDouble
DBoolean
✗ Incorrect
The repeat() function takes an integer to specify how many times to repeat.
Inside repeat(n) { }, what does it represent?
AThe current repetition index starting at 0
BThe total number of repetitions
CThe last printed value
DThe function name
✗ Incorrect
it is the current index of the repetition, starting from 0 up to n-1.
Which of these is a correct way to repeat a block 5 times in Kotlin?
Arepeat(5); { /* code */ }
Brepeat { 5 /* code */ }
Crepeat(5) -> { /* code */ }
Drepeat(5) { /* code */ }
✗ Incorrect
The correct syntax is repeat(5) { /* code */ }.
What will this code print? repeat(3) { println(it * 2) }
A0 1 2
B1 2 3
C0 2 4
D2 4 6
✗ Incorrect
The code prints the current index multiplied by 2: 0*2=0, 1*2=2, 2*2=4.
Explain how the Kotlin repeat() function works and give a simple example.
Think about how you would say 'do this 3 times' in code.
You got /3 concepts.
How can you use the iteration index inside the repeat() function? Provide a code snippet.
Remember 'it' is the current count starting at zero.
You got /3 concepts.
Practice
(1/5)
1. What does the Kotlin repeat function do?
easy
A. Defines a loop that runs until a condition is false
B. Repeats a string multiple times
C. Runs a block of code a specified number of times
D. Creates a list with repeated elements
Solution
Step 1: Understand the purpose of repeat
The repeat function is designed to run a block of code a fixed number of times.
Step 2: Compare with other options
Options B and C describe string or list repetition, which are different functions. Defines a loop that runs until a condition is false describes a conditional loop, not repeat.
Final Answer:
Runs a block of code a specified number of times -> Option C
Quick Check:
repeat = run code N times [OK]
Hint: Remember: repeat runs code block N times [OK]
Common Mistakes:
Confusing repeat with string repetition
Thinking repeat creates collections
Mixing repeat with while or for loops
2. Which of the following is the correct syntax to repeat a print statement 3 times in Kotlin?
easy
A. repeat 3 { println("Hello") }
B. repeat(3) { println("Hello") }
C. repeat(3); println("Hello")
D. repeat {3} println("Hello")
Solution
Step 1: Check the function call syntax
The repeat function requires parentheses around the number of repetitions and a lambda block with curly braces.
Step 2: Validate each option
repeat(3) { println("Hello") } correctly uses repeat(3) { ... }. Options B, C, and D have syntax errors or misplaced braces.
Final Answer:
repeat(3) { println("Hello") } -> Option B
Quick Check:
repeat(number) { code } syntax [OK]
Hint: Use parentheses for count and braces for code block [OK]
Common Mistakes:
Omitting parentheses around the count
Using semicolon instead of braces
Placing braces incorrectly
3. What will be the output of this Kotlin code?
repeat(4) { i -> println(i * 2) }
medium
A. 0 1 2 3
B. 2 4 6 8
C. 1 2 3 4
D. 0 2 4 6
Solution
Step 1: Understand the index parameter
The lambda receives i from 0 to 3 (4 times). Each iteration prints i * 2.
Step 2: Calculate each output line
For i=0: 0*2=0, i=1: 2, i=2: 4, i=3: 6. So output lines are 0, 2, 4, 6.
Final Answer:
0 2 4 6 -> Option D
Quick Check:
repeat with index * 2 = 0 2 4 6 [OK]
Hint: Index starts at 0 in repeat lambda [OK]
Common Mistakes:
Starting index at 1 instead of 0
Forgetting to multiply by 2
Confusing output with index values
4. Identify the error in this Kotlin code:
repeat(3)
println("Hi")
medium
A. Missing curly braces for the code block
B. repeat cannot be used with println
C. repeat requires a semicolon after the number
D. The number 3 should be a string
Solution
Step 1: Check the syntax of repeat
The repeat function expects a lambda block enclosed in curly braces after the count.
Step 2: Identify the missing braces
The code has no braces around println("Hi"), so it is a syntax error.
Final Answer:
Missing curly braces for the code block -> Option A
Quick Check:
repeat needs braces { } for code [OK]
Hint: Always use braces { } after repeat(count) [OK]
Common Mistakes:
Omitting braces for single statement
Adding semicolon after number
Passing number as string
5. How can you use repeat to create a list of squares of numbers from 0 to 4 in Kotlin?
hard
A. val squares = mutableListOf<Int>(); repeat(5) { squares.add(it * it) }
B. val squares = repeat(5) { it * it }
C. val squares = List(5) { repeat(it * it) }
D. val squares = repeat(5) { println(it * it) }
Solution
Step 1: Understand repeat usage for list building
The repeat function runs code multiple times but does not return a list itself.
Step 2: Check each option's approach
val squares = mutableListOf<Int>(); repeat(5) { squares.add(it * it) } creates a mutable list and adds squares inside repeat, which works. val squares = repeat(5) { it * it } tries to assign repeat's result directly, which is Unit (no list). val squares = List(5) { repeat(it * it) } misuses List and repeat. val squares = repeat(5) { println(it * it) } only prints values, no list created.
Final Answer:
val squares = mutableListOf<Int>(); repeat(5) { squares.add(it * it) } -> Option A
Quick Check:
repeat + add to list = correct list [OK]
Hint: Use repeat to add items to a mutable list [OK]
Common Mistakes:
Expecting repeat to return a list
Using repeat inside List constructor incorrectly
Only printing values without storing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.