Classes Flashcards

(30 cards)

1
Q

What is a class in Python?

A

A class in Python is a blueprint for creating objects that encapsulate data and functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What keyword is used to define a class in Python?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is an instance variable?

A

An instance variable is a variable that is defined inside a class and is tied to the instance of that class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a constructor in Python?

A

A constructor in Python is a special method called ‘__init__’ that initializes an object’s attributes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

True or False: The ‘self’ keyword refers to the instance of the class.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Fill in the blank: In Python, the method used to initialize an object’s attributes is called ______.

A

__init__

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the purpose of the ‘self’ parameter in methods?

A

The ‘self’ parameter allows access to instance variables and methods within the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the difference between instance variables and class variables?

A

Instance variables are unique to each instance, while class variables are shared across all instances of the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What type of method is defined with ‘def’ inside a class?

A

A method is defined with ‘def’ and can be an instance method, class method, or static method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

True or False: You must always include ‘self’ as the first parameter in instance methods.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is an attribute function in a Python class?

A

An attribute function is a method that retrieves or modifies an instance variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the ‘pass’ statement do in a class definition?

A

The ‘pass’ statement serves as a placeholder, indicating that no action is to be taken.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Fill in the blank: To create a class method, you use the ______ decorator.

A

@classmethod

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

The __str__ method defines the string representation of an object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What do you call a method that modifies the class state rather than instance state?

A

A class method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

True or False: A static method can modify class or instance state.

17
Q

What is the output of print(type(MyClass)) if MyClass is a defined class?

A

<class ‘type’>

18
Q

What is the purpose of the __repr__ method?

A

The __repr__ method provides an unambiguous string representation of an object for debugging.

19
Q

Fill in the blank: Instance variables are defined within the ______ method.

20
Q

How do you access an instance variable from outside the class?

A

By using the syntax instance_name.variable_name.

21
Q

What does the keyword ‘self’ represent in a method?

A

‘self’ represents the instance of the class upon which the method is being called.

22
Q

What is the difference between a static method and a class method?

A

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.

23
Q

True or False: You can have multiple constructors in a Python class.

24
Q

What is the syntax for creating an instance of a class?

A

instance_name = ClassName()

25
Fill in the blank: Class variables are defined outside of any methods, usually at the ______ of the class.
top
26
What is the primary purpose of using classes in Python?
The primary purpose is to bundle data and functionality together, enabling better organization and code reuse.
27
What does it mean to 'instantiate' a class?
To instantiate a class means to create an object from that class.
28
What happens if you do not define an __init__ method in a class?
If you do not define an __init__ method, Python provides a default constructor that does nothing.
29
Fill in the blank: In a class, methods can be defined to perform operations on ______.
instance variables
30
What is the role of the __del__ method in a class?
The __del__ method is called when an object is about to be destroyed, allowing for cleanup actions.