What is the output of this Kotlin code?
abstract class Animal { abstract fun sound(): String } class Dog : Animal() { override fun sound() = "Bark" } fun main() { val dog: Animal = Dog() println(dog.sound()) }
Remember that abstract methods must be overridden in subclasses.
The Dog class overrides the abstract sound() method and returns "Bark". So, calling dog.sound() prints "Bark".
Which statement about abstract classes in Kotlin is true?
Think about what abstract classes are used for and their flexibility.
Abstract classes can have both abstract methods (without body) and concrete methods (with body). You cannot instantiate abstract classes directly, but they can have constructors.
What error will this Kotlin code produce?
abstract class Vehicle { abstract fun move(): String } class Car : Vehicle() { fun move(): String { return "Driving" } } fun main() { val car = Car() println(car.move()) }
Check if the method in subclass properly overrides the abstract method.
The move() method in Car is missing the override keyword. Kotlin requires explicit override for abstract methods.
Which option shows the correct way to declare an abstract class with a primary constructor in Kotlin?
Remember how primary constructors are declared in Kotlin classes.
Option B correctly declares an abstract class with a primary constructor and an abstract method. Other options have syntax errors or missing initialization.
What is the output of this Kotlin program?
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()) }
Trace the inheritance and method overrides carefully.
Class C inherits f() from B, g() from A, and overrides h(). So the output matches option D.