Sample questions
Question 1Easy
Which is true about listOf(1, 2, 3) in Kotlin?
- It is always empty
- You can call
add() on it - It returns an
Array - It is read-only
Answer: D. It is read-only
Explanation
Use mutableListOf for a list you can modify.
Step-by-step solution
- listOf returns a List, Kotlin's read-only interface.
- List has no add or remove.
- Use mutableListOf for a list you can change.
Why the other options are wrong
- A. It contains 1, 2 and 3.
- B. add() is not on the List interface.
- C. listOf returns a List, not an Array.
Question 2Easy
What does this print?
println(listOf(1, 2, 3).map { it * 2 })
- [1, 2, 3, 1, 2, 3]
- [2, 4, 6]
- [1, 4, 9]
- 12
Answer: B. [2, 4, 6]
Explanation
map transforms each element; it is the implicit lambda parameter.
Step-by-step solution
- map applies the lambda to every element.
- it * 2 doubles each: 1 -> 2, 2 -> 4, 3 -> 6.
- Result is a new list [2, 4, 6].
Why the other options are wrong
- A. map transforms elements; it does not repeat the list.
- C. [1, 4, 9] would be squaring.
- D. 12 would be a sum of the doubled values; map returns a list.
Question 3Easy
What does this print?
data class Point(val x: Int, val y: Int)
println(Point(1, 2))
- {x=1, y=2}
- (1, 2)
- Point@1b6d3586
- Point(x=1, y=2)
Answer: D. Point(x=1, y=2)
Explanation
Data classes generate a readable toString.
Step-by-step solution
- Data classes generate
toString() automatically. - The format is
ClassName(prop=value, ...). - So it prints
Point(x=1, y=2).
Why the other options are wrong
- A. That brace format is how Kotlin prints maps.
- B. Tuple-style output is not Kotlin's format.
- C. ClassName@hash is the default toString of a normal class.
Question 4Easy
What does this print?
val s: String? = null
println(s?.length)
- Compile error
- NullPointerException
- null
- 0
Answer: C. null
Explanation
The safe-call operator ?. returns null instead of throwing when the receiver is null.
Step-by-step solution
- s is null.
- ?. is the safe call: if the receiver is null, the whole expression is null.
- println(null) prints null.
Why the other options are wrong
- A. Safe calls on nullable types compile fine.
- B. Safe calls avoid the exception.
- D. No default of 0 is applied without ?:.
Question 5Medium
What does this print?
fun greet(name: String = "Guest", times: Int = 1) =
"Hi $name ".repeat(times).trim()
println(greet(times = 2))
- Hi 2 Hi 2
- Hi Guest Hi Guest
- Compile error: missing name
- Hi Guest
Answer: B. Hi Guest Hi Guest
Explanation
Named arguments let you skip earlier parameters that have defaults.
Step-by-step solution
- Named argument
times = 2; name uses its default "Guest". "Hi Guest ".repeat(2) = "Hi Guest Hi Guest ".trim() removes the trailing space: Hi Guest Hi Guest.
Why the other options are wrong
- A. times is named, so 2 is not taken as name.
- C. name has a default, so it may be omitted.
- D. repeat(2) repeats the greeting.
Question 6Medium
What does this print?
val name = "Kotlin"
println("$name has ${name.length} letters")
- $name has ${name.length} letters
- Kotlin has 6 letters
- name has 6 letters
- Kotlin has name.length letters
Answer: B. Kotlin has 6 letters
Explanation
String templates insert values with $ and expressions with ${}.
Step-by-step solution
- $name inserts the value of name: Kotlin.
- ${name.length} evaluates the expression: 6.
- Output: Kotlin has 6 letters.
Why the other options are wrong
- A. Double-quoted strings are templates, so the values are inserted.
- C. $name inserts the value, not the word 'name'.
- D. Expressions inside ${} are evaluated.
Question 7Medium
A companion object in a Kotlin class is used to:
- Make the class a singleton
- Create a second instance of the class
- Hold class-level members
- Declare a data class
Answer: C. Hold class-level members
Explanation
Members of the companion object are accessed through the class name.
Step-by-step solution
- Kotlin has no static keyword.
- Members inside companion object belong to the class, not instances.
- Call them as ClassName.member, e.g. factory methods or constants.
Why the other options are wrong
- A. object declarations create singletons.
- B. A companion does not create instances of the class.
- D. Data classes use data class.
Question 8Medium
What does this print?
data class Point(val x: Int, val y: Int)
val a = Point(1, 2)
val b = Point(1, 2)
println("${a == b} ${a === b}")
- false true
- false false
- true false
- true true
Answer: C. true false
Explanation
== uses the generated equals (structural); === compares references.
Step-by-step solution
- == calls equals(); data classes compare property values.
- Both have x = 1, y = 2, so a == b is true.
- === checks identity; they are two separate objects, so false.
Why the other options are wrong
- A. This reverses the two results.
- B. Data-class equality is by value, so == is true.
- D. Two separate constructor calls make two different objects.
Tip: == same content; === same object.
Question 9Hard
Why is a sealed class useful with a when expression?
- The compiler checks all subclasses are handled
- It removes the need for else in all whens on any type
- It makes when run faster
- It allows any class to extend it
Answer: A. The compiler checks all subclasses are handled
Explanation
Sealed hierarchies are known at compile time, so when can be exhaustive without else, e.g. for UiState Loading/Success/Error.
Step-by-step solution
- A sealed class has a fixed set of subclasses known at compile time.
- When a when expression covers all of them, no else is needed.
- Add a subclass later and the compiler flags every when that misses it.
Why the other options are wrong
- B. Exhaustiveness applies to sealed types and enums (and Boolean), not any type.
- C. It is a compile-time check, not a speed gain.
- D. Sealed classes restrict who can extend them.
Question 10Hard
What does this print?
val x: Int? = 5
val y = x?.takeIf { it > 10 } ?: "small"
println(y)
- 5
- Compile error
- null
- small
Answer: D. small
Explanation
takeIf returns null when the predicate fails, so the Elvis branch is used.
Step-by-step solution
- x = 5; takeIf { it > 10 } is false, so it returns null.
- null ?: "small" gives "small".
- y's type is Any, which compiles; it prints small.
Why the other options are wrong
- A. takeIf returns null when the test fails.
- B. Mixing Int and String gives type Any, which is valid.
- C. Elvis replaces the null.
Practise all 34 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 →