0
0
Swiftprogramming~20 mins

Overriding methods and properties in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Overriding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of overridden method call
What is the output of this Swift code when calling dog.makeSound()?
Swift
class Animal {
    func makeSound() {
        print("Some generic sound")
    }
}

class Dog: Animal {
    override func makeSound() {
        print("Bark")
    }
}

let dog = Dog()
dog.makeSound()
ASome generic sound
BBark
CCompilation error due to missing override keyword
DRuntime error
Attempts:
2 left
💡 Hint
Remember that the override keyword allows a subclass to provide its own implementation of a method.
Predict Output
intermediate
2:00remaining
Output of overridden computed property
What will be printed when accessing circle.description in this Swift code?
Swift
class Shape {
    var description: String {
        return "A shape"
    }
}

class Circle: Shape {
    override var description: String {
        return "A circle"
    }
}

let circle = Circle()
print(circle.description)
AA shape
BRuntime error
CCompilation error: cannot override stored property
DA circle
Attempts:
2 left
💡 Hint
Check how computed properties can be overridden with override var.
🔧 Debug
advanced
2:00remaining
Identify the error in method overriding
Which option correctly explains the error in this Swift code?
Swift
class Vehicle {
    func startEngine() {
        print("Engine started")
    }
}

class Car: Vehicle {
    func startEngine() {
        print("Car engine started")
    }
}
ANo error, code runs and prints "Car engine started"
BRuntime error due to method ambiguity
CCompilation error: missing 'override' keyword before 'startEngine()' in Car
DCompilation error: cannot override a method without 'final' keyword
Attempts:
2 left
💡 Hint
In Swift, overriding a method requires the override keyword.
Predict Output
advanced
2:00remaining
Output of property observer in overridden property
What is the output when setting mySquare.sideLength = 10 in this Swift code?
Swift
class Square {
    var sideLength: Int = 0 {
        willSet {
            print("Square will set sideLength to \(newValue)")
        }
    }
}

class ColoredSquare: Square {
    override var sideLength: Int {
        get {
            super.sideLength
        }
        set {
            print("ColoredSquare will set sideLength to \(newValue)")
            super.sideLength = newValue
        }
    }
}

let mySquare = ColoredSquare()
mySquare.sideLength = 10
AColoredSquare will set sideLength to 10
BSquare will set sideLength to 10
CBoth "Square will set sideLength to 10" and "ColoredSquare will set sideLength to 10"
DNo output
Attempts:
2 left
💡 Hint
Overridden property observers replace the superclass observers.
🧠 Conceptual
expert
2:00remaining
Behavior of overriding a property with different types
Consider this Swift code. What error will occur and why?
Swift
class Parent {
    var value: Int {
        return 5
    }
}

class Child: Parent {
    override var value: String {
        return "Five"
    }
}
ACompilation error: Cannot override property with a different type
BRuntime error: Type mismatch when accessing value
CNo error, Child's value returns "Five" as String
DCompilation error: Missing 'override' keyword
Attempts:
2 left
💡 Hint
Overriding properties must keep the same type in Swift.