0
0
Kotlinprogramming~10 mins

Generating infinite sequences in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create an infinite sequence starting from 1.

Kotlin
val numbers = generateSequence([1]) { it + 1 }
Drag options to blanks, or click blank then click option'
A-1
B0
Cnull
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from 0 or null will change the sequence start.
2fill in blank
medium

Complete the code to generate an infinite sequence of even numbers starting from 2.

Kotlin
val evens = generateSequence([1]) { it + 2 }
Drag options to blanks, or click blank then click option'
A1
B0
C2
D-2
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from 1 or 0 will not produce even numbers.
3fill in blank
hard

Fix the error in the code to generate an infinite sequence of odd numbers starting from 1.

Kotlin
val odds = generateSequence([1]) { it + 2 }
Drag options to blanks, or click blank then click option'
A0
B1
C2
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or 2 as the start value will produce even numbers.
4fill in blank
hard

Fill both blanks to generate the infinite sequence 1, 2, 5, 14, 41, … starting from 1.

Kotlin
val squares = generateSequence([1]) { it + [2] }
Drag options to blanks, or click blank then click option'
A1
B2
Cit * 2 - 1
Dit + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed increment like 2 will not generate the correct sequence.
5fill in blank
hard

Fill all three blanks to generate an infinite Fibonacci sequence starting from 0 and 1.

Kotlin
val fibs = generateSequence(Pair([1], [2])) { Pair(it.second, it.first [3] it.second) } .map { it.first }
Drag options to blanks, or click blank then click option'
A0
B1
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or wrong starting numbers will break the Fibonacci sequence.