0
0
Android Kotlinmobile~5 mins

Variables (val, var) and null safety in Android Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the difference between val and var in Kotlin?

val declares a read-only variable (like a constant). You cannot change its value after assignment.<br>var declares a mutable variable, so you can change its value later.

Click to reveal answer
beginner
What does null safety mean in Kotlin?

Null safety means Kotlin helps you avoid errors caused by null values by distinguishing nullable and non-nullable types at compile time.

Click to reveal answer
beginner
How do you declare a variable that can hold null in Kotlin?

Use 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?

Kotlin will show a compile-time error. Non-nullable variables cannot hold null values.

Click to reveal answer
intermediate
How can you safely access a nullable variable's property or method?

Use the safe call operator ?.. It only calls the property or method if the variable is not null, otherwise returns null.

Click to reveal answer
Which keyword declares a variable that cannot be reassigned in Kotlin?
Aval
Bvar
Cconst
Dlet
How do you declare a nullable String variable named email?
Aval email: String?
Bvar email: String
Cval email: String
Dvar email: String?
What will happen if you assign null to a non-nullable variable?
AIt compiles and works fine
BCompile-time error
CIt compiles but crashes at runtime
DWarning but compiles
Which operator safely accesses a property of a nullable variable?
A?.
B->
C:
D!!
What does the !! operator do in Kotlin?
ADeclares a variable as nullable
BSafely accesses a nullable variable
CThrows an exception if the value is null
DAssigns a default value
Explain the difference between val and var in Kotlin and when to use each.
Think about constants versus variables in everyday life.
You got /4 concepts.
    Describe how Kotlin's null safety helps prevent app crashes and how you declare nullable variables.
    Imagine avoiding a null pointer crash by checking if something is null first.
    You got /4 concepts.