POC Object-Oriented Programming in Python 2 — Questions and Answers
Question 1: What is the purpose of the `__str__` method in a Python class?
- To compare two objects for equality
- To return a human-readable string representation of an object (Correct answer)
- To initialize object attributes
- To delete an object from memory
Correct answer: To return a human-readable string representation of an object
`__str__` returns a user-friendly string representation used by `print()` and `str()` functions.
Question 2: Which keyword is used to inherit from a parent class in Python?
- extends
- implements
- inherits
- Parentheses with the parent class name in the class definition (Correct answer)
Correct answer: Parentheses with the parent class name in the class definition
Python uses parentheses in the class definition (e.g., `class Child(Parent):`) to indicate inheritance.
Question 3: What does `super().__init__()` do in a child class?
- Creates a new parent class instance
- Calls the parent class's `__init__` method to initialize inherited attributes (Correct answer)
- Overrides the parent constructor entirely
- Deletes the child class constructor
Correct answer: Calls the parent class's `__init__` method to initialize inherited attributes
`super().__init__()` invokes the parent class constructor so inherited attributes are properly set up.
Question 4: Which of the following best describes method overriding?
- Defining a method with the same name in a child class to replace the parent's implementation (Correct answer)
- Adding extra parameters to an inherited method
- Calling a parent method from inside the child class
- Creating two methods with the same name in the same class
Correct answer: Defining a method with the same name in a child class to replace the parent's implementation
Method overriding allows a subclass to provide a specific implementation for a method already defined in its parent class.
Question 5: What will `isinstance(obj, MyClass)` return if `obj` is an instance of a subclass of `MyClass`?
- False
- True (Correct answer)
- None
- It raises a TypeError
Correct answer: True
`isinstance()` returns `True` for instances of the class and all its subclasses.
Question 6: Which access modifier convention in Python indicates a method is intended for internal use only?
- A double underscore prefix (name mangling)
- A single underscore prefix (Correct answer)
- The `private` keyword
- Enclosing the name in square brackets
Correct answer: A single underscore prefix
A single leading underscore (e.g., `_method`) is a convention signaling the method is intended for internal/protected use.
Question 7: What is name mangling in Python?
- Automatically renaming variables during garbage collection
- Transforming `__attr` to `_ClassName__attr` to avoid name conflicts in subclasses (Correct answer)
- Encrypting attribute names for security
- Converting attribute names to lowercase
Correct answer: Transforming `__attr` to `_ClassName__attr` to avoid name conflicts in subclasses
Name mangling transforms double-underscore prefixed names (e.g., `__x`) to `_ClassName__x` to prevent accidental override in subclasses.
What is the purpose of the `__str__` method in a Python class?