Kotlin Basics Questions with Answers

34 practice questions on Kotlin Basics (Android (Kotlin & Compose)) with answers and step-by-step solutions. Try 10 samples below, then practise all 34 free in the AptiNest app.

Sample questions

Question 1Easy

Which is true about listOf(1, 2, 3) in Kotlin?

  1. It is always empty
  2. You can call add() on it
  3. It returns an Array
  4. It is read-only

Answer: D. It is read-only

Explanation

Use mutableListOf for a list you can modify.

Step-by-step solution

  1. listOf returns a List, Kotlin's read-only interface.
  2. List has no add or remove.
  3. 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. [1, 2, 3, 1, 2, 3]
  2. [2, 4, 6]
  3. [1, 4, 9]
  4. 12

Answer: B. [2, 4, 6]

Explanation

map transforms each element; it is the implicit lambda parameter.

Step-by-step solution

  1. map applies the lambda to every element.
  2. it * 2 doubles each: 1 -> 2, 2 -> 4, 3 -> 6.
  3. 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))
  1. {x=1, y=2}
  2. (1, 2)
  3. Point@1b6d3586
  4. Point(x=1, y=2)

Answer: D. Point(x=1, y=2)

Explanation

Data classes generate a readable toString.

Step-by-step solution

  1. Data classes generate toString() automatically.
  2. The format is ClassName(prop=value, ...).
  3. 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)
  1. Compile error
  2. NullPointerException
  3. null
  4. 0

Answer: C. null

Explanation

The safe-call operator ?. returns null instead of throwing when the receiver is null.

Step-by-step solution

  1. s is null.
  2. ?. is the safe call: if the receiver is null, the whole expression is null.
  3. 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))
  1. Hi 2 Hi 2
  2. Hi Guest Hi Guest
  3. Compile error: missing name
  4. Hi Guest

Answer: B. Hi Guest Hi Guest

Explanation

Named arguments let you skip earlier parameters that have defaults.

Step-by-step solution

  1. Named argument times = 2; name uses its default "Guest".
  2. "Hi Guest ".repeat(2) = "Hi Guest Hi Guest ".
  3. 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")
  1. $name has ${name.length} letters
  2. Kotlin has 6 letters
  3. name has 6 letters
  4. Kotlin has name.length letters

Answer: B. Kotlin has 6 letters

Explanation

String templates insert values with $ and expressions with ${}.

Step-by-step solution

  1. $name inserts the value of name: Kotlin.
  2. ${name.length} evaluates the expression: 6.
  3. 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:

  1. Make the class a singleton
  2. Create a second instance of the class
  3. Hold class-level members
  4. 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

  1. Kotlin has no static keyword.
  2. Members inside companion object belong to the class, not instances.
  3. 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}")
  1. false true
  2. false false
  3. true false
  4. true true

Answer: C. true false

Explanation

== uses the generated equals (structural); === compares references.

Step-by-step solution

  1. == calls equals(); data classes compare property values.
  2. Both have x = 1, y = 2, so a == b is true.
  3. === 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?

  1. The compiler checks all subclasses are handled
  2. It removes the need for else in all whens on any type
  3. It makes when run faster
  4. 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

  1. A sealed class has a fixed set of subclasses known at compile time.
  2. When a when expression covers all of them, no else is needed.
  3. 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)
  1. 5
  2. Compile error
  3. null
  4. small

Answer: D. small

Explanation

takeIf returns null when the predicate fails, so the Elvis branch is used.

Step-by-step solution

  1. x = 5; takeIf { it > 10 } is false, so it returns null.
  2. null ?: "small" gives "small".
  3. 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 →

Related topics

See all Android (Kotlin & Compose) topics · All topics