MCQs on Null Safety | Kotlin

Mastering Null Safety in Kotlin: 30 Essential MCQs
Dive into the critical concept of null safety in Kotlin, including nullable types, non-null types, Elvis operator, safe calls, and null assertions. Sharpen your skills with these practice questions!


MCQs on Null Safety in Kotlin

1. Nullable Types (?) and Non-Null Types

  1. Which of the following declarations represents a nullable type in Kotlin?
    a) val name: String
    b) val name: String?
    c) var name = null
    d) val name: ?String
  2. What will be the output of the following code?
    val name: String? = null println(name)
    a) null
    b) Error
    c) ""
    d) undefined
  3. How can you assign a nullable value to a non-nullable variable in Kotlin?
    a) Using !!
    b) Directly assigning
    c) Using as keyword
    d) It’s not allowed
  4. What is the default type for a variable in Kotlin if no type is explicitly declared?
    a) Nullable
    b) Non-null
    c) Inferred nullable
    d) Inferred non-null
  5. When declaring a variable, adding ? after the type implies:
    a) It is a nullable type
    b) It is a generic type
    c) It can hold any type
    d) It supports typecasting

2. Elvis Operator (?:)

  1. What is the primary purpose of the Elvis operator ?: in Kotlin?
    a) To provide a default value for nullable expressions
    b) To throw exceptions on null
    c) To combine two nullable variables
    d) None of the above
  2. What is the output of the following code?
    val name: String? = null val result = name ?: "Unknown" println(result)
    a) null
    b) Unknown
    c) Error
    d) ""
  3. Which of the following expressions demonstrates the correct use of the Elvis operator?
    a) val x = a ?: b
    b) val x = a ? b
    c) val x = a ?? b
    d) val x = a :? b
  4. How does the Elvis operator behave when the left-hand operand is not null?
    a) Returns the left-hand operand
    b) Returns the right-hand operand
    c) Throws an exception
    d) Assigns null
  5. If x is null and y is "default", what will val z = x ?: y return?
    a) null
    b) "default"
    c) Error
    d) undefined

3. Safe Calls and Null Assertions

  1. What does the safe call operator ?. do in Kotlin?
    a) Executes a block of code on non-null values
    b) Prevents null pointer exceptions
    c) Forces null values to be non-null
    d) Allows nullable types to hold non-null values
  2. What is the result of the following code?
    val length = str?.length
    a) Length of str or null
    b) Always throws an exception
    c) Null pointer exception if str is null
    d) Undefined behavior
  3. What will happen if !! operator is used on a null variable?
    a) Throws a NullPointerException
    b) Assigns a default value
    c) Silently ignores the null
    d) None of the above
  4. Which operator should be avoided to prevent runtime exceptions in Kotlin?
    a) !!
    b) ?.
    c) :
    d) =
  5. How does safe call chaining work in Kotlin?
    a) Stops at the first null value
    b) Executes all calls regardless of nulls
    c) Always returns null
    d) Combines all non-null values
  6. Can the null assertion operator !! be used with safe calls?
    a) No, they are mutually exclusive
    b) Yes, but with caution
    c) Yes, it forces nulls to non-null
    d) None of the above
  7. What is the correct way to safely access a nullable property?
    a) Using ?.
    b) Using !!
    c) Using as
    d) Using val
  8. What will user?.address?.city return if user or address is null?
    a) Throws an exception
    b) Returns null
    c) Returns "city"
    d) Undefined
  9. Which of these statements can lead to NullPointerException in Kotlin?
    a) Using !! on a null variable
    b) Using safe calls
    c) Using Elvis operator with non-null values
    d) Assigning null to a nullable type
  10. How do you avoid null pointer exceptions effectively in Kotlin?
    a) By using safe calls and Elvis operator
    b) By avoiding !! operator
    c) By using nullable types
    d) All of the above

Comprehensive

  1. Can a non-null type be explicitly assigned null?
    a) No
    b) Yes
    c) Only using !!
    d) None of the above
  2. What does the following code snippet do?
    val result = myString!!.toUpperCase() a) Converts myString to uppercase if not null
    b) Throws an exception if myString is null
    c) Converts to lowercase if null
    d) None of the above
  3. What is the purpose of combining ?. with ?:?
    a) To safely access and provide defaults
    b) To force null to non-null
    c) To assign types dynamically
    d) None of the above
  4. What will the code listOf(null)?.size ?: 0 return?
    a) 1
    b) 0
    c) null
    d) Throws an error
  5. When is a nullable type preferable over a non-null type?
    a) When dealing with optional values
    b) Always
    c) Never
    d) In primitive types only
  6. Can !! be applied to a non-nullable type?
    a) No, it’s redundant
    b) Yes, it forces a null check
    c) No, it throws an exception
    d) None of the above
  7. What is the output of this code? val x: String? = "Hello" println(x ?: "World") a) Hello
    b) World
    c) null
    d) Error
  8. Which operator is best suited for null-safe operations?
    a) ?.
    b) !!
    c) :
    d) =
  9. What is a potential drawback of using !!?
    a) Can cause NullPointerException
    b) Slows down code execution
    c) Ignores null values
    d) Makes types dynamic
  10. Which statement about null safety in Kotlin is correct?
    a) Null safety is enforced at compile-time
    b) Null safety is optional
    c) Null safety applies only to primitives
    d) None of the above

Answers Table

QnoAnswer (Option with Text)
1b) val name: String?
2a) null
3a) Using !!
4b) Non-null
5a) It is a nullable type
6a) To provide a default value
7b) Unknown
8a) val x = a ?: b
9a) Returns the left-hand operand
10b) "default"
11b) Prevents null pointer exceptions
12a) Length of str or null
13a) Throws a NullPointerException
14a) !!
15a) Stops at the first null value
16b) Yes, but with caution
17a) Using ?.
18b) Returns null
19a) Using !! on a null variable
20d) All of the above
21a) No
22b) Throws an exception if null
23a) To safely access and provide defaults
24a) 1
25a) When dealing with optional values
26a) No, it’s redundant
27a) Hello
28a) ?.
29a) Can cause NullPointerException
30a) Null safety is enforced at compile-time

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top