โ† All POC Flashcard Decks

Object-Oriented Programming in Python Flashcards

9 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 9 Object-Oriented Programming in Python flashcards as text
  1. What is a class in Python?

    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.

  2. Which keyword is used to define a class in Python?

    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.

  3. What is the purpose of the __init__ method?

    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.

  4. How do you access attributes of an object?

    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.

  5. What is inheritance in Python?

    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.

  6. What does 'self' refer to in a class method?

    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`.

  7. What is polymorphism in OOP?

    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.

  8. Which method is used to represent an object as a string?

    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.

  9. Which concept allows redefining a method in a derived class?

    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.