0
0
Kotlinprogramming~5 mins

Non-nullable types by default in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Aage cannot be null
Bage can be null
Cage is a nullable integer
Dage is a string
How do you declare a nullable string in Kotlin?
Avar name: ?String
Bvar name: String
Cvar name: String?
Dvar name: Nullable String
What operator helps safely access a property of a nullable variable?
A?.
B!!
C::
D->
What happens if you assign null to a non-nullable variable?
AVariable becomes null
BVariable is ignored
CRuntime exception
DCompilation error
Which of these is a non-nullable type in Kotlin?
AInt?
BInt
CString?
Dnull
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.