Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using val instead of a string value to assign.
Trying to assign null without making the variable nullable.
✗ Incorrect
We use var to declare a variable that can change. Assigning "Alice" updates the value.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using var instead of val for constants.
Assigning null without nullable type.
✗ Incorrect
val declares a constant. Assigning "Hello" sets the constant string value.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to assign null to a non-nullable type.
Using var keyword inside the assignment.
✗ Incorrect
Adding ? after Int makes the variable nullable, so it can hold null.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Declaring variable as non-nullable String and assigning null.
Assigning a string value instead of null.
✗ Incorrect
String? makes city nullable. Assigning null is allowed then.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using !! which throws error if null.
Not providing default value for null case.
✗ Incorrect
The safe call operator ? allows access if not null, .length gets length, ?: 0 provides default if null.