OOP Flashcards

(17 cards)

1
Q

What is a constructor in Python?

A

A special method named __init__ that sets up the initial state of an object.

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

What’s the difference between public, private, and protected attributes?

A

Public: self.x – accessible anywhere

Protected: self._x – internal use, subclasses OK

Private: self.__x – not accessible outside class

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

What is inheritance?

A

A class (child) can inherit methods and properties from another class (parent).

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

What is polymorphism in OOP?

A

Different classes can define the same method, and the correct version is chosen at runtime.

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

What does super() do?

A

Calls the method of the superclass (usually the parent class version of a method).

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

What is an abstract class?

A

A class that cannot be instantiated and is meant to be inherited.

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

How do you force a subclass to implement a method?

A

Use @abstractmethod from the abc module.

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

What is composition?

A

A class contains another class (has-a relationship); the composed object is owned.

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

What does open(‘file.txt’, ‘r’) do?

A

Opens a file for reading.

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

What’s the difference between ‘w’ and ‘a’ modes?

A

‘w’ overwrites, ‘a’ appends.

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

What does assert do in testing?

A

Checks that an expression is True. If not, raises an AssertionError.

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

What is pytest used for?

A

Automated unit testing in Python.

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

What’s the structure of a try-except block?

A

try:
# risky code
except ErrorType:
# handle error

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

What does raise do?

A

Manually triggers an exception.

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

What is the ternary operator syntax?

A

x = “yes” if condition else “no”

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

What does isinstance(obj, Class) do?

A

Returns True if obj is an instance of Class.

17
Q

What is a Python property?

A

A way to use getters and setters like attributes with @property.