Recall & Review
beginner
What does the Kotlin
repeat() function do?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?✗ Incorrect
The
repeat() function runs the code block 4 times, printing "Hi" each time.What is the type of the parameter passed to
repeat()?✗ Incorrect
The
repeat() function takes an integer to specify how many times to repeat.Inside
repeat(n) { }, what does it represent?✗ 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?
✗ Incorrect
The correct syntax is
repeat(5) { /* code */ }.What will this code print?
repeat(3) { println(it * 2) }✗ 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.