Python OOP Course
- 30Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is a class in Python?
What is a class in Python?
30 questions
- 1
What is a class in Python?
- A
A type of loop
- B
A variable that stores multiple values
- C
A function that returns an object
- D
A blueprint for creating objects
Correct answer
DA blueprint for creating objects
Explanation
A class is a blueprint for creating instances (objects) with shared methods and attributes.
- A
- 2
Which keyword is used to define a class in Python?
- A
object - B
struct - C
class - D
def
Correct answer
CclassExplanation
The
classkeyword is used to define a class in Python. - A
- 3
What happens when you write
item1 = Item()?- A
An instance of the Item class is created
- B
The Item class is deleted
- C
The item1 variable is set to None
- D
A new method is defined
Correct answer
AAn instance of the Item class is created
Explanation
Item()calls the class constructor and creates a new instance (object) of the Item class. - A
- 4
How do you assign an attribute to an instance after creation?
- A
item1(name="Phone") - B
item1.name = "Phone" - C
name(item1, "Phone") - D
Item.name = "Phone"
Correct answer
Bitem1.name = "Phone"Explanation
Attributes are assigned using the dot notation:
instance.attribute = value. - A
- 5
What is the relationship between instance attributes and variables with a common prefix (e.g.,
item1_name,item1_price)?- A
Instance attributes can only be integers
- B
They are the same thing
- C
Instance attributes are explicitly related to one object, while separate variables are not
- D
Variables with prefix are more efficient
Correct answer
CInstance 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.
- A
- 6
What is the first parameter of every instance method in Python?
- A
this - B
cls - C
self - D
instance
Correct answer
CselfExplanation
By convention,
selfis the first parameter, representing the instance on which the method is called. - A
- 7
Why is the
selfparameter required in instance methods?- A
It must be used to access class variables
- B
Python automatically passes the instance as the first argument
- C
It is optional
- D
It defines the method as a class method
Correct answer
BPython 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.
- A
- 8
What is the purpose of the
__init__method in a class?- A
To initialize the object's attributes automatically when created
- B
To define class methods
- C
To print the object
- D
To delete the object
Correct answer
ATo initialize the object's attributes automatically when created
Explanation
__init__(the constructor) is called automatically when an instance is created, allowing attribute initialization. - A
- 9
What is the output if you do not include
selfas a parameter in an instance method?- A
The method becomes a static method
- B
The method works normally
- C
The instance is automatically passed as
self - D
Python raises a TypeError about missing positional arguments
Correct answer
DPython 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.
- A
- 10
Which of the following correctly defines an
__init__method?- A
def __init__(self, name): name = name - B
def __init__(name): self.name = name - C
def __init__(name): name = name - D
def __init__(self, name): self.name = name
Correct answer
Ddef __init__(self, name): self.name = nameExplanation
The
__init__method must acceptselfas the first parameter, then additional parameters. Attributes are assigned usingself.attribute = parameter. - A
- 11
How can you set a default value for a parameter in
__init__?- A
By using the
defaultkeyword - B
By assigning a value in the parameter list (e.g.,
quantity=0) - C
By using a conditional inside the method
- D
By defining the attribute before
__init__
Correct answer
BBy 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). - A
- 12
What is the difference between a class attribute and an instance attribute?
- A
Instance attributes are defined inside methods only
- B
Class attributes are mutable, instance attributes are immutable
- C
Class attributes can only be integers
- D
Class attributes are shared across all instances, instance attributes are unique per instance
Correct answer
DClass 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.
- A
- 13
If you access an attribute on an instance and it is not found there, what happens?
- A
Python looks for the attribute in the class
- B
Python assigns the attribute automatically
- C
Python returns None
- D
Python raises an AttributeError
Correct answer
APython 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.
- A
- 14
What is the purpose of using double underscores
__attributein Python?- A
It makes the attribute public
- B
It makes the attribute private (name mangling)
- C
It makes the attribute a property
- D
It converts the attribute to a class attribute
Correct answer
BIt 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.
- A
- 15
Which decorator is used to create a read-only property in Python?
- A
@staticmethod - B
@property - C
@classmethod - D
@readonly
Correct answer
B@propertyExplanation
The
@propertydecorator allows a method to be accessed like an attribute, and without a setter, it becomes read-only. - A
- 16
How do you create a setter for a property named
price?- A
@set_price - B
@price.setter - C
@price.setter()with parentheses - D
@price.set
Correct answer
B@price.setterExplanation
The setter decorator uses
@property_name.setterand defines a method with the same name. - A
- 17
What is encapsulation in OOP?
- A
Restricting direct access to some attributes and methods
- B
Allowing direct access to all attributes
- C
Using only static methods
- D
Making all methods public
Correct answer
ARestricting 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.
- A
- 18
Which decorator is used to define a class method?
- A
@instancemethod - B
@property - C
@staticmethod - D
@classmethod
Correct answer
D@classmethodExplanation
The
@classmethoddecorator indicates that the method receives the class (cls) as the first argument. - A
- 19
What is the first parameter of a class method?
- A
No parameter is required
- B
class - C
cls - D
self
Correct answer
CclsExplanation
Class methods receive the class itself as the first parameter, conventionally named
cls. - A
- 20
What is a common use case for a class method?
- A
To access instance attributes
- B
To instantiate objects from structured data (like CSV)
- C
To define utility functions
- D
To override operators
Correct answer
BTo instantiate objects from structured data (like CSV)
Explanation
Class methods are often used as alternative constructors, e.g.,
Item.instantiate_from_csv(). - A
- 21
What distinguishes a static method from a class method?
- A
Static methods do not receive any automatic first argument
- B
Static methods can access class attributes
- C
Static methods receive the instance as first argument
- D
Class methods are faster
Correct answer
AStatic methods do not receive any automatic first argument
Explanation
Static methods (with
@staticmethod) do not receiveselforclsautomatically; they behave like regular functions but belong to the class namespace. - A
- 22
Which of the following is an appropriate use of a static method?
- A
Creating a new instance from a CSV file
- B
Checking if a number is an integer
- C
Calculating total price for an instance
- D
Accessing class-level discount rate
Correct answer
BChecking 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). - A
- 23
In inheritance, what is the parent class?
- A
The class being inherited from
- B
The class that defines
__init__ - C
The class with the most methods
- D
The class that inherits from another
Correct answer
AThe class being inherited from
Explanation
The parent class (or base class) is the one that provides methods and attributes to its child classes.
- A
- 24
How do you call the parent class's
__init__method from a child class?- A
Parent.__init__(self, args) - B
self.__init__(args) - C
super(self).__init__(args) - D
super().__init__(args)
Correct answer
Dsuper().__init__(args)Explanation
super()returns a proxy object that allows calling methods of the parent class. The correct syntax issuper().__init__(args). - A
- 25
What is the effect of not calling
super().__init__()in a child class's constructor?- A
The child class cannot have its own attributes
- B
It raises an error automatically
- C
The parent class attributes and validations are not executed
- D
The child class becomes abstract
Correct answer
CThe 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. - A
- 26
What is polymorphism?
- A
The process of creating multiple classes
- B
The mechanism of hiding data
- C
The ability to use a single interface for different data types
- D
The ability to override methods
Correct answer
CThe 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.
- A
- 27
Which built-in function in Python is an example of polymorphism?
- A
print() - B
range() - C
type() - D
len()
Correct answer
Dlen()Explanation
len()works on strings, lists, etc., returning different results based on the object's type. - A
- 28
When a child class defines its own version of a method inherited from the parent, it is called:
- A
Overriding
- B
Overloading
- C
Encapsulation
- D
Overwriting
Correct answer
AOverriding
Explanation
Method overriding means redefining a method in a child class to provide specific behavior.
- A
- 29
Which OOP principle is demonstrated by using private attributes and providing public methods to access them?
- A
Polymorphism
- B
Inheritance
- C
Abstraction
- D
Encapsulation
Correct answer
DEncapsulation
Explanation
Encapsulation hides internal state and requires interaction through methods.
- A
- 30
What is abstraction in OOP?
- A
Hiding complex implementation details and showing only essential features
- B
Allowing a class to inherit from multiple classes
- C
Making all methods private
- D
Using only class methods
Correct answer
AHiding 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.
- A

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