Duck Typing, ABCs & Protocols — Concept Check
Questions on Duck Typing, ABCs & Protocols. Python trusts behaviour over declared types — "if it quacks, it's a duck". ABCs enforce interfaces at instantiation; Protocols (3.8+) bring static duck typing for type checkers.
Questions in this quiz
8 questions on Duck Typing, ABCs & Protocols — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer tries to instantiate a class that inherits from an ABC but hasn't implemented all abstract methods. What will happen?
- AThe instantiation will succeed but methods will raise NotImplementedError.
- BThe instantiation will succeed with default implementations.
- CThe instantiation will succeed if methods are defined later.
- DThe instantiation will fail with a TypeError.
2.You have a class `EmailNotifier` with a `send` method. If you pass it to a function expecting a `Notifier` Protocol, what will happen?
- AThe function will fail because `EmailNotifier` doesn't inherit from `Notifier`.
- BThe function will work if `send` matches the Protocol signature.
- CThe function will work only if `EmailNotifier` registers with `Notifier`.
- DThe function will fail unless `EmailNotifier` explicitly declares it implements `Notifier`.
3.A team uses duck typing for a payment system. They replace a `Card` class with a new `Wallet` class. What must they ensure for compatibility?
- A`Wallet` must register with a payment Protocol.
- B`Wallet` must inherit from `Card`.
- C`Wallet` must have a `pay` method.
- D`Wallet` must implement all Card methods.
4.A developer mistakenly believes that using an ABC is necessary for all interface-like structures in Python. Which of the following is true?
- AABCs are the only way to enforce method presence.
- BABCs and Protocols serve the same purpose.
- CProtocols require classes to inherit from them.
- DProtocols can be used without ABCs for static type checking.
5.A function is typed to accept a `Notifier` Protocol. What happens if you pass a string to it?
- AThe function will accept the string if it has a `send` method.
- BThe function will ignore the type and execute normally.
- CThe function will execute and fail at runtime.
- DThe type checker will raise an error due to missing `send` method.
6.Why might using duck typing be preferred over explicitly defined interfaces in Python?
- AIt requires less memory than interfaces.
- BIt is a more secure way to handle method calls.
- CIt allows flexibility by not enforcing method presence.
- DIt ensures better runtime performance.
7.A developer believes that Protocols require inheritance like ABCs. What is the correct understanding?
- AProtocols require explicit inheritance for method checks.
- BInheritance is optional but recommended for Protocols.
- CProtocols check for method presence without requiring inheritance.
- DProtocols automatically register classes with matching methods.
8.In a Python project, a class `Broken` inherits from an ABC but doesn't implement all methods. What is the result?
- AInstantiation of `Broken` will raise a TypeError.
- B`Broken` will instantiate but raise errors on method calls.
- C`Broken` will work if methods are implemented later.
- D`Broken` will inherit default implementations for missing methods.