POC Object-Oriented Programming in Python 1 — Questions and Answers
Question 1: What is a class in Python?
- A function
- A data type
- A blueprint for creating objects (Correct answer)
- A conditional statement
Correct answer: A blueprint for creating objects
In Object-Oriented Programming (OOP) in Python, a class serves as a blueprint or a template for creating objects. It defines a set of attributes (data) and methods (functions) that the objects created from the class will possess. Objects are instances of a class, embodying the structure and behavior defined by the class.
Question 2: Which keyword is used to define a class in Python?
- object
- class (Correct answer)
- define
- method
Correct answer: class
The `class` keyword is used in Python to define a new class. It is followed by the class name and a colon, indicating the start of the class's definition block. This keyword is fundamental for implementing Object-Oriented Programming principles and structuring code into reusable components.
Question 3: What is the purpose of the __init__ method?
- To delete an object
- To define class variables
- To initialize the object (Correct answer)
- To call the superclass
Correct answer: To initialize the object
The `__init__` method is a special constructor method in Python classes. It is automatically called when a new object (instance) of the class is created. Its primary purpose is to initialize the object's attributes with the values passed during object creation, setting up the initial state of the instance.
Question 4: How do you access attributes of an object?
- object->attribute
- object:attribute
- object.attribute (Correct answer)
- object/attribute
Correct answer: object.attribute
In Python, you access attributes (variables) and methods (functions) of an object using dot notation. The syntax `object.attribute` allows you to retrieve or modify the value of a specific attribute belonging to that object. This is the standard way to interact with an object's members.
Question 5: What is inheritance in Python?
- Using functions within functions
- Extending a class to reuse functionality (Correct answer)
- Overloading operators
- Calling functions from modules
Correct answer: Extending a class to reuse functionality
Inheritance is a core concept in Object-Oriented Programming where a new class (subclass/derived class) can inherit attributes and methods from an existing class (superclass/base class). This promotes code reusability and establishes an "is-a" relationship between classes, allowing the subclass to extend or specialize the functionality of the parent.
Question 6: What does 'self' refer to in a class method?
- The class name
- A global variable
- The current object (Correct answer)
- A reserved keyword
Correct answer: The current object
In Python class methods, `self` is a conventional parameter that refers to the instance of the class itself. It allows methods to access and operate on the object's attributes and other methods. When a method is called on an object, Python automatically passes that object as the first argument to `self`.
Question 7: What is polymorphism in OOP?
- A type of error handling
- Writing code that can work on different types of objects (Correct answer)
- Using one method only
- Defining multiple classes
Correct answer: Writing code that can work on different types of objects
Polymorphism, meaning "many forms," is an OOP concept that allows objects of different classes to be treated as objects of a common base class. This enables a single interface to be used for different data types, allowing functions or methods to operate on objects of various types in a uniform way, promoting flexibility and extensibility.
Question 8: Which method is used to represent an object as a string?
- __repr__
- __str__ (Correct answer)
- __show__
- __display__
Correct answer: __str__
The `__str__` method in Python is a special method used to define the "informal" or "user-friendly" string representation of an object. When you use `print()` on an object or `str()` to convert it to a string, Python calls its `__str__` method. This makes objects more readable when displayed to users.
Question 9: Which concept allows redefining a method in a derived class?
- Encapsulation
- Inheritance
- Polymorphism
- Overriding (Correct answer)
Correct answer: Overriding
Method overriding is an OOP concept where a subclass provides a specific implementation for a method that is already defined in its superclass. This allows the subclass to change or extend the behavior of the inherited method, providing specialized functionality while maintaining the same method signature. It's crucial for achieving polymorphism.
What is a class in Python?