Kotlin Null Safety Quiz

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By ProProfs AI
P
ProProfs AI
Community Contributor
Quizzes Created: 81 | Total Attempts: 817
| Questions: 15 | Updated: Apr 30, 2026
Please wait...
Question 1 / 16
🏆 Rank #--
0 %
0/100
Score 0/100

1. What does the safe call operator (?.) do in Kotlin?

Explanation

The safe call operator (?.) in Kotlin allows developers to safely access properties or methods of an object that might be null. When used, it checks if the object is null before proceeding with the method call, preventing potential NullPointerExceptions and ensuring that the code executes smoothly when dealing with nullable types.

Submit
Please wait...
About This Quiz
Kotlin Null Safety Quiz - Quiz

Master Kotlin's null safety features with this college-level quiz. The Kotlin Null Safety Quiz covers nullable and non-nullable types, safe calls, the Elvis operator, and null checks. Understand how Kotlin's type system prevents NullPointerExceptions and learn best practices for handling null values in production code.

2.

What first name or nickname would you like us to use?

You may optionally provide this to label your report, leaderboard, or certificate.

2. What is the result of executing `val x: String? = null; x?.length` in Kotlin?

Explanation

In Kotlin, the safe call operator `?.` allows you to access properties or methods of a nullable type without risking a `NullPointerException`. When `x` is null, `x?.length` evaluates to null instead of throwing an exception, resulting in the expression returning null.

Submit

3. The Elvis operator (?:) in Kotlin provides a default value when the left side is ____.

Explanation

The Elvis operator (?:) in Kotlin is used to handle nullability by providing a concise way to return a default value when the expression on the left side evaluates to null. This operator enhances code readability and safety by allowing developers to avoid explicit null checks.

Submit

4. Which statement correctly uses the Elvis operator?

Explanation

The Elvis operator (?:) is used in Kotlin to provide a default value when the expression on the left is null. In the statement "val name = person.name ?: 'Unknown'", if `person.name` is null, it assigns "Unknown" to `name`, ensuring that a non-null value is always given.

Submit

5. What does the not-null assertion operator (!!) do?

Explanation

The not-null assertion operator (!!) is used in programming languages like Kotlin to assert that a value is non-null. When applied to a nullable type, it converts it to a non-nullable type. If the value is null, it will throw an exception, ensuring that the code does not operate on null values, which can lead to runtime errors.

Submit

6. True or False: Using !! on a nullable variable that contains null will cause a runtime exception.

Explanation

Using !! on a nullable variable that contains null will indeed cause a runtime exception. The !! operator is designed to assert that a value is non-null, and when it encounters a null value, it throws a NullPointerException. This behavior highlights the importance of handling nullable types carefully in programming.

Submit

7. What is smart casting in Kotlin's null safety context?

Explanation

Smart casting in Kotlin allows the compiler to automatically recognize a variable as non-nullable after a null check has been performed. This eliminates the need for explicit casting, streamlining code and enhancing safety by ensuring that developers do not inadvertently use a nullable type after confirming it is non-null.

Submit

8. After the code `if (name != null) { println(name.length) }`, what type is `name` inside the if block?

Explanation

Inside the if block, the condition `name != null` ensures that `name` cannot be null. Therefore, within this block, the type of `name` is treated as non-nullable, allowing methods like `length` to be safely called without the risk of a null reference exception.

Submit

9. Which operator is used for safe type casting with null handling?

Explanation

The "as?" operator in programming languages like Swift is used for safe type casting, allowing the conversion of an object to a specified type while handling potential null values. If the cast fails, it returns nil instead of throwing an error, ensuring safer code execution and reducing the risk of runtime crashes.

Submit

10. What does `val x = person?.address?.city?.name` do if any intermediate property is null?

Explanation

The expression `val x = person?.address?.city?.name` uses safe call operators (`?.`) to access nested properties. If any property in the chain (`person`, `address`, or `city`) is null, the entire expression evaluates to null instead of throwing an exception, ensuring safe access without runtime errors.

Submit

11. True or False: Kotlin's null safety is enforced only at runtime, not at compile time.

Submit

12. The `let` function in Kotlin is commonly used with the safe call operator to ____.

Submit

13. Which approach best prevents NullPointerExceptions in Kotlin?

Submit

14. In Kotlin, what is the difference between a non-nullable type and a nullable type?

Explanation

In Kotlin, non-nullable types are designed to prevent null references, ensuring that a variable always holds a valid value. In contrast, nullable types are explicitly defined to allow null values, enabling developers to handle cases where a variable may not have a valid object. This distinction helps reduce null-related errors in the code.

Submit

15. Which syntax declares a nullable String in Kotlin?

Explanation

In Kotlin, appending a question mark (`?`) to a type indicates that the variable can hold either a value of that type or `null`. Thus, `String? name;` correctly declares a nullable String variable, allowing it to be assigned a String value or remain null.

Submit
×
Saved
Thank you for your feedback!
View My Results
Cancel
  • All
    All (15)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
What does the safe call operator (?.) do in Kotlin?
What is the result of executing `val x: String? = null; x?.length` in...
The Elvis operator (?:) in Kotlin provides a default value when the...
Which statement correctly uses the Elvis operator?
What does the not-null assertion operator (!!) do?
True or False: Using !! on a nullable variable that contains null will...
What is smart casting in Kotlin's null safety context?
After the code `if (name != null) { println(name.length) }`, what type...
Which operator is used for safe type casting with null handling?
What does `val x = person?.address?.city?.name` do if any intermediate...
True or False: Kotlin's null safety is enforced only at runtime, not...
The `let` function in Kotlin is commonly used with the safe call...
Which approach best prevents NullPointerExceptions in Kotlin?
In Kotlin, what is the difference between a non-nullable type and a...
Which syntax declares a nullable String in Kotlin?
play-Mute sad happy unanswered_answer up-hover down-hover success oval cancel Check box square blue
Alert!