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
✗ Incorrect
Without a ?, the type is non-nullable, so age cannot be null.
How do you declare a nullable string in Kotlin?
Avar name: ?String
Bvar name: String
Cvar name: String?
Dvar name: Nullable String
✗ Incorrect
Adding ? after the type makes it nullable.
What operator helps safely access a property of a nullable variable?
A?.
B!!
C::
D->
✗ 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?
AVariable becomes null
BVariable is ignored
CRuntime exception
DCompilation error
✗ Incorrect
Kotlin prevents this at compile time to avoid null pointer errors.
Which of these is a non-nullable type in Kotlin?
AInt?
BInt
CString?
Dnull
✗ 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.
Practice
(1/5)
1. In Kotlin, what happens if you declare a variable without a ? after its type?
easy
A. The variable cannot hold null values.
B. The variable can hold null values.
C. The variable is automatically initialized to null.
D. The variable must be declared as lateinit.
Solution
Step 1: Understand Kotlin's default type behavior
Kotlin types are non-nullable by default, meaning variables cannot hold null unless specified.
Step 2: Check the effect of missing ?
Without ?, the variable type is non-nullable, so it cannot hold null.
Final Answer:
The variable cannot hold null values. -> Option A
Quick Check:
Non-nullable type = no null allowed [OK]
Hint: No ? means no null allowed [OK]
Common Mistakes:
Thinking variables are nullable by default
Assuming variables initialize to null automatically
Confusing lateinit with nullability
2. Which of the following Kotlin variable declarations is correct to allow null values?
easy
A. val name: String? = null
B. val name: String?
C. val name: String = null
D. val name: String = "null"
Solution
Step 1: Identify nullable type syntax
Adding ? after the type allows the variable to hold null.
Step 2: Check initialization
Nullable variables must be initialized or declared with null explicitly, so val name: String? = null is correct.
Final Answer:
val name: String? = null -> Option A
Quick Check:
Nullable type with ? and initialized to null [OK]
Hint: Use ? and initialize nullable variables [OK]
Common Mistakes:
Declaring nullable without ?
Not initializing nullable variables
Assigning null to non-nullable types
3. What will be the output of this Kotlin code?
fun main() {
val text: String? = null
println(text?.length ?: "No text")
}
medium
A. 0
B. Compilation error
C. null
D. No text
Solution
Step 1: Understand safe call operator usage
The text?.length safely accesses length only if text is not null.
Step 2: Evaluate the Elvis operator
Since text is null, text?.length is null, so the Elvis operator ?: returns "No text".
Final Answer:
No text -> Option D
Quick Check:
Safe call + Elvis operator returns fallback [OK]
Hint: Safe call with Elvis returns fallback if null [OK]
Common Mistakes:
Expecting 0 instead of fallback string
Confusing safe call with normal call
Thinking code causes error due to null
4. Identify the error in this Kotlin code snippet:
fun main() {
val number: Int = null
println(number)
}
medium
A. Variable 'number' is not initialized.
B. Cannot assign null to a non-nullable type Int.
C. Missing semicolon after println statement.
D. Variable 'number' should be declared as var.
Solution
Step 1: Check variable type and assignment
The variable 'number' is declared as non-nullable Int but assigned null, which is not allowed.
Step 2: Understand Kotlin null safety rules
Non-nullable types cannot hold null, so this causes a compilation error.
Final Answer:
Cannot assign null to a non-nullable type Int. -> Option B
Quick Check:
Non-nullable types reject null assignments [OK]
Hint: Non-nullable types cannot be assigned null [OK]
Common Mistakes:
Thinking semicolon is required
Assuming var fixes null assignment
Ignoring Kotlin's null safety
5. You have a nullable variable val input: String?. You want to print its length only if it is not null, otherwise print "Empty input". Which Kotlin code correctly does this?
hard
A. println(input.length ?: "Empty input")
B. println(input!!.length)
C. println(if (input != null) input.length else "Empty input")
D. println(input?.length)
Solution
Step 1: Understand nullable variable usage
Since input can be null, we must check before accessing length.
Step 2: Evaluate each option
println(input.length ?: "Empty input") is invalid because length is Int, not nullable, so Elvis operator on it is wrong. println(if (input != null) input.length else "Empty input") safely checks null and prints length or fallback. println(input!!.length) uses !! which throws exception if null. println(input?.length) prints null if input is null, not the string "Empty input".