0
0
Kotlinprogramming~10 mins

Infix functions for readable calls 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 declare an infix function named shout for the String class.

Kotlin
infix fun String.[1]() = this.uppercase() + "!"
Drag options to blanks, or click blank then click option'
Ashout
Bprint
Clowercase
Drepeat
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name that is not descriptive of the action.
Forgetting to use the infix keyword.
2fill in blank
medium

Complete the code to call the infix function shout on the string "hello".

Kotlin
val loud = "hello" [1] shout()
Drag options to blanks, or click blank then click option'
Ainfix
B.
C->
D!!
Attempts:
3 left
💡 Hint
Common Mistakes
Using the infix keyword in the call instead of the declaration.
Using invalid operators like -> or !!.
3fill in blank
hard

Fix the error in the infix function declaration by completing the code.

Kotlin
infix fun Int.[1](times: Int): Int = this * times
Drag options to blanks, or click blank then click option'
AmultiplyBy
Brepeat
Ctimes
Dmultiply
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name that conflicts with existing Kotlin functions.
Choosing a name that is not intuitive for multiplication.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their uppercase form only if the word length is greater than 3.

Kotlin
val result = words.associateWith { it.[1]() }.filter { it.key.length [2] 3 }
Drag options to blanks, or click blank then click option'
Auppercase
B>
C<
Dlowercase
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase() instead of uppercase().
Using the wrong comparison operator.
5fill in blank
hard

Fill all three blanks to create a map from uppercase keys to their lengths, only for words longer than 4 characters.

Kotlin
val map = words.filter { it.length [1] 4 }.associateBy( [2] ) { it.[3] }
Drag options to blanks, or click blank then click option'
A>
Bit.uppercase()
Clength
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong comparison operator.
Using the word itself as key instead of uppercase.
Using a function name instead of a property for length.