Understanding Class Definition
In Python, which statement correctly defines a class named Vehicle with no methods or attributes?
- def class Vehicle(): pass
- Vehicle class: pass
- class Vehicle: pass
- define Vehicle: pass
- Vehicle(): class pass
Object Instantiation
Given the class definition 'class Gadget:', which of the following methods correctly creates an object of this class?
- device = Gadget
- device = Gadget.new()
- device = new Gadget()
- device = Gadget(object)
- device = Gadget()
Encapsulation with Private Attributes
How can you declare an attribute as private so that it cannot be directly accessed from outside a class called Account?
- self.balance = 0
- self._balance = 0
- self.__balance = 0
- self::balance = 0
- self.private balance = 0
Multiple Inheritance Resolution Order
Given classes X, Y, Z and class W(X, Y, Z), what determines the method resolution order (MRO) when invoking W's methods?
- C3 linearization
- Post-order traversal
- Depth-first traversal
- Zigzag pattern
- BFS Algorithm
Polymorphism and Method Overriding
If a subclass implements a method with the same name as its superclass, what feature is demonstrated when the subclass's method is called instead?
- Method overriding
- Method shadowing
- Data wrapping
- Function hiding
- Method overbidding
Abstract Base Classes
What is required for a class in Python to be considered an abstract class?
- It must use the 'abstract' keyword in its declaration.
- It must define methods prefixed with 'abs_'.
- It must have only static methods.
- It must inherit from object and contain virtual methods.
- It must inherit from abc.ABC and have at least one @abstractmethod.
Operator Overloading with Special Methods
Which special method in a class allows overloading the addition operator ('+') for custom objects?
- __add__
- __concat__
- __append__
- __sum__
- __plus__
Using the @property Decorator
How does the @property decorator enhance attribute access in Python classes?
- It allows methods to be accessed like attributes.
- It turns class methods into instance methods.
- It automatically creates class-level variables.
- It hides all attributes from subclasses.
- It makes attributes static and read-only.
Name Mangling for Double Underscore Attributes
When an attribute is named with double leading underscores (e.g., __value), how does Python handle it internally?
- It completely restricts access from outside the class.
- It converts underscores to hyphens in the name.
- It encrypts the attribute automatically.
- It throws an AttributeError during runtime.
- It performs name mangling by prefixing with _ClassName.
Static Methods versus Class Methods
Which of the following statements best differentiates a static method from a class method in Python?
- A static method can access instance variables directly, but a class method cannot.
- A class method can only be called from outside the class.
- Both types of methods are identical in behavior.
- A static method receives the class as the first argument, while a class method does not.
- A class method receives the class as its first argument, while a static method does not receive any implicit first argument.
Diamond Problem in Multiple Inheritance
In multiple inheritance, what is the diamond problem, and how does Python resolve it?
- It occurs when two base classes share a common ancestor; Python resolves it using the MRO.
- It refers to circular imports; Python resolves it with sys.path manipulation.
- It is caused by conflicting variable names; Python throws a RuntimeError.
- It arises from private inheritance; Python ignores duplicated methods.
- It happens if a class inherits from itself; Python stops compilation.
Duck Typing in Polymorphism
Given two classes, both defining a method named 'quack', which concept allows using them interchangeably in a function expecting 'quackable' objects?
- Protocol subclassing
- Type checking
- Single dispatch
- Duck typing
- Explicit interface
Calling Superclass Methods
Which syntax correctly invokes a method called 'move' from a superclass in a subclass's method?
- this.move()
- self.super(move)
- super().move()
- base.move(self)
- superclass:move()
Abstract Method Enforcement
What happens if a subclass does not implement all abstract methods of its abstract base class when instantiated?
- The missing methods default to pass.
- The class instance is silently created.
- A TypeError is raised during instantiation.
- An ImportError is thrown.
- A warning is issued at runtime.
Operator Overloading for Equality
Which special method should be defined to overload the equality operator '==' for a custom class?
- __equal__
- __equals__
- __isequal__
- __eq__
- __equiv__