(Ch.8 question) Questions and Answers
Already Passed
What is an object in Python?
A variable
A function
✔✔An instance of a class
A data type
What is the purpose of a constructor in a class?
To define the methods
✔✔To initialize the attributes of an object
To delete the attributes of an object
To create a copy of an object
Which keyword is used to create a class in Python?
function
✔✔class
1
,define
object
What is the special method `__str__` used for?
✔✔To return a string representation of an object
To initialize an object
To compare two objects
To delete an object
What does encapsulation in object-oriented programming provide?
✔✔Data hiding
Code repetition
Improved runtime
Direct access to attributes
How do you call a method of an object in Python?
Using the class name directly
✔✔Using the object name followed by the method name
2
, Using square brackets
Using the `call` keyword
What is the difference between a class and an object?
A class is a blueprint, and an object is an instance of the class.
An object defines attributes, while a class does not.
✔✔A class is a template, and an object is the actual entity created from that template.
An object is a method, while a class is a variable.
What does inheritance allow in Python?
✔✔One class to derive attributes and methods from another
A function to override a class
Multiple objects to be combined
None of the above
How do you define an attribute as private in Python?
Using parentheses around the name
Using a colon after the name
3