Sample questions
Question 1Easy
What is the output?
val a: Int? = null
println(a.toString() + "!")
- null!
- !
- NullPointerException
- Compile error
Answer: A. null!
Explanation
Any?.toString() is an extension that returns the string "null" for a null receiver.
Step-by-step solution
- a is null.
toString() on a nullable gives "null".- 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"
- val a: String = "x"
- val c: String = null
- val b: String? = null
- 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
- Non-null types (String) reject null.
- Nullable types (String?) accept null or a value.
- 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?
- Int
- Int?
- String?
- 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
s?.length runs only if s is not null.- If s is null the whole expression is null.
- 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?
- const var MAX = 100
- const val LIST = mutableListOf(1)
- const lateinit val X = 1
- 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
- const means a compile-time constant.
- So it must be
val, never var. - 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")
- A
- B
- AB
- 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
- x is null, so let is skipped.
- The whole safe call is null.
?: 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
- b becomes 5L automatically
- It compiles but gives a warning
- b becomes 0 as Int overflows
- 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
- Int and Long are different types.
- No implicit conversion exists.
- 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)
- 4
- 0
- null
- 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
- s is null.
- t = "none".
- "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)
- false
- true
- Compile error
- 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
- A is 1000, b is 1000, both are Int? with same value.
- == checks value equality, not reference.
- So a == b returns true.
- Println prints true.
- 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?
- token != null
- this::token.isInitialized
- token.isEmpty()
- 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
- lateinit has no null state.
- Reading before init throws.
::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?
- Any
- Unit
- Nothing?
- Null
Answer: C. Nothing?
Explanation
A bare null literal has type Nothing?, which is a subtype of every nullable type.
Step-by-step solution
- null carries no other type information.
- Its type is Nothing?.
- 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 →