Variables & Null Safety Questions with Answers

23 practice questions on Variables & Null Safety (Kotlin) with answers and step-by-step solutions. Try 10 samples below, then practise all 23 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

val a: Int? = null
println(a.toString() + "!")
  1. null!
  2. !
  3. NullPointerException
  4. Compile error

Answer: A. null!

Explanation

Any?.toString() is an extension that returns the string "null" for a null receiver.

Step-by-step solution

  1. a is null.
  2. toString() on a nullable gives "null".
  3. Concatenating gives "null!".

Why the other options are wrong

  • B. toString() on a null Int? returns the text "null", not an empty string.
  • C. Any?.toString() is defined for nullable receivers and never throws.
  • D. toString() exists as an extension on Any?.

Tip: String templates also print null as "null".

Question 2Easy

Which line below does NOT compile?

val a: String = "x"
val b: String? = null
val c: String = null
val d: String? = "y"
  1. val a: String = "x"
  2. val c: String = null
  3. val b: String? = null
  4. val d: String? = "y"

Answer: B. val c: String = null

Explanation

A type without ? cannot hold null, so assigning null to String is a compile-time error.

Step-by-step solution

  1. Non-null types (String) reject null.
  2. Nullable types (String?) accept null or a value.
  3. Only val c mixes a non-null type with null.

Why the other options are wrong

  • A. Assigning a string literal to a non-null String is fine.
  • C. A nullable type may hold null.
  • D. A nullable type may also hold a normal value.

Tip: Kotlin catches null mistakes at compile time, not at runtime.

Question 3Easy

If s is String?, what is the type of val n = s?.length?

  1. Int
  2. Int?
  3. String?
  4. Nothing

Answer: B. Int?

Explanation

A safe call returns null when the receiver is null, so the result type becomes the nullable Int?.

Step-by-step solution

  1. s?.length runs only if s is not null.
  2. If s is null the whole expression is null.
  3. So the type is Int?, not Int.

Why the other options are wrong

  • A. The safe call yields null when s is null, so the result must be nullable.
  • C. length is an Int, not a String.
  • D. Nothing is a type that never has a value; not the case here.

Tip: Safe call always widens the result type to nullable.

Question 4Easy

Which of these is valid at the top level of a Kotlin file?

  1. const var MAX = 100
  2. const val LIST = mutableListOf(1)
  3. const lateinit val X = 1
  4. const val MAX = 100

Answer: D. const val MAX = 100

Explanation

const val must be a val of primitive or String type whose value is known at compile time.

Step-by-step solution

  1. const means a compile-time constant.
  2. So it must be val, never var.
  3. Only primitives and Strings qualify.

Why the other options are wrong

  • A. const must be val; a constant cannot be var.
  • B. const needs a compile-time primitive or String value.
  • C. const and lateinit cannot combine; not valid.

Question 5Medium

What is the output?

val x: String? = null
x?.let { println("A") } ?: println("B")
  1. A
  2. B
  3. AB
  4. Nothing is printed

Answer: B. B

Explanation

x?.let{} is null because x is null, so the Elvis right-hand side prints B.

Step-by-step solution

  1. x is null, so let is skipped.
  2. The whole safe call is null.
  3. ?: runs println("B").

Why the other options are wrong

  • A. let is skipped as x is null.
  • C. Only one branch runs here.
  • D. The Elvis side runs println("B").

Tip: Beware: if the let block returns null, Elvis also runs.

Question 6Medium

What is true about this code?

val a: Int = 5
val b: Long = a
  1. b becomes 5L automatically
  2. It compiles but gives a warning
  3. b becomes 0 as Int overflows
  4. The second line is a compile error; use a.toLong()

Answer: D. The second line is a compile error; use a.toLong()

Explanation

Kotlin does not widen numbers implicitly. Convert explicitly with a.toLong().

Step-by-step solution

  1. Int and Long are different types.
  2. No implicit conversion exists.
  3. Write val b: Long = a.toLong().

Why the other options are wrong

  • A. Kotlin has no implicit widening conversions.
  • B. It is an error, not a warning.
  • C. No overflow occurs; the code is rejected first.

Tip: Java widens automatically; Kotlin does not.

Question 7Medium

What is the output?

val s: String? = null
val t = s ?: "none"
println(t.length)
  1. 4
  2. 0
  3. null
  4. Compile error

Answer: A. 4

Explanation

s is null, so t is "none", whose length is 4. The Elvis expression has type String, not String?.

Step-by-step solution

  1. s is null.
  2. t = "none".
  3. "none".length = 4.

Why the other options are wrong

  • B. The fallback "none" is used, not an empty string.
  • C. t is a non-null String after Elvis.
  • D. t has type String, so .length is fine.

Question 8Medium

What is the output?

val a: Int? = 1000
val b: Int? = 1000
println(a == b)
  1. false
  2. true
  3. Compile error
  4. Depends on the JVM cache

Answer: B. true

Explanation

== calls equals, so it compares values and prints true regardless of boxing.

Step-by-step solution

  1. A is 1000, b is 1000, both are Int? with same value.
  2. == checks value equality, not reference.
  3. So a == b returns true.
  4. Println prints true.
  5. Final answer: true.

Why the other options are wrong

  • A. Boxed Int? values use equals for ==, so the equal values 1000 give true.
  • C. The code compiles, since Int? values can be compared with == without problems.
  • D. == on Int? uses equals, not reference identity, so no JVM cache affects the result.

Tip: For nullable types, == checks value equality.

Question 9Hard

Inside a class, how do you check whether a lateinit var token: String has been initialised?

  1. token != null
  2. this::token.isInitialized
  3. token.isEmpty()
  4. token is String

Answer: B. this::token.isInitialized

Explanation

Use the property reference this::token.isInitialized, available where the property is accessible.

Step-by-step solution

  1. lateinit has no null state.
  2. Reading before init throws.
  3. ::token.isInitialized checks safely.

Why the other options are wrong

  • A. The type is non-null String so this check does not detect initialisation.
  • C. Accessing it uninitialised throws UninitializedPropertyAccessException.
  • D. It is always true statically and does not detect initialisation.

Question 10Hard

What is the type of val x = null?

  1. Any
  2. Unit
  3. Nothing?
  4. Null

Answer: C. Nothing?

Explanation

A bare null literal has type Nothing?, which is a subtype of every nullable type.

Step-by-step solution

  1. null carries no other type information.
  2. Its type is Nothing?.
  3. It can be assigned to any T?.

Why the other options are wrong

  • A. Any does not accept null.
  • B. Unit is a single-valued type, not null.
  • D. There is no type named Null in Kotlin.

Practise all 23 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Kotlin topics · All topics