0
0
Kotlinprogramming~5 mins

Nullable types with ? suffix in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the ? suffix mean in Kotlin types?
It means the variable can hold either a value of that type or null. For example, String? means a variable can be a string or null.
Click to reveal answer
beginner
How do you declare a nullable integer variable in Kotlin?
You add a ? after the type. For example: var number: Int? = null means number can be an integer or null.
Click to reveal answer
intermediate
Why does Kotlin use nullable types instead of allowing null everywhere?
To prevent errors caused by null values, called NullPointerExceptions. Nullable types force you to check for null before using the value.
Click to reveal answer
beginner
What happens if you try to assign null to a non-nullable type in Kotlin?
The code will not compile. Kotlin requires you to explicitly mark a type as nullable with ? to allow null values.
Click to reveal answer
intermediate
How do you safely access a property or method of a nullable variable in Kotlin?
Use the safe call operator ?.. For example, name?.length returns the length if name is not null, otherwise it returns null.
Click to reveal answer
What does String? mean in Kotlin?
AA non-nullable string
BA string that cannot be null
CA nullable integer
DA string that can also be null
Which operator safely accesses a property of a nullable variable?
A?.
B!!
C:
D?:
What happens if you assign null to a non-nullable type?
ACode compiles and runs
BCode compiles but throws error at runtime
CCode does not compile
DCode ignores the null
How do you declare a nullable Boolean variable named flag?
Avar flag: Boolean = null
Bvar flag: Boolean? = null
Cvar flag: Boolean?
Dvar flag: Boolean
Why does Kotlin use nullable types?
ATo prevent NullPointerExceptions by forcing null checks
BTo allow any variable to be null without checks
CTo make code longer
DTo allow nulls everywhere
Explain what nullable types are in Kotlin and how the ? suffix works.
Think about how Kotlin prevents null errors.
You got /3 concepts.
    Describe how to safely use a nullable variable's property or method in Kotlin.
    How do you avoid crashes when a variable might be null?
    You got /3 concepts.