Recall & Review
beginner
What does it mean that Kotlin has non-nullable types by default?
In Kotlin, variables cannot hold
null unless explicitly declared. This helps prevent errors caused by null values, making programs safer.Click to reveal answer
beginner
How do you declare a variable that can hold
null in Kotlin?You add a question mark
? after the type. For example, var name: String? means name can be a string or null.Click to reveal answer
beginner
What happens if you try to assign
null to a non-nullable variable in Kotlin?The code will not compile. Kotlin forces you to handle nulls explicitly to avoid runtime errors.
Click to reveal answer
beginner
Explain the difference between
String and String? in Kotlin.String cannot be null. String? can be null. You must check for null before using String? safely.Click to reveal answer
intermediate
What Kotlin feature helps you safely use nullable variables?
The safe call operator
?. lets you access properties or methods only if the variable is not null. Otherwise, it returns null.Click to reveal answer
In Kotlin, what does
var age: Int mean?✗ Incorrect
Without a ?, the type is non-nullable, so age cannot be null.
How do you declare a nullable string in Kotlin?
✗ Incorrect
Adding ? after the type makes it nullable.
What operator helps safely access a property of a nullable variable?
✗ Incorrect
The safe call operator ?. returns null if the variable is null, avoiding errors.
What happens if you assign null to a non-nullable variable?
✗ Incorrect
Kotlin prevents this at compile time to avoid null pointer errors.
Which of these is a non-nullable type in Kotlin?
✗ Incorrect
Int is non-nullable, while Int? can hold null.
Explain why Kotlin uses non-nullable types by default and how it helps programmers.
Think about how null values cause errors and how Kotlin stops that.
You got /4 concepts.
Describe how to safely work with a nullable variable in Kotlin.
Remember the special operator that helps avoid null problems.
You got /4 concepts.