Object-Oriented Programming in Python Flashcards
7 cards from real POC practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Object-Oriented Programming in Python flashcards as text
What is multiple inheritance in Python?
Answer: A class inheriting attributes and methods from more than one parent class
Multiple inheritance allows a class to inherit from several parent classes, e.g., `class C(A, B):`.
What is the MRO (Method Resolution Order) used for?
Answer: Determining the order in which base classes are searched when looking up a method
MRO defines the lookup order for methods and attributes in class hierarchies, especially important in multiple inheritance.
What does the `@property` decorator enable?
Answer: Accessing a method as if it were an attribute (getter)
`@property` lets you define a method that is accessed like an attribute, enabling controlled access to private data.
Which of the following correctly defines a class method?
Answer: @classmethod def my_method(cls):
A class method is decorated with `@classmethod` and takes `cls` as its first parameter, referring to the class itself.
What is encapsulation in OOP?
Answer: Bundling data and methods that operate on that data within a class, restricting direct outside access
Encapsulation hides internal state and exposes only necessary interfaces, protecting object integrity.
What is the output of the following code? ``` class Dog: sound = 'Woof' d = Dog() d.sound = 'Bark' print(Dog.sound) ```
Answer: Woof
Assigning `d.sound = 'Bark'` creates an instance attribute on `d`, leaving the class attribute `Dog.sound` unchanged as 'Woof'.
Which built-in function returns all attributes and methods of an object?
Answer: dir()
`dir(obj)` returns a list of all attributes and methods available on the object, including inherited ones.