Kotlin Null Safety u0026 Smart Casts Fundamentals Quiz Quiz

Explore essential concepts of Kotlin's null safety and smart casts with this introductory quiz. Strengthen your understanding of nullable types, safe calls, null checks, and smart type conversions in Kotlin for safer and more efficient code.

  1. Nullable Variable Declaration

    Which syntax correctly declares a nullable String variable named 'message' in Kotlin?

    1. var message String?
    2. var message: String! = null
    3. var message: String?
    4. String? message = null

    Explanation: In Kotlin, the correct way to declare a nullable variable uses the question mark after the type, as in 'var message: String?'. The other options are incorrect because they either have syntax errors, use an incorrect type declaration, or follow a syntax from another programming language. Option two misses the colon, option three is not valid Kotlin syntax for nullability, and option four follows a Java style not used in Kotlin.

  2. Safe Call Operator Usage

    What is the purpose of the safe call operator (?.) in Kotlin when working with a nullable variable, such as 'val name: String?'?

    1. It prevents code execution if the variable is null.
    2. It changes the nullability of the variable.
    3. It forces the variable to be non-null.
    4. It throws an exception if the variable is null.

    Explanation: The safe call operator (?.) allows you to safely access properties or methods of a nullable object; if the variable is null, the call returns null instead of throwing an exception. Option two is incorrect because the operator does not convert nullable types to non-null. Option three gets it wrong because no exception is thrown; rather, you avoid errors. Option four is false, as it does not change the variable's nullability.

  3. Elvis Operator Purpose

    In Kotlin, what does the Elvis operator (?:) do when used with nullable types like 'val value: String?'?

    1. It throws an exception if the value is null.
    2. It converts the variable to an integer.
    3. It provides a default value if the expression on the left is null.
    4. It removes nullability from the variable.

    Explanation: The Elvis operator (?:) returns the value on its left if it's not null, otherwise it returns the right-hand side value, acting as a default. It doesn't remove nullability or force a type conversion. Option three is incorrect because the Elvis operator does not throw an exception (unlike the not-null assertion). Option four introduces an unrelated concept.

  4. Not-Null Assertion Operator

    What is the effect of using the double exclamation mark (!!) on a nullable variable, such as 'input!!', in Kotlin?

    1. It throws a NullPointerException if the variable is null.
    2. It ignores type safety and continues the code.
    3. It converts the nullable to a non-null without any risk.
    4. It makes the variable always null.

    Explanation: The not-null assertion operator (!!) tells Kotlin to treat the variable as non-null but will cause a NullPointerException at runtime if the variable actually is null. It doesn't safely convert nullables nor ignores type safety. Making the variable always null or allowing the code to proceed unsafely is not the operator's function. The real risk is its possible exception.

  5. Smart Casts After Null Check

    After checking if a nullable String 'text' is not null, how does Kotlin treat the variable inside the 'if' block?

    1. As a nullable String?
    2. As an optional type
    3. As a non-null String
    4. As an integer

    Explanation: Once you've checked 'text != null', Kotlin smart-casts 'text' as a non-null String within that scope, so you don't need to use a safe call or check again. Option two is wrong because the variable isn't considered nullable inside the block. Option three is not relevant, as the variable's type doesn't change to integer. Option four uses terminology that doesn't apply in Kotlin.

  6. Default Value with Safe Call and Elvis Operator

    Which expression safely converts a nullable variable 'val num: Int?' to a non-null Int, using 10 as a default if it is null?

    1. val result = num ?: 10
    2. val result = num ? 10
    3. val result = num ?? 10
    4. val result = num !! 10

    Explanation: The correct way to provide a default value is using the Elvis operator: 'num ?: 10'. Option two is invalid syntax as '? 10' is not recognized. Option three misuses the not-null assertion, which throws instead of providing a default. Option four uses '??', which is not recognized in Kotlin for this purpose.

  7. Null Safety in Function Parameters

    Which Kotlin function declaration ensures that the 'email' parameter cannot be null?

    1. fun send(email: String)
    2. fun send(email: string)
    3. fun send(email: ?String)
    4. fun send(email: String?)

    Explanation: Declaring 'email' as 'String' means the parameter must be non-null. Using 'String?' (option two) would accept nulls. Option three is a syntax error as '?String' isn't valid, and option four is incorrect because 'string' (all lowercase) is not a recognized type in Kotlin.

  8. Safe Cast Operator Functionality

    What does the safe cast operator 'as?' do in Kotlin when applied to 'val obj: Any'?

    1. It returns null if the cast fails.
    2. It converts nulls to non-null types.
    3. It throws an exception on failure.
    4. It ignores type mismatch and continues.

    Explanation: The safe cast operator 'as?' attempts to cast a value to a type and returns null if the cast is not possible. Unlike a regular cast that throws an exception on failure, 'as?' is non-throwing. Option three is incorrect because it does not modify nullability. Option four is misleading, as type safety is still enforced, but errors are avoided with null results.

  9. String Length Property on Nullable Types

    Given 'val note: String?', how can you safely access the 'length' property without risking a NullPointerException?

    1. note::length
    2. note?.length
    3. note!length
    4. note.length

    Explanation: The safe call operator (?.) ensures you only access 'length' if 'note' is not null, preventing errors. Option two is unsafe and can throw a NullPointerException if 'note' is null. Option three uses invalid syntax, and option four refers to a function reference, which is not appropriate here.

  10. Smart Casts After Type Check

    If you check that a variable 'data: Any' is a String using 'is String' in an 'if' statement, how does Kotlin treat 'data' inside that block?

    1. As an integer
    2. As an Any
    3. As a nullable String?
    4. As a String

    Explanation: Once the 'is String' check passes, Kotlin smart-casts 'data' to a String within the 'if' block. Option two is incorrect because it remains as Any only outside the block. Option three is wrong because the nullability doesn't change; it's smart-cast to String, not String?. Option four is unrelated to the type being checked.