0
0
Android Kotlinmobile~10 mins

Variables (val, var) and null safety in Android 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 a variable that can be changed later.

Android Kotlin
var name: String = "John"
name = [1]
Drag options to blanks, or click blank then click option'
A"Alice"
Bval
Cvar
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using val instead of a string value to assign.
Trying to assign null without making the variable nullable.
2fill in blank
medium

Complete the code to declare a constant string that cannot be changed.

Android Kotlin
val greeting: String = [1]
Drag options to blanks, or click blank then click option'
Anull
Bval
Cvar
D"Hello"
Attempts:
3 left
💡 Hint
Common Mistakes
Using var instead of val for constants.
Assigning null without nullable type.
3fill in blank
hard

Fix the error in the code by making the variable nullable.

Android Kotlin
var age: Int = [1]
age = null
Drag options to blanks, or click blank then click option'
AInt?
B0
Cnull
Dvar
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to assign null to a non-nullable type.
Using var keyword inside the assignment.
4fill in blank
hard

Fill both blanks to declare a nullable variable and assign null to it.

Android Kotlin
var city: [1] = [2]
Drag options to blanks, or click blank then click option'
AString?
Bnull
CString
D"Paris"
Attempts:
3 left
💡 Hint
Common Mistakes
Declaring variable as non-nullable String and assigning null.
Assigning a string value instead of null.
5fill in blank
hard

Fill all three blanks to safely print the length of a nullable string.

Android Kotlin
val length = name[1][2][3]
Drag options to blanks, or click blank then click option'
A?
B.length
C?: 0
D!!
Attempts:
3 left
💡 Hint
Common Mistakes
Using !! which throws error if null.
Not providing default value for null case.