What is a class in Python?
A class in Python is a blueprint for creating objects that encapsulate data and functions.
What keyword is used to define a class in Python?
The keyword ‘class’ is used to define a class in Python.
What is an instance variable?
An instance variable is a variable that is defined inside a class and is tied to the instance of that class.
What is a constructor in Python?
A constructor in Python is a special method called ‘__init__’ that initializes an object’s attributes.
True or False: The ‘self’ keyword refers to the instance of the class.
True
Fill in the blank: In Python, the method used to initialize an object’s attributes is called ______.
__init__
What is the purpose of the ‘self’ parameter in methods?
The ‘self’ parameter allows access to instance variables and methods within the class.
What is the difference between instance variables and class variables?
Instance variables are unique to each instance, while class variables are shared across all instances of the class.
What type of method is defined with ‘def’ inside a class?
A method is defined with ‘def’ and can be an instance method, class method, or static method.
True or False: You must always include ‘self’ as the first parameter in instance methods.
True
What is an attribute function in a Python class?
An attribute function is a method that retrieves or modifies an instance variable.
What does the ‘pass’ statement do in a class definition?
The ‘pass’ statement serves as a placeholder, indicating that no action is to be taken.
Fill in the blank: To create a class method, you use the ______ decorator.
@classmethod
What is the purpose of the __str__ method in a class?
The __str__ method defines the string representation of an object.
What do you call a method that modifies the class state rather than instance state?
A class method.
True or False: A static method can modify class or instance state.
False
What is the output of print(type(MyClass)) if MyClass is a defined class?
<class ‘type’>
What is the purpose of the __repr__ method?
The __repr__ method provides an unambiguous string representation of an object for debugging.
Fill in the blank: Instance variables are defined within the ______ method.
__init__
How do you access an instance variable from outside the class?
By using the syntax instance_name.variable_name.
What does the keyword ‘self’ represent in a method?
‘self’ represents the instance of the class upon which the method is being called.
What is the difference between a static method and a class method?
A class method takes ‘cls’ as its first parameter and can access class variables, while a static method does not take ‘self’ or ‘cls’ and cannot access instance or class variables.
True or False: You can have multiple constructors in a Python class.
False
What is the syntax for creating an instance of a class?
instance_name = ClassName()