Bird
0
0

Which of the following is a correct Kotlin syntax feature that improves safety compared to Java?

easy📝 Syntax Q12 of 15
Kotlin - Basics and JVM Runtime
Which of the following is a correct Kotlin syntax feature that improves safety compared to Java?
AUsing <code>var x: Int = null</code> to allow null values
BDeclaring variables without specifying type always
CUsing <code>val name: String?</code> to declare a nullable variable
DUsing <code>void</code> as a return type
Step-by-Step Solution
Solution:
  1. Step 1: Identify Kotlin's null safety syntax

    Kotlin uses ? after a type to mark it nullable, e.g., String?.
  2. Step 2: Check other options for correctness

    Using var x: Int = null to allow null values is wrong because Int cannot be assigned null without ?. Declaring variables without specifying type always is incorrect because Kotlin requires type inference or explicit types. Using void as a return type is Java syntax, not Kotlin.
  3. Final Answer:

    Using val name: String? to declare a nullable variable -> Option C
  4. Quick Check:

    Nullable types use ? in Kotlin [OK]
Quick Trick: Nullable types end with ? in Kotlin [OK]
Common Mistakes:
MISTAKES
  • Assigning null to non-nullable types
  • Confusing Kotlin and Java syntax
  • Using void instead of Unit in Kotlin

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes