Sample questions
Question 1Easy
Which keyword creates a class meant mainly to hold data and auto-generates equals, hashCode, toString and copy?
- record class
- struct
- value object
- data class
Answer: D. data class
Explanation
A data class generates equals, hashCode, toString, componentN and copy from its primary constructor properties.
Step-by-step solution
- A data class automatically generates equals, hashCode, toString, and copy.
- These methods are created based on the primary constructor's properties.
- The keyword used is 'data class' in Kotlin.
- This makes it ideal for holding data with these built-in functions.
- Final answer: data class.
Why the other options are wrong
- A. Kotlin has no record class keyword; Java records are a different feature.
- B. struct is not a Kotlin keyword, so it cannot declare a data holder.
- C. value object is not a Kotlin keyword; value classes use the value class syntax instead.
Tip: Use 'data class' for objects that mainly store data with built-in methods.
Question 2Easy
What is the output?
data class P(val x: Int, val y: Int)
println(P(1, 2).copy(y = 5))
- P(x=1, y=5)
- P(x=1, y=2)
- P(1, 5)
- Compile error
Answer: A. P(x=1, y=5)
Explanation
copy creates a new instance with y changed; toString prints named properties.
Step-by-step solution
- Original (1, 2).
- copy(y = 5) -> (1, 5).
- Printed with names.
Why the other options are wrong
- B. copy changes y.
- C. data class toString uses names.
- D. data classes generate copy.
Question 3Easy
What does class Person(val name: String) declare?
- Only a constructor parameter that is not a property
- A mutable property name
- A static field
- A class with a primary constructor and a read-only property name
Answer: D. A class with a primary constructor and a read-only property name
Explanation
val or var in the primary constructor makes the parameter a property.
Step-by-step solution
- Primary constructor in the header.
val makes a property.- Read-only getter only.
Why the other options are wrong
- A. val/var in the header makes a property.
- B. val is read-only.
- C. Kotlin has no static keyword in classes.
Question 4Easy
By default, Kotlin classes are:
- open to inheritance unless marked final
- abstract unless marked otherwise
- sealed unless marked open
- final, so they cannot be inherited unless marked open
Answer: D. final, so they cannot be inherited unless marked open
Explanation
Classes and members are final by default; you must write open to allow subclassing or overriding.
Step-by-step solution
- Kotlin classes are final by default.
- Final means they cannot be inherited.
- You must use
open to allow inheritance. - So the correct answer is final, so they cannot be inherited unless marked open.
Why the other options are wrong
- A. Kotlin is the reverse: classes are final by default, and open is needed to allow inheritance.
- B. Classes are not abstract by default; abstract must be written explicitly.
- C. sealed restricts subclasses and is a separate modifier, not the default for all classes.
Tip: Remember: Kotlin defaults to final; use open for inheritance.
Question 5Medium
What does the internal modifier mean?
- Visible only inside the same file
- Visible to subclasses only
- Visible anywhere within the same module
- Visible to everyone
Answer: C. Visible anywhere within the same module
Explanation
internal members are visible within the same compilation module (for example a Gradle module).
Step-by-step solution
- Between private and public.
- Scope is the module.
- Not visible to other modules.
Why the other options are wrong
- A. That is private at top level.
- B. That is protected.
- D. That is public.
Question 6Medium
What is the output?
open class A { init { print("A") } }
class B : A() { init { print("B") } }
B()
- BA
- AB
- A
- B
Answer: B. AB
Explanation
A superclass constructor and init blocks run before the subclass's.
Step-by-step solution
- B() calls A() first.
- A prints.
- Then B prints.
Why the other options are wrong
- A. The superclass is initialised first.
- C. Both inits run.
- D. Both inits run.
Question 7Medium
What is the output?
class R(val w: Int, val h: Int) {
val area get() = w * h
}
println(R(2, 3).area)
- 5
- 6
- 23
- Compile error
Answer: B. 6
Explanation
The getter computes w * h each time it is read.
Step-by-step solution
- w=2, h=3.
- Getter multiplies.
- 6.
Why the other options are wrong
- A. area multiplies.
- C. It is arithmetic, not concatenation.
- D. A custom getter with an inferred type is valid.
Question 8Medium
What is the output?
object Counter {
var c = 0
fun inc() = ++c
}
Counter.inc()
println(Counter.inc())
- 1
- 2
- 0
- Compile error
Answer: B. 2
Explanation
object is a singleton; c goes 1 then 2 across calls.
Step-by-step solution
- First call c=1.
- Second call c=2.
- Prints 2.
Why the other options are wrong
- A. The object is a single instance that remembers state.
- C. inc increases c.
- D. Objects may hold mutable state.
Question 9Hard
What is the output?
class A {
val x: Int by lazy { println("calc"); 5 }
}
val a = A()
println(a.x + a.x)
- calc twice, then 10
- 10 only
- calc, then 10 on the next line
- 5
Answer: C. calc, then 10 on the next line
Explanation
by lazy runs its initialiser on first access only and caches the value, so calc prints once and 5 + 5 = 10.
Step-by-step solution
- First a.x runs lambda and prints calc.
- Value 5 is cached.
- 5 + 5 = 10.
Why the other options are wrong
- A. lazy computes once and caches.
- B. The initialiser prints calc the first time.
- D. Both accesses are added.
Question 10Hard
Which statement about companion object is correct?
- It holds class-level members, called as ClassName.member, and can implement interfaces
- It creates one new companion object per instance of the enclosing class
- It can only contain constants that are marked with the const modifier
- It is simply the Kotlin syntax for a Java static initializer block
Answer: A. It holds class-level members, called as ClassName.member, and can implement interfaces
Explanation
A companion is a real singleton object scoped to the class; it can have state, functions and implement interfaces.
Step-by-step solution
- Companion object is a singleton object tied to the class, not instances.
- It can hold members accessed as ClassName.member and implement interfaces.
- It can have functions and state, not just constants or static blocks.
- Option A correctly describes companion object features.
- Final answer: It holds class-level members, called as ClassName.member, and can implement interfaces.
Why the other options are wrong
- B. A companion object is a single object tied to the class, shared by all instances.
- C. Companion objects can hold functions and mutable state, not only const values.
- D. A companion object is an object with its own members; it is not the same as a static block.
Tip: Remember companion object is not per instance; it's a single shared object for the whole class.
Practise all 15 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 →