0
0
Kotlinprogramming~20 mins

Abstract classes and methods in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Abstract Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of abstract class method call

What is the output of this Kotlin code?

Kotlin
abstract class Animal {
    abstract fun sound(): String
}

class Dog : Animal() {
    override fun sound() = "Bark"
}

fun main() {
    val dog: Animal = Dog()
    println(dog.sound())
}
ABark
BSound
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint

Remember that abstract methods must be overridden in subclasses.

🧠 Conceptual
intermediate
1:30remaining
Abstract class instantiation

Which statement about abstract classes in Kotlin is true?

AAll methods in an abstract class must be abstract.
BAbstract classes cannot have constructors.
CAbstract classes can have both abstract and non-abstract methods.
DYou can create an instance of an abstract class directly.
Attempts:
2 left
💡 Hint

Think about what abstract classes are used for and their flexibility.

🔧 Debug
advanced
2:30remaining
Identify the error in abstract method override

What error will this Kotlin code produce?

Kotlin
abstract class Vehicle {
    abstract fun move(): String
}

class Car : Vehicle() {
    fun move(): String {
        return "Driving"
    }
}

fun main() {
    val car = Car()
    println(car.move())
}
ACompilation error: 'move' in 'Car' hides abstract member in 'Vehicle' and must be marked override
BRuntime error: Abstract method not implemented
CNo error, output: Driving
DCompilation error: Cannot instantiate abstract class
Attempts:
2 left
💡 Hint

Check if the method in subclass properly overrides the abstract method.

📝 Syntax
advanced
2:00remaining
Correct syntax for abstract class with constructor

Which option shows the correct way to declare an abstract class with a primary constructor in Kotlin?

A
abstract class Shape {
    constructor(name: String)
    abstract fun area(): Double
}
B
abstract class Shape(val name: String) {
    abstract fun area(): Double
}
C
abstract class Shape() {
    val name: String
    abstract fun area(): Double
}
D
abstract class Shape {
    val name: String
    abstract fun area(): Double
    constructor(name: String) { this.name = name }
}
Attempts:
2 left
💡 Hint

Remember how primary constructors are declared in Kotlin classes.

🚀 Application
expert
3:00remaining
Determine the output of complex abstract class hierarchy

What is the output of this Kotlin program?

Kotlin
abstract class A {
    abstract fun f(): String
    fun g() = "G from A"
}

abstract class B : A() {
    override fun f() = "F from B"
    abstract fun h(): String
}

class C : B() {
    override fun h() = "H from C"
}

fun main() {
    val c = C()
    println(c.f())
    println(c.g())
    println(c.h())
}
ARuntime error: Abstract method h() not implemented
B
F from B
G from B
H from C
CCompilation error: Class C must override abstract method g()
D
F from B
G from A
H from C
Attempts:
2 left
💡 Hint

Trace the inheritance and method overrides carefully.