0
0
Swiftprogramming~20 mins

Base class and subclass in Swift - Practice Problems & Coding Challenges

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

What is the output of this Swift code?

Swift
class Animal {
    func sound() -> String {
        return "Some sound"
    }
}

class Dog: Animal {
    override func sound() -> String {
        return "Bark"
    }
}

let pet: Animal = Dog()
print(pet.sound())
ACompilation error due to missing override
BSome sound
CBark
DRuntime error
Attempts:
2 left
💡 Hint

Remember that subclass methods can replace base class methods when marked with override.

Predict Output
intermediate
2:00remaining
Value of property after subclass initialization

What will be printed by this Swift code?

Swift
class Vehicle {
    var wheels: Int
    init(wheels: Int) {
        self.wheels = wheels
    }
}

class Bicycle: Vehicle {
    init() {
        super.init(wheels: 2)
    }
}

let bike = Bicycle()
print(bike.wheels)
A2
B0
CCompilation error due to missing initializer
DRuntime error
Attempts:
2 left
💡 Hint

Check how the subclass calls the base class initializer.

🔧 Debug
advanced
2:00remaining
Identify the error in subclass initializer

What error does this Swift code produce?

Swift
class Person {
    var name: String
    init(name: String) {
        self.name = name
    }
}

class Employee: Person {
    var id: Int
    init(id: Int) {
        self.id = id
    }
}
AError: Missing call to super.init in subclass initializer
BError: Property 'name' not initialized before use
CNo error, code compiles fine
DError: Cannot override stored property
Attempts:
2 left
💡 Hint

Subclass initializers must call the base class initializer to initialize inherited properties.

Predict Output
advanced
2:00remaining
Output when calling base class method from subclass

What is the output of this Swift code?

Swift
class Printer {
    func printMessage() {
        print("Base message")
    }
}

class CustomPrinter: Printer {
    override func printMessage() {
        super.printMessage()
        print("Custom message")
    }
}

let p = CustomPrinter()
p.printMessage()
ACompilation error due to super call
BCustom message
CBase message
D
Base message
Custom message
Attempts:
2 left
💡 Hint

Look at how super.printMessage() is used inside the subclass method.

🧠 Conceptual
expert
2:00remaining
Number of stored properties in subclass instance

Given these Swift classes, how many stored properties does an instance of Manager have?

class Employee {
    var name: String
    var salary: Double
    init(name: String, salary: Double) {
        self.name = name
        self.salary = salary
    }
}

class Manager: Employee {
    var department: String
    init(name: String, salary: Double, department: String) {
        self.department = department
        super.init(name: name, salary: salary)
    }
}
A4
B3
C1
D2
Attempts:
2 left
💡 Hint

Count all stored properties declared in base and subclass.