0
0
Kotlinprogramming~10 mins

Why Kotlin over Java - Test Your Understanding

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

Complete the code to declare a Kotlin variable with type inference.

Kotlin
val name = [1]
Drag options to blanks, or click blank then click option'
A"John"
Bvar
CString
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'var' instead of a value
Using type name instead of value
Using 'val' again instead of a value
2fill in blank
medium

Complete the code to create a Kotlin function that returns the length of a string.

Kotlin
fun lengthOfString(str: String): Int = str.[1]
Drag options to blanks, or click blank then click option'
Asize
Bcount
Clength()
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using length() with parentheses (Java style)
Using size which is for collections
Using count which requires a predicate
3fill in blank
hard

Fix the error in the Kotlin code to safely access the length of a nullable string.

Kotlin
val length = str[1]length ?: 0
Drag options to blanks, or click blank then click option'
A?.
B?
C!!
D:
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!!' which throws an exception if null
Using '?' alone which is incomplete
Using ':' which is for Elvis operator
4fill in blank
hard

Fill both blanks to create a Kotlin data class with two properties.

Kotlin
data class User(val name: [1], val age: [2])
Drag options to blanks, or click blank then click option'
AString
BInt
CDouble
DBoolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using Double for age
Using Boolean for name
Mixing types incorrectly
5fill in blank
hard

Fill all three blanks to create a Kotlin map comprehension filtering entries with values greater than 10.

Kotlin
val filtered = mapOf("a" to 5, "b" to 15, "c" to 20).filter { it.[1] [2] [3] }
Drag options to blanks, or click blank then click option'
Avalue
B>
C10
Dkey
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'key' instead of 'value'
Using '<' instead of '>'
Using a wrong number