Bird
Raised Fist0
Kotlinprogramming~20 mins

Multiple interface implementation in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Multiple Interface Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of multiple interface implementation with method override
What is the output of this Kotlin code when the test() function is called?
Kotlin
interface A {
    fun greet() = "Hello from A"
}

interface B {
    fun greet() = "Hello from B"
}

class C : A, B {
    override fun greet(): String {
        return super<A>.greet() + " and " + super<B>.greet()
    }
}

fun test() {
    val c = C()
    println(c.greet())
}
AHello from B and Hello from A
BHello from A
CCompilation error due to ambiguous greet()
DHello from A and Hello from B
Attempts:
2 left
💡 Hint
Look at how super is used to specify which interface's method to call.
❓ Predict Output
intermediate
2:00remaining
Which method is called in multiple interface implementation?
Given the following Kotlin code, what will be printed when test() is executed?
Kotlin
interface X {
    fun action() = "Action from X"
}

interface Y {
    fun action() = "Action from Y"
}

class Z : X, Y {
    override fun action(): String {
        return super<Y>.action()
    }
}

fun test() {
    val z = Z()
    println(z.action())
}
AAction from Y
BAction from X
CCompilation error due to ambiguity
DRuntime error
Attempts:
2 left
💡 Hint
Check which interface's method is explicitly called with super.
🔧 Debug
advanced
2:00remaining
Identify the error in multiple interface implementation
What error will this Kotlin code produce when compiled?
Kotlin
interface M {
    fun foo() = "M"
}

interface N {
    fun foo()
}

class O : M, N {
    // No override of foo()
}
ACompilation error: Class O must override foo()
BNo error, compiles successfully
CCompilation error: Conflicting implementations of foo()
DRuntime error: foo() not implemented
Attempts:
2 left
💡 Hint
Check which interface requires foo() to be implemented and which provides a default.
📝 Syntax
advanced
2:00remaining
Correct syntax for multiple interface implementation with property
Which option correctly implements two interfaces with a property of the same name in Kotlin?
Kotlin
interface P {
    val value: String
}

interface Q {
    val value: String
}

class R : P, Q {
    // Implement value here
}
Aoverride fun value() = "Hello"
Boverride var value = "Hello"
Coverride val value = "Hello"
Dval value = "Hello"
Attempts:
2 left
💡 Hint
Properties from interfaces must be overridden with 'override val' or 'override var'.
🚀 Application
expert
3:00remaining
Implement a class that combines behaviors from two interfaces
You have two interfaces in Kotlin:

interface Logger { fun log(msg: String) }
interface Printer { fun print(msg: String) }

Implement a class Console that implements both interfaces. The log method should print the message prefixed with "LOG: ", and the print method should print the message prefixed with "PRINT: ".

Which of the following class implementations correctly does this?
A
class Console : Logger, Printer {
    fun log(msg: String) {
        println("LOG: $msg")
    }
    fun print(msg: String) {
        println("PRINT: $msg")
    }
}
B
class Console : Logger, Printer {
    override fun log(msg: String) {
        println("LOG: $msg")
    }
    override fun print(msg: String) {
        println("PRINT: $msg")
    }
}
C
class Console : Logger, Printer {
    override fun log(msg: String) {
        println("PRINT: $msg")
    }
    override fun print(msg: String) {
        println("LOG: $msg")
    }
}
D
class Console : Logger, Printer {
    override fun log(msg: String) = println("LOG: $msg")
}
Attempts:
2 left
💡 Hint
Remember to use 'override' keyword for interface methods and implement both methods.

Practice

(1/5)
1. What does it mean when a Kotlin class implements multiple interfaces?
easy
A. The class cannot have its own functions if it implements interfaces.
B. The class can only use one interface's functions at a time.
C. The class agrees to provide all functions defined in all those interfaces.
D. The class automatically inherits properties from all interfaces without code.

Solution

  1. Step 1: Understand interface implementation

    Implementing interfaces means the class must provide the functions declared in those interfaces.
  2. Step 2: Check multiple interfaces

    When multiple interfaces are implemented, the class must provide all functions from each interface.
  3. Final Answer:

    The class agrees to provide all functions defined in all those interfaces. -> Option C
  4. Quick Check:

    Multiple interface implementation = implement all interface functions [OK]
Hint: Remember: Implementing means providing all interface functions [OK]
Common Mistakes:
  • Thinking interfaces provide code automatically
  • Believing only one interface's functions are needed
  • Assuming class can't have its own functions
2. Which of the following is the correct syntax to declare a Kotlin class MyClass implementing interfaces InterfaceA and InterfaceB?
easy
A. class MyClass : InterfaceA, InterfaceB {}
B. class MyClass implements InterfaceA, InterfaceB {}
C. class MyClass extends InterfaceA, InterfaceB {}
D. class MyClass : InterfaceA & InterfaceB {}

Solution

  1. Step 1: Recall Kotlin interface syntax

    Kotlin uses a colon (:) to implement interfaces, separated by commas.
  2. Step 2: Check each option

    class MyClass : InterfaceA, InterfaceB {} uses colon and commas correctly. Options B and C use Java-like keywords. class MyClass : InterfaceA & InterfaceB {} uses & which is invalid in Kotlin.
  3. Final Answer:

    class MyClass : InterfaceA, InterfaceB {} -> Option A
  4. Quick Check:

    Use ':' and commas for interfaces [OK]
Hint: Use ':' and commas to list interfaces in Kotlin [OK]
Common Mistakes:
  • Using 'implements' keyword like Java
  • Using 'extends' for interfaces
  • Using '&' to combine interfaces
3. What will be the output of this Kotlin code?
interface A { fun greet() = println("Hello from A") }
interface B { fun greet() = println("Hello from B") }
class C : A, B {
  override fun greet() {
    super<A>.greet()
    super<B>.greet()
  }
}
fun main() {
  val c = C()
  c.greet()
}
medium
A. Hello from A\nHello from B
B. Hello from B\nHello from A
C. Hello from A
D. Compilation error due to greet() conflict

Solution

  1. Step 1: Understand interface default methods

    Both interfaces A and B have greet() with default implementations.
  2. Step 2: Check class C's override

    C overrides greet() and calls both super<A>.greet() and super<B>.greet() in order.
  3. Step 3: Predict output

    First prints "Hello from A", then "Hello from B" on separate lines.
  4. Final Answer:

    Hello from A Hello from B -> Option A
  5. Quick Check:

    Calling both super interface methods prints both greetings [OK]
Hint: Use super<InterfaceName> to call specific interface method [OK]
Common Mistakes:
  • Assuming compilation error due to method conflict
  • Only calling one interface's greet()
  • Mixing order of greetings
4. Identify the error in this Kotlin code where class D implements interfaces X and Y:
interface X { fun foo() }
interface Y { fun bar() }
class D : X, Y {
  override fun foo() { println("foo") }
}
medium
A. Cannot implement two interfaces at once
B. Missing override of bar() from interface Y
C. foo() should not be overridden
D. Class D should be abstract

Solution

  1. Step 1: Check implemented interfaces

    D implements X and Y, so must override all their functions.
  2. Step 2: Verify overridden functions

    D overrides foo() from X but does not override bar() from Y.
  3. Step 3: Identify error

    Missing override of bar() causes compilation error.
  4. Final Answer:

    Missing override of bar() from interface Y -> Option B
  5. Quick Check:

    All interface functions must be overridden [OK]
Hint: Override all interface functions or make class abstract [OK]
Common Mistakes:
  • Forgetting to override all interface methods
  • Thinking multiple interfaces are not allowed
  • Assuming override is optional
5. You have two interfaces:
interface Printer { fun print() }
interface Scanner { fun scan() }

You want a class MultiFunctionDevice that implements both and logs each action. Which is the best way to implement this?
hard
A. class MultiFunctionDevice : Scanner { override fun scan() { println("Scanning...") } }
B. class MultiFunctionDevice : Printer { override fun print() { println("Printing...") } }
C. class MultiFunctionDevice { fun print() { println("Printing...") } fun scan() { println("Scanning...") } }
D. class MultiFunctionDevice : Printer, Scanner { override fun print() { println("Printing...") } override fun scan() { println("Scanning...") } }

Solution

  1. Step 1: Understand requirement

    MultiFunctionDevice must implement both Printer and Scanner interfaces.
  2. Step 2: Check options

    class MultiFunctionDevice : Printer, Scanner { override fun print() { println("Printing...") } override fun scan() { println("Scanning...") } } implements both interfaces and overrides both functions with logging. Options A and B implement only one interface each. class MultiFunctionDevice { fun print() { println("Printing...") } fun scan() { println("Scanning...") } } does not implement interfaces.
  3. Final Answer:

    class MultiFunctionDevice : Printer, Scanner { override fun print() { println("Printing...") } override fun scan() { println("Scanning...") } } -> Option D
  4. Quick Check:

    Implement all interfaces and override all methods [OK]
Hint: Implement all interfaces and override all their methods [OK]
Common Mistakes:
  • Implementing only one interface when multiple are needed
  • Not overriding interface methods
  • Not declaring interfaces in class header
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard