Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using division (/) instead of modulus (%)
Using multiplication (*) or addition (+) operators
✗ Incorrect
The modulus operator (%) returns the remainder. If the remainder when divided by 2 is 0, the number is even.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 0 which returns the first character
Using length as an index which is out of bounds
✗ Incorrect
The
last() function returns the last character of the string.3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition (+) or subtraction (-) instead of division
Using multiplication (*) which increases length
✗ Incorrect
To get the first half, divide the length by 2 to get the midpoint index.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using inequality (!=) which gives wrong result
Using logical AND (&&) or OR (||) which are not comparison operators
✗ Incorrect
To check palindrome, compare the string with its reversed version using equality (==).
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using minus (-) instead of plus (+) which can reduce count incorrectly
Using wrong functions like join or length
✗ Incorrect
Split the string by spaces, get the size of the resulting list, and add 0 (no change) to get the count.