0
0
Kotlinprogramming~5 mins

Repeat function for simple repetition in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
APrints "Hi" 4 times
BPrints "Hi" once
CPrints numbers 1 to 4
DCauses an error
What is the type of the parameter passed to repeat()?
AString
BInt
CDouble
DBoolean
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
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 */ }
What will this code print? repeat(3) { println(it * 2) }
A0 1 2
B1 2 3
C0 2 4
D2 4 6
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.