0
0
Kotlinprogramming~10 mins

Extension properties 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 extension property isEven for Int that returns true if the number is even.

Kotlin
val Int.isEven: Boolean
    get() = this [1] 2 == 0
Drag options to blanks, or click blank then click option'
A*
B/
C+
D%
Attempts:
3 left
💡 Hint
Common Mistakes
Using division (/) instead of modulus (%)
Using multiplication (*) or addition (+) operators
2fill in blank
medium

Complete the code to declare an extension property lastChar for String that returns the last character.

Kotlin
val String.lastChar: Char
    get() = this[1]
Drag options to blanks, or click blank then click option'
A.last()
B[0]
C.first()
D[length - 1]
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 0 which returns the first character
Using length as an index which is out of bounds
3fill in blank
hard

Fix the error in the extension property that returns the first half of a string.

Kotlin
val String.firstHalf: String
    get() = this.substring(0, this.length [1] 2)
Drag options to blanks, or click blank then click option'
A+
B/
C*
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition (+) or subtraction (-) instead of division
Using multiplication (*) which increases length
4fill in blank
hard

Fill the blank to create an extension property isPalindrome for String that checks if the string reads the same forwards and backwards.

Kotlin
val String.isPalindrome: Boolean
    get() = this [1] this.reversed()
Drag options to blanks, or click blank then click option'
A==
B!=
C&&
D||
Attempts:
3 left
💡 Hint
Common Mistakes
Using inequality (!=) which gives wrong result
Using logical AND (&&) or OR (||) which are not comparison operators
5fill in blank
hard

Fill all three blanks to create an extension property wordCount for String that returns the number of words separated by spaces.

Kotlin
val String.wordCount: Int
    get() = this.[1](" ").[2] [3] 0
Drag options to blanks, or click blank then click option'
Asplit
Bsize
C-
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using minus (-) instead of plus (+) which can reduce count incorrectly
Using wrong functions like join or length