1. Home
  2. Explore
  3. Computer Science
  4. Python OOP Course

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Python OOP Course

  • 30Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Classes and Objects

What is a class in Python?

30 questions

  1. 1

    What is a class in Python?

    1. A

      A type of loop

    2. B

      A variable that stores multiple values

    3. C

      A function that returns an object

    4. D

      A blueprint for creating objects

    Correct answer
    D

    A blueprint for creating objects

    Explanation

    A class is a blueprint for creating instances (objects) with shared methods and attributes.

  2. 2

    Which keyword is used to define a class in Python?

    1. A

      object

    2. B

      struct

    3. C

      class

    4. D

      def

    Correct answer
    C

    class

    Explanation

    The class keyword is used to define a class in Python.

  3. 3

    What happens when you write item1 = Item()?

    1. A

      An instance of the Item class is created

    2. B

      The Item class is deleted

    3. C

      The item1 variable is set to None

    4. D

      A new method is defined

    Correct answer
    A

    An instance of the Item class is created

    Explanation

    Item() calls the class constructor and creates a new instance (object) of the Item class.

  4. 4

    How do you assign an attribute to an instance after creation?

    1. A

      item1(name="Phone")

    2. B

      item1.name = "Phone"

    3. C

      name(item1, "Phone")

    4. D

      Item.name = "Phone"

    Correct answer
    B

    item1.name = "Phone"

    Explanation

    Attributes are assigned using the dot notation: instance.attribute = value.

  5. 5

    What is the relationship between instance attributes and variables with a common prefix (e.g., item1_name, item1_price)?

    1. A

      Instance attributes can only be integers

    2. B

      They are the same thing

    3. C

      Instance attributes are explicitly related to one object, while separate variables are not

    4. D

      Variables with prefix are more efficient

    Correct answer
    C

    Instance attributes are explicitly related to one object, while separate variables are not

    Explanation

    Instance attributes belong to a specific object, whereas separate variables are unrelated even if they share a naming convention.

  6. 6

    What is the first parameter of every instance method in Python?

    1. A

      this

    2. B

      cls

    3. C

      self

    4. D

      instance

    Correct answer
    C

    self

    Explanation

    By convention, self is the first parameter, representing the instance on which the method is called.

  7. 7

    Why is the self parameter required in instance methods?

    1. A

      It must be used to access class variables

    2. B

      Python automatically passes the instance as the first argument

    3. C

      It is optional

    4. D

      It defines the method as a class method

    Correct answer
    B

    Python automatically passes the instance as the first argument

    Explanation

    When calling a method on an instance, Python automatically passes the instance as the first argument, so the method must have a parameter to receive it.

  8. 8

    What is the purpose of the __init__ method in a class?

    1. A

      To initialize the object's attributes automatically when created

    2. B

      To define class methods

    3. C

      To print the object

    4. D

      To delete the object

    Correct answer
    A

    To initialize the object's attributes automatically when created

    Explanation

    __init__ (the constructor) is called automatically when an instance is created, allowing attribute initialization.

  9. 9

    What is the output if you do not include self as a parameter in an instance method?

    1. A

      The method becomes a static method

    2. B

      The method works normally

    3. C

      The instance is automatically passed as self

    4. D

      Python raises a TypeError about missing positional arguments

    Correct answer
    D

    Python raises a TypeError about missing positional arguments

    Explanation

    Python always passes the instance as the first argument, so the method must accept at least one parameter; otherwise, a TypeError occurs.

  10. 10

    Which of the following correctly defines an __init__ method?

    1. A

      def __init__(self, name): name = name

    2. B

      def __init__(name): self.name = name

    3. C

      def __init__(name): name = name

    4. D

      def __init__(self, name): self.name = name

    Correct answer
    D

    def __init__(self, name): self.name = name

    Explanation

    The __init__ method must accept self as the first parameter, then additional parameters. Attributes are assigned using self.attribute = parameter.

  11. 11

    How can you set a default value for a parameter in __init__?

    1. A

      By using the default keyword

    2. B

      By assigning a value in the parameter list (e.g., quantity=0)

    3. C

      By using a conditional inside the method

    4. D

      By defining the attribute before __init__

    Correct answer
    B

    By assigning a value in the parameter list (e.g., quantity=0)

    Explanation

    Default values are provided in the function signature: def __init__(self, quantity=0).

  12. 12

    What is the difference between a class attribute and an instance attribute?

    1. A

      Instance attributes are defined inside methods only

    2. B

      Class attributes are mutable, instance attributes are immutable

    3. C

      Class attributes can only be integers

    4. D

      Class attributes are shared across all instances, instance attributes are unique per instance

    Correct answer
    D

    Class attributes are shared across all instances, instance attributes are unique per instance

    Explanation

    Class attributes belong to the class and are shared by all instances; instance attributes are unique to each instance.

  13. 13

    If you access an attribute on an instance and it is not found there, what happens?

    1. A

      Python looks for the attribute in the class

    2. B

      Python assigns the attribute automatically

    3. C

      Python returns None

    4. D

      Python raises an AttributeError

    Correct answer
    A

    Python looks for the attribute in the class

    Explanation

    When an attribute is not found at the instance level, Python checks the class (and its parent classes) for the attribute.

  14. 14

    What is the purpose of using double underscores __attribute in Python?

    1. A

      It makes the attribute public

    2. B

      It makes the attribute private (name mangling)

    3. C

      It makes the attribute a property

    4. D

      It converts the attribute to a class attribute

    Correct answer
    B

    It makes the attribute private (name mangling)

    Explanation

    Double underscores trigger name mangling, making the attribute harder to access from outside the class, simulating private attributes.

  15. 15

    Which decorator is used to create a read-only property in Python?

    1. A

      @staticmethod

    2. B

      @property

    3. C

      @classmethod

    4. D

      @readonly

    Correct answer
    B

    @property

    Explanation

    The @property decorator allows a method to be accessed like an attribute, and without a setter, it becomes read-only.

  16. 16

    How do you create a setter for a property named price?

    1. A

      @set_price

    2. B

      @price.setter

    3. C

      @price.setter() with parentheses

    4. D

      @price.set

    Correct answer
    B

    @price.setter

    Explanation

    The setter decorator uses @property_name.setter and defines a method with the same name.

  17. 17

    What is encapsulation in OOP?

    1. A

      Restricting direct access to some attributes and methods

    2. B

      Allowing direct access to all attributes

    3. C

      Using only static methods

    4. D

      Making all methods public

    Correct answer
    A

    Restricting direct access to some attributes and methods

    Explanation

    Encapsulation restricts direct access to an object's internal state and requires methods to interact with it, promoting data integrity.

  18. 18

    Which decorator is used to define a class method?

    1. A

      @instancemethod

    2. B

      @property

    3. C

      @staticmethod

    4. D

      @classmethod

    Correct answer
    D

    @classmethod

    Explanation

    The @classmethod decorator indicates that the method receives the class (cls) as the first argument.

  19. 19

    What is the first parameter of a class method?

    1. A

      No parameter is required

    2. B

      class

    3. C

      cls

    4. D

      self

    Correct answer
    C

    cls

    Explanation

    Class methods receive the class itself as the first parameter, conventionally named cls.

  20. 20

    What is a common use case for a class method?

    1. A

      To access instance attributes

    2. B

      To instantiate objects from structured data (like CSV)

    3. C

      To define utility functions

    4. D

      To override operators

    Correct answer
    B

    To instantiate objects from structured data (like CSV)

    Explanation

    Class methods are often used as alternative constructors, e.g., Item.instantiate_from_csv().

  21. 21

    What distinguishes a static method from a class method?

    1. A

      Static methods do not receive any automatic first argument

    2. B

      Static methods can access class attributes

    3. C

      Static methods receive the instance as first argument

    4. D

      Class methods are faster

    Correct answer
    A

    Static methods do not receive any automatic first argument

    Explanation

    Static methods (with @staticmethod) do not receive self or cls automatically; they behave like regular functions but belong to the class namespace.

  22. 22

    Which of the following is an appropriate use of a static method?

    1. A

      Creating a new instance from a CSV file

    2. B

      Checking if a number is an integer

    3. C

      Calculating total price for an instance

    4. D

      Accessing class-level discount rate

    Correct answer
    B

    Checking if a number is an integer

    Explanation

    Static methods are used for utility functions that are related to the class but don't need instance or class data, like is_integer(num).

  23. 23

    In inheritance, what is the parent class?

    1. A

      The class being inherited from

    2. B

      The class that defines __init__

    3. C

      The class with the most methods

    4. D

      The class that inherits from another

    Correct answer
    A

    The class being inherited from

    Explanation

    The parent class (or base class) is the one that provides methods and attributes to its child classes.

  24. 24

    How do you call the parent class's __init__ method from a child class?

    1. A

      Parent.__init__(self, args)

    2. B

      self.__init__(args)

    3. C

      super(self).__init__(args)

    4. D

      super().__init__(args)

    Correct answer
    D

    super().__init__(args)

    Explanation

    super() returns a proxy object that allows calling methods of the parent class. The correct syntax is super().__init__(args).

  25. 25

    What is the effect of not calling super().__init__() in a child class's constructor?

    1. A

      The child class cannot have its own attributes

    2. B

      It raises an error automatically

    3. C

      The parent class attributes and validations are not executed

    4. D

      The child class becomes abstract

    Correct answer
    C

    The parent class attributes and validations are not executed

    Explanation

    Without calling super().__init__(), the parent's initialization code (like validation and attribute assignment) is skipped.

  26. 26

    What is polymorphism?

    1. A

      The process of creating multiple classes

    2. B

      The mechanism of hiding data

    3. C

      The ability to use a single interface for different data types

    4. D

      The ability to override methods

    Correct answer
    C

    The ability to use a single interface for different data types

    Explanation

    Polymorphism allows a single entity (e.g., a method) to work with objects of different classes in a way appropriate to each class.

  27. 27

    Which built-in function in Python is an example of polymorphism?

    1. A

      print()

    2. B

      range()

    3. C

      type()

    4. D

      len()

    Correct answer
    D

    len()

    Explanation

    len() works on strings, lists, etc., returning different results based on the object's type.

  28. 28

    When a child class defines its own version of a method inherited from the parent, it is called:

    1. A

      Overriding

    2. B

      Overloading

    3. C

      Encapsulation

    4. D

      Overwriting

    Correct answer
    A

    Overriding

    Explanation

    Method overriding means redefining a method in a child class to provide specific behavior.

  29. 29

    Which OOP principle is demonstrated by using private attributes and providing public methods to access them?

    1. A

      Polymorphism

    2. B

      Inheritance

    3. C

      Abstraction

    4. D

      Encapsulation

    Correct answer
    D

    Encapsulation

    Explanation

    Encapsulation hides internal state and requires interaction through methods.

  30. 30

    What is abstraction in OOP?

    1. A

      Hiding complex implementation details and showing only essential features

    2. B

      Allowing a class to inherit from multiple classes

    3. C

      Making all methods private

    4. D

      Using only class methods

    Correct answer
    A

    Hiding complex implementation details and showing only essential features

    Explanation

    Abstraction focuses on what an object does rather than how it does it, hiding unnecessary details.

Question 1 of 30
Question 1 of 30

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent