Interfaces & Generics Questions with Answers

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

Sample questions

Question 1Easy

What is the difference between an abstract class and an interface in Kotlin?

  1. Interfaces can't have functions
  2. Abstract classes can't have functions
  3. A class can implement many interfaces but extend only one class
  4. There is none

Answer: C. A class can implement many interfaces but extend only one class

Explanation

Kotlin has single class inheritance but multiple interface implementation.

Step-by-step solution

  1. Class: one superclass.
  2. Interfaces: many allowed.
  3. Interfaces hold no state.

Why the other options are wrong

  • A. They can, even with default bodies.
  • B. They can.
  • D. Multiple inheritance differs.

Question 2Easy

What is the default visibility modifier in Kotlin?

  1. internal
  2. private
  3. public
  4. protected

Answer: C. public

Explanation

Declarations without a modifier are public.

Step-by-step solution

  1. In Kotlin, declarations without a modifier are public.
  2. Public is the default visibility modifier.
  3. So the correct answer is public.

Why the other options are wrong

  • A. internal means visible within the same module, not the default visibility.
  • B. private limits visibility to the declaring scope, and is not the default.
  • D. protected is for subclasses and is not the default for top-level declarations.

Tip: Remember: 'public' is default in Kotlin.

Question 3Easy

Can a Kotlin interface contain method implementations?

  1. No, only abstract signatures are allowed
  2. Yes, interface methods can have default bodies
  3. Only if the method is marked static
  4. Only inside abstract classes, never plain interfaces

Answer: B. Yes, interface methods can have default bodies

Explanation

Kotlin interfaces may include default implementations and abstract properties, but no state.

Step-by-step solution

  1. Kotlin interfaces can declare functions with a default body.
  2. Classes that implement the interface can use that body or override it. Marked answer: option B.

Why the other options are wrong

  • A. Kotlin interfaces can have default method bodies.
  • C. No static modifier is needed; interface methods can have bodies.
  • D. Plain interfaces can have default bodies; abstract classes are not required.

Tip: Interfaces can have default method bodies; classes can override them.

Question 4Easy

What is the output?

interface Sh { fun area(): Int }
class Sq(val s: Int) : Sh {
    override fun area() = s * s
}
println(Sq(3).area())
  1. 6
  2. 9
  3. 3
  4. Compile error

Answer: B. 9

Explanation

Sq overrides area to return s * s = 9.

Step-by-step solution

  1. Sq(3).
  2. area = 3*3.
  3. 9.

Why the other options are wrong

  • A. area squares, it doesn't double.
  • C. area returns s * s.
  • D. Override is correct.

Question 5Medium

What is a sealed class used for?

  1. Preventing any instance of the class from ever being created
  2. Making every instance of the class automatically thread-safe
  3. Hiding the class from every other module in the project
  4. Restricting direct subclasses to those known at compile time

Answer: D. Restricting direct subclasses to those known at compile time

Explanation

Sealed hierarchies are closed, enabling exhaustive when expressions.

Step-by-step solution

  1. A sealed class restricts inheritance.
  2. Only specific subclasses can inherit from it.
  3. These subclasses must be known at compile time.
  4. This ensures controlled and predictable hierarchy.
  5. Final answer: Restricting direct subclasses to those known at compile time.

Why the other options are wrong

  • A. A sealed class can be instantiated when it is not abstract; its purpose is restricting subclasses.
  • B. Sealed classes have nothing to do with thread safety.
  • C. Sealed does not control module visibility; that is done with internal.

Tip: Use sealed classes for restricted inheritance in Kotlin.

Question 6Medium

What is the output?

fun <T : Comparable<T>> bigger(a: T, b: T) = if (a > b) a else b
println(bigger("pear", "apple"))
  1. apple
  2. pear
  3. pear apple
  4. Compile error

Answer: B. pear

Explanation

Strings compare lexicographically; 'p' comes after 'a', so "pear" is bigger.

Step-by-step solution

  1. T is String.
  2. "pear" > "apple".
  3. pear.

Why the other options are wrong

  • A. "pear" > "apple" alphabetically.
  • C. One value is returned.
  • D. String is Comparable.

Question 7Medium

What does the out modifier in class Box<out T> mean?

  1. T is only produced, so Box<Sub> is a subtype of Box<Super> (covariance)
  2. Box<Super> is a subtype of Box<Sub>
  3. T can never be null anywhere inside the class body or its members
  4. T is always erased and is therefore unavailable to callers

Answer: A. T is only produced, so Box<Sub> is a subtype of Box<Super> (covariance)

Explanation

out declares covariance: T appears only in output positions.

Step-by-step solution

  1. Out marks T as only produced, never taken in, so the class is covariant.
  2. Box<Sub> can then be used where Box<Super> is expected. Marked answer: option A.

Why the other options are wrong

  • B. That describes consuming T, which is the in modifier, and the subtyping direction is wrong.
  • C. Out does not make T non-null anywhere.
  • D. Out is about variance, not erasure of T.

Tip: out = producer, covariant; in = consumer, contravariant.

Question 8Medium

What is out in interface Source<out T> called?

  1. Contravariance
  2. Declaration-site covariance: T is only produced
  3. A nullable marker
  4. A reified type parameter

Answer: B. Declaration-site covariance: T is only produced

Explanation

With out, Source<String> is a subtype of Source<Any> because T only appears in output positions.

Step-by-step solution

  1. out = producer.
  2. Subtype relation is preserved.
  3. Covariance.

Why the other options are wrong

  • A. That is in.
  • C. Not related to nullability.
  • D. reified is separate.

Tip: Remember: out = produces, in = consumes.

Question 9Hard

What is the output?

inline fun <reified T> isA(x: Any) = x is T
println(isA<String>("s"))
  1. true
  2. false
  3. Compile error
  4. T

Answer: A. true

Explanation

reified keeps the type argument at runtime in inline functions, so x is T works.

Step-by-step solution

  1. T is reified as String.
  2. "s" is a String.
  3. true.

Why the other options are wrong

  • B. "s" is a String.
  • C. reified allows is T in inline functions.
  • D. The function returns Boolean.

Question 10Hard

What is the output?

interface Sh { fun area(): Int }
class Sq(val s: Int) : Sh { override fun area() = s * s }
class W(s: Sh) : Sh by s
println(W(Sq(4)).area())
  1. 4
  2. 0
  3. Compile error
  4. 16

Answer: D. 16

Explanation

Sh by s forwards all interface calls to s, so W(Sq(4)).area() = 16.

Step-by-step solution

  1. W delegates to Sq(4).
  2. area() = 4*4.
  3. 16.

Why the other options are wrong

  • A. Delegation forwards to Sq.
  • B. No default value exists.
  • C. by delegates interface members.

Practise all 11 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