An interface in Kotlin is a contract that defines a set of functions and properties that a class can implement. It specifies what a class should do, but not how to do it.
Click to reveal answer
beginner
How do you declare an interface in Kotlin?
Use the interface keyword followed by the interface name and curly braces. Inside, define abstract functions or properties. Example:
interface Drivable { fun drive() }
Click to reveal answer
beginner
How does a class implement an interface in Kotlin?
A class uses a colon <code>:</code> followed by the interface name to implement it. The class must provide concrete implementations for all interface functions. Example: <br><pre>class Car : Drivable { override fun drive() { println("Driving") } }</pre>
Click to reveal answer
intermediate
Can interfaces in Kotlin have method implementations?
Yes, interfaces can have default method implementations. Classes implementing the interface can use these defaults or override them with their own versions.
Click to reveal answer
intermediate
What happens if a class does not implement all interface methods?
The Kotlin compiler will show an error. The class must implement all abstract methods of the interface unless the class is declared abstract.
Click to reveal answer
How do you declare an interface named Flyable in Kotlin?
Aclass Flyable { fun fly() }
Binterface Flyable() { fly() }
Cfun interface Flyable { fly() }
Dinterface Flyable { fun fly() }
✗ Incorrect
The correct syntax uses the interface keyword followed by the name and curly braces with function declarations.
What keyword does a Kotlin class use to implement an interface?
Aextends
Bimplements
C:
Dinherits
✗ Incorrect
Kotlin uses a colon : to indicate implementation or inheritance.
Can Kotlin interfaces contain properties?
AYes, but they must be abstract or have default getters
BNo, only functions
CYes, with backing fields
DNo, only constants
✗ Incorrect
Interfaces can declare properties but they cannot have backing fields. They can have abstract properties or properties with default getters.
What must a class do when it implements an interface with abstract methods?
AImplement all abstract methods or be declared abstract
BImplement only some methods
CNothing, methods are optional
DOverride only default methods
✗ Incorrect
The class must implement all abstract methods or be declared abstract itself.
If an interface method has a default implementation, can a class override it?
ANo, default implementations cannot be changed
BYes, the class can override it
COnly if the method is marked open
DOnly if the class is abstract
✗ Incorrect
Classes can override default interface methods to provide their own implementation.
Explain how to declare and implement an interface in Kotlin with a simple example.
Think about how you tell a class what it must do using an interface.
You got /5 concepts.
Describe what happens if a Kotlin class does not implement all abstract methods of an interface it declares to implement.
Consider what the compiler expects from a class that promises to follow an interface.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of an interface in Kotlin?
easy
A. To inherit implementation from multiple classes
B. To create objects directly without classes
C. To store data like variables and constants
D. To define a contract with functions and properties without implementation
Solution
Step 1: Understand what an interface is
An interface in Kotlin declares functions and properties but does not provide their implementation.
Step 2: Compare options with interface purpose
To define a contract with functions and properties without implementation correctly states that interfaces define a contract without implementation. Other options describe features unrelated to interfaces.
Final Answer:
To define a contract with functions and properties without implementation -> Option D
Quick Check:
Interface = Contract without code [OK]
Hint: Interfaces declare what, not how, functions work [OK]
Common Mistakes:
Thinking interfaces can create objects directly
Confusing interfaces with classes that store data
Believing interfaces inherit from classes
2. Which of the following is the correct way to declare an interface in Kotlin?
easy
A. interface MyInterface { fun doWork() }
B. class MyInterface { fun doWork() }
C. fun interface MyInterface { fun doWork(); fun doOther() }
D. interface MyInterface() { fun doWork() }
Solution
Step 1: Recall Kotlin interface syntax
Interfaces are declared with the keyword interface followed by the name and curly braces containing function declarations without parentheses after the name.
Step 2: Check each option
interface MyInterface { fun doWork() } matches the correct syntax. class MyInterface { fun doWork() } uses class instead of interface. fun interface MyInterface { fun doWork() } uses fun interface which is for functional interfaces but requires a single abstract method and different usage. interface MyInterface() { fun doWork() } incorrectly adds parentheses after the interface name.
Final Answer:
interface MyInterface { fun doWork() } -> Option A
Quick Check:
Interface syntax = interface Name { ... } [OK]
Hint: Interfaces use 'interface' keyword without parentheses [OK]
Common Mistakes:
Using class keyword instead of interface
Adding parentheses after interface name
Confusing fun interface with regular interface
3. What will be the output of the following Kotlin code?
interface Greeter {
fun greet(): String
}
class FriendlyGreeter : Greeter {
override fun greet() = "Hello!"
}
fun main() {
val greeter: Greeter = FriendlyGreeter()
println(greeter.greet())
}
medium
A. FriendlyGreeter
B. Greeter
C. Hello!
D. Compilation error
Solution
Step 1: Understand interface implementation
The interface Greeter declares a function greet(). The class FriendlyGreeter implements this interface and overrides greet() to return "Hello!".
Step 2: Trace the main function output
In main(), an instance of FriendlyGreeter is assigned to a variable of type Greeter. Calling greet() on this instance returns "Hello!" which is printed.
Hint: Interface calls use implemented class methods [OK]
Common Mistakes:
Expecting interface name printed instead of method result
Thinking it causes compilation error without implementation
Confusing class name with output
4. Identify the error in this Kotlin code snippet:
interface Animal {
fun sound(): String
}
class Dog : Animal {
fun sound() = "Bark"
}
medium
A. Dog class must override sound() with 'override' keyword
B. Interface cannot have functions with return type
C. Dog class should not implement Animal interface
D. sound() function should be private in Dog class
Solution
Step 1: Check interface function implementation rules
When a class implements an interface function, it must use the override keyword to provide the implementation.
Step 2: Analyze the Dog class
The Dog class defines sound() without override. This causes a compilation error because it does not properly override the interface method.
Final Answer:
Dog class must override sound() with 'override' keyword -> Option A
Quick Check:
Override keyword required for interface methods [OK]
Hint: Always use 'override' when implementing interface methods [OK]
Common Mistakes:
Omitting the override keyword
Thinking interface methods can't have return types
Making interface implementation optional
5. Given the interface and classes below, what will be the output when main() is run?
interface Calculator {
fun calculate(x: Int, y: Int): Int
}
class Adder : Calculator {
override fun calculate(x: Int, y: Int) = x + y
}
class Multiplier : Calculator {
override fun calculate(x: Int, y: Int) = x * y
}
fun main() {
val calculators: List<Calculator> = listOf(Adder(), Multiplier())
for (calc in calculators) {
println(calc.calculate(3, 4))
}
}
hard
A. 12
7
B. 7
12
C. 34
34
D. Compilation error due to interface list
Solution
Step 1: Understand interface implementations
Two classes implement Calculator: Adder adds two numbers, Multiplier multiplies them.
Step 2: Trace the loop output
The list contains an Adder and a Multiplier. Calling calculate(3,4) on Adder returns 7, on Multiplier returns 12. These are printed in order.
Final Answer:
7
12 -> Option B
Quick Check:
Adder + Multiplier outputs 7 and 12 [OK]
Hint: Interface list calls each class's method in order [OK]
Common Mistakes:
Mixing order of outputs
Expecting string concatenation instead of arithmetic
Thinking interface list causes error
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.