Challenge - 5 Problems
Final Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of overriding a final method
What will be the output of this Swift code?
Swift
class Vehicle { final func start() { print("Vehicle started") } } class Car: Vehicle { override func start() { print("Car started") } } let myCar = Car() myCar.start()
Attempts:
2 left
💡 Hint
Remember what the 'final' keyword means for methods in Swift.
✗ Incorrect
The 'final' keyword prevents a method from being overridden in subclasses. Trying to override a final method causes a compile-time error.
❓ Predict Output
intermediate2:00remaining
Output when subclass tries to override a non-final method
What will be printed when running this Swift code?
Swift
class Animal { func sound() { print("Some sound") } } class Dog: Animal { override func sound() { print("Bark") } } let pet: Animal = Dog() pet.sound()
Attempts:
2 left
💡 Hint
Check if the method is marked final or not.
✗ Incorrect
Since 'sound()' is not final, the Dog class can override it. The call uses dynamic dispatch and prints 'Bark'.
🔧 Debug
advanced2:00remaining
Identify the error caused by final class inheritance
What error will this Swift code produce?
Swift
final class Calculator { func add(_ a: Int, _ b: Int) -> Int { return a + b } } class AdvancedCalculator: Calculator { func multiply(_ a: Int, _ b: Int) -> Int { return a * b } }
Attempts:
2 left
💡 Hint
Check the meaning of 'final' when applied to classes.
✗ Incorrect
A class marked as 'final' cannot be subclassed. Trying to inherit from it causes a compile-time error.
📝 Syntax
advanced2:00remaining
Which code correctly prevents overriding a method?
Choose the Swift code snippet that correctly prevents subclasses from overriding the method 'display()'.
Attempts:
2 left
💡 Hint
The 'final' keyword must come before 'func' in method declaration inside a class.
✗ Incorrect
Option A uses the correct syntax: 'final func' inside a class. Other options have syntax errors or misplaced keywords.
🚀 Application
expert3:00remaining
How many methods can be overridden?
Given the following Swift code, how many methods can subclasses override?
Swift
class Base { final func fixedMethod() {} func openMethod() {} private func privateMethod() {} fileprivate func fileMethod() {} }
Attempts:
2 left
💡 Hint
Consider method access levels and the effect of 'final'.
✗ Incorrect
Only 'openMethod()' and 'fileMethod()' can be overridden by subclasses in the same file. 'fixedMethod()' is final and cannot be overridden. 'privateMethod()' is not visible to subclasses.