Bird
Raised Fist0
Swiftprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1.

What keyword must you use in Swift to change a method from a parent class in a subclass?

easy
A. override
B. super
C. extend
D. inherit

Solution

  1. Step 1: Understand method overriding in Swift

    Swift requires a special keyword to indicate a method is being changed in a subclass.
  2. Step 2: Identify the correct keyword

    The keyword to override a method or property is override.
  3. Final Answer:

    override -> Option A
  4. Quick Check:

    Keyword to change parent method = override [OK]
Hint: Use 'override' before methods to change them in subclasses [OK]
Common Mistakes:
  • Using 'super' instead of 'override'
  • Forgetting the 'override' keyword
  • Using 'extend' which is not a Swift keyword
2.

Which of the following is the correct syntax to override a method named greet() in Swift?

class Parent {
    func greet() {
        print("Hello")
    }
}

class Child: Parent {
    ??? func greet() {
        print("Hi")
    }
}
easy
A. override
B. super
C. func override
D. override func

Solution

  1. Step 1: Recall Swift override syntax

    In Swift, the keyword override comes before the func keyword.
  2. Step 2: Match the correct syntax

    The correct syntax is override func greet().
  3. Final Answer:

    override func -> Option D
  4. Quick Check:

    Keyword order is 'override func' [OK]
Hint: Write 'override' before 'func' to override methods [OK]
Common Mistakes:
  • Writing 'func override' instead of 'override func'
  • Using 'super' keyword instead of 'override'
  • Omitting 'override' keyword entirely
3.

What will be the output of the following Swift code?

class Animal {
    func sound() {
        print("Some sound")
    }
}

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

let pet: Animal = Dog()
pet.sound()
medium
A. Some sound
B. Bark
C. Compile error
D. Runtime error

Solution

  1. Step 1: Understand method overriding and polymorphism

    Even though the variable type is Animal, the actual object is Dog, so overridden method runs.
  2. Step 2: Identify which method is called

    The Dog's overridden sound() method prints "Bark".
  3. Final Answer:

    Bark -> Option B
  4. Quick Check:

    Overridden method runs on subclass instance [OK]
Hint: Overridden methods run on subclass instances even if typed as parent [OK]
Common Mistakes:
  • Thinking parent method runs because variable type is Animal
  • Expecting compile or runtime error
  • Confusing method overriding with overloading
4.

Identify the error in this Swift code that tries to override a property:

class Vehicle {
    var speed: Int = 0
}

class Car: Vehicle {
    var speed: Int = 100
}
medium
A. Missing 'override' keyword before 'var speed' in subclass
B. Cannot override properties in Swift
C. Property types must differ when overriding
D. Superclass property must be 'let' to override

Solution

  1. Step 1: Check property overriding rules

    To override a property in Swift, you must use the override keyword.
  2. Step 2: Identify missing keyword

    The subclass property speed lacks the override keyword, causing an error.
  3. Final Answer:

    Missing 'override' keyword before 'var speed' in subclass -> Option A
  4. Quick Check:

    Override properties need 'override' keyword [OK]
Hint: Always add 'override' before properties you change in subclass [OK]
Common Mistakes:
  • Forgetting 'override' keyword on properties
  • Thinking properties cannot be overridden
  • Changing property type when overriding
5.

Given this Swift code, what will be printed?

class Person {
    var description: String {
        return "Person"
    }
}

class Student: Person {
    override var description: String {
        return super.description + " - Student"
    }
}

let s = Student()
print(s.description)
hard
A. Person
B. Student
C. Person - Student
D. Compile error due to super usage

Solution

  1. Step 1: Understand property overriding with super

    The subclass overrides description and calls super.description to get parent value.
  2. Step 2: Combine parent and subclass strings

    The returned string is "Person" + " - Student" = "Person - Student".
  3. Final Answer:

    Person - Student -> Option C
  4. Quick Check:

    super.property adds parent value [OK]
Hint: Use 'super' to include parent property value when overriding [OK]
Common Mistakes:
  • Expecting only 'Student' without parent text
  • Thinking 'super' causes compile error
  • Not using 'override' keyword on property
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard