Null safety is a feature in Kotlin that helps prevent errors caused by null references by distinguishing nullable and non-nullable types at compile time.
Click to reveal answer
beginner
How does Kotlin differentiate between nullable and non-nullable types?
Kotlin uses a question mark (?) after a type to mark it as nullable (e.g., String?), while types without it (e.g., String) cannot hold null values.
Click to reveal answer
intermediate
Why is null safety considered Kotlin's defining feature?
Because it eliminates the common source of runtime errors called NullPointerExceptions by catching potential null issues during compilation, making apps safer and more reliable.
Click to reveal answer
beginner
What happens if you try to assign null to a non-nullable type in Kotlin?
The Kotlin compiler will show an error and prevent the code from compiling, forcing you to handle nulls explicitly.
Click to reveal answer
intermediate
Name two Kotlin operators that help safely work with nullable types.
The safe call operator (?.) allows accessing properties or methods only if the object is not null, and the Elvis operator (?:) provides a default value if the expression is null.
Click to reveal answer
What symbol does Kotlin use to mark a type as nullable?
A!
B?
C*
D#
✗ Incorrect
Kotlin uses the question mark (?) after a type to indicate it can hold null values.
What error does Kotlin's null safety help prevent?
ATypeMismatchException
BSyntaxError
CIndexOutOfBoundsException
DNullPointerException
✗ Incorrect
Null safety helps prevent NullPointerExceptions by catching null issues at compile time.
What will happen if you assign null to a non-nullable variable in Kotlin?
AVariable becomes null
BRuntime exception
CCompilation error
DCode runs normally
✗ Incorrect
Kotlin's compiler prevents assigning null to non-nullable types, causing a compilation error.
Which operator safely accesses a property or method of a nullable object?
A?.
B!!
C?:
D::
✗ Incorrect
The safe call operator (?.) accesses members only if the object is not null.
What does the Elvis operator (?:) do in Kotlin?
AProvides a default value if the expression is null
BThrows an exception if null
CConverts null to zero
DChecks if a value is null
✗ Incorrect
The Elvis operator returns the left expression if not null, otherwise the right default value.
Explain why null safety is important in Kotlin and how it improves code reliability.
Think about how null errors cause crashes and how Kotlin stops them early.
You got /4 concepts.
Describe how Kotlin's type system enforces null safety with examples.
Consider how Kotlin marks types and operators to handle null values safely.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of Kotlin's null safety feature?
easy
A. To allow all variables to be null by default
B. To prevent errors caused by accessing null values
C. To make Kotlin code run faster
D. To remove the need for variable declarations
Solution
Step 1: Understand null safety concept
Kotlin's null safety is designed to avoid null pointer exceptions by making null handling explicit.
Step 2: Identify the main purpose
The main goal is to prevent errors caused by accessing null values, which are common bugs in many languages.
Final Answer:
To prevent errors caused by accessing null values -> Option B
Quick Check:
Null safety = prevent null errors [OK]
Hint: Null safety means avoiding null errors in code [OK]
Common Mistakes:
Thinking null safety allows all variables to be null
Believing it improves performance directly
Confusing null safety with removing declarations
2. Which of the following is the correct way to declare a nullable variable in Kotlin?
easy
A. val name: String? = null
B. val name: String = null
C. val name: String! = null
D. val name: String = ?null
Solution
Step 1: Recall Kotlin nullable syntax
In Kotlin, adding a question mark after the type makes it nullable, allowing it to hold null.
Step 2: Check each option
val name: String? = null uses String? which is correct. Others are invalid syntax or do not allow null.
Final Answer:
val name: String? = null -> Option A
Quick Check:
Nullable type uses ? after type [OK]
Hint: Use ? after type to allow nulls in Kotlin [OK]
Common Mistakes:
Omitting the ? for nullable types
Using invalid symbols like ! or ?null
Assigning null to non-nullable types
3. What will be the output of this Kotlin code?
val name: String? = null
println(name?.length ?: "No name")
medium
A. Throws NullPointerException
B. 0
C. null
D. No name
Solution
Step 1: Understand safe call and Elvis operator
The safe call ?. returns null if name is null. The Elvis operator ?: provides a default value if the left side is null.
Step 2: Evaluate the expression
Since name is null, name?.length is null, so the expression returns "No name".
4. Identify the error in this Kotlin code snippet:
val length = name.length
val name: String? = null
medium
A. Variable 'name' is used before declaration
B. No error, code runs fine
C. Missing semicolon after declaration
D. Cannot access length on nullable variable without safe call
Solution
Step 1: Analyze variable usage order
The variable name is used before it is declared.
Step 2: Identify the compile error
Kotlin requires variables to be declared before use, causing an unresolved reference error.
Final Answer:
Variable 'name' is used before declaration -> Option A
Quick Check:
Declare before use [OK]
Hint: Declare variables before using them in Kotlin [OK]
Common Mistakes:
Thinking it's only a null safety issue
Believing the code runs fine
Assuming Kotlin requires semicolons
5. How can you safely call a function printLength that prints the length of a nullable String text without causing a NullPointerException?
hard
A. fun printLength(text: String) { println(text.length) }
printLength(null)
B. fun printLength(text: String?) { println(text.length) }
printLength(null)
C. fun printLength(text: String?) { println(text?.length ?: 0) }
printLength(null)
D. fun printLength(text: String) { println(text?.length) }
printLength(null)
Solution
Step 1: Understand function parameter nullability
To accept null, parameter must be String?. Options A and D use non-nullable String, so passing null causes error.
Step 2: Use safe call and default value
fun printLength(text: String?) { println(text?.length ?: 0) }
printLength(null) uses safe call ?. and Elvis operator ?: to print 0 if text is null, avoiding exceptions.
Final Answer:
fun printLength(text: String?) { println(text?.length ?: 0) }
printLength(null) -> Option C
Quick Check:
Safe call + Elvis handles null safely [OK]
Hint: Use ?. and ?: to safely handle nullable parameters [OK]
Common Mistakes:
Passing null to non-nullable parameter
Accessing length without safe call
Ignoring default value for null case
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.