Object Oriented Programming Flashcards

(39 cards)

1
Q

What is an object in OOP?

A

an entity or thing in your program

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

how do you create objects in Python?

A

Using classes. Classes define a type (not default types like str, int). Process of creating an object from that class is called instantiation.

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

True or False - Objects of the same class are independent of each other.

A

True. The class just tells python how to create the objects.

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

Write a class named Dog that has no input parameters and all it does is pass.

A
class Dog:
    pass
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are instance attributes?

A

Attributes that are independent to each object

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

What value do you pass in for ‘self’ when creating an object?

A

Nothing. Self is used to refer to the current object being created during instantiation.

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

What does self mean?

A

Take the value we are passing in and assign it to the attribute for the new instance.

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

What are class attributes?

A

Attributes that are the same for each instance of the class.

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

What is an instance method?

A

An instance method is a function that belongs to a class. Just like functions, these methods can accept parameters, and they can access the attributes of the object they belong to.

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

What is inheritance?

A

Inheritance is the OOP concept that allows one class to subclass another.

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

What is overwriting in Python when referring to class attributes?

A

Overwriting can allow for a class which inherits attributes from a parent, to overwrite one of the attributes.

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

Can you overwrite / add methods to children classes?

A

Yes.

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

What is the difference between dict['key'] and dict.get('key')?

A

dict[‘key’] raises a KeyError if the key doesn’t exist. dict.get(‘key’) returns None (or a default you specify) if the key doesn’t exist. Use .get() when the key might not be there.

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

When should you store objects in a dict instead of a list?

A

When you need to look up objects by a unique identifier. Dicts give O(1) constant-time lookup. Lists require scanning every item O(n). If you have patient_id and need fast retrieval: use a dict keyed by patient_id.

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

How do you store an object in a dict using one of its attributes as the key?

A

Access the attribute as the key: self.patients[patient.patient_id] = patient. Now you can retrieve it instantly with self.patients.get('P001').

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

How do you iterate over key-value pairs in a dict?

A

Use .items(): for key, value in my_dict.items():. Use .keys() for keys only. Use .values() for values only.

17
Q

How do you get a value from a dict with a custom default if the key is missing?

A

Pass the default as the second argument to .get(): my_dict.get('missing_key', 'default_value'). Returns ‘default_value’ instead of None if the key isn’t found.

18
Q

How do you check if a key exists in a dict?

A

Use the in operator: if 'patient_id' in my_dict:. Faster and more readable than calling .get() and checking for None.

19
Q

How do you remove a key-value pair from a dict?

A

Use del my_dict['key'] — raises KeyError if missing. Or use my_dict.pop('key', None) — returns None instead of crashing if the key doesn’t exist.

20
Q

What is a dict comprehension?

A

A concise way to build a dict: {patient.patient_id: patient for patient in patient_list}. Equivalent to a for loop that builds the dict but in one line.

21
Q

What is the time complexity of dict lookup vs list lookup?

A

Dict lookup is O(1) — constant time regardless of size. List lookup (scanning for a value) is O(n) — grows with the size of the list. Dicts are backed by a hash table.

22
Q

What is the difference between a class and an instance?

A

A class is a blueprint. An instance is an object created from that blueprint. class Patient: is the class. Patient('P001', 'Alice', 45) creates an instance.

23
Q

What does \_\_init\_\_ do and when does it run?

A

It runs automatically when you create an instance. It sets up the instance’s initial data (attributes). Think of it as the setup step that runs every time you build a new object.

24
Q

What is self in a Python class?

A

A reference to the specific instance the method is being called on. Always the first parameter in instance methods — Python passes it automatically. You never pass it yourself when calling the method.

25
How do you access an instance's own data inside a method?
Use `self.attribute_name`. For example: `self.patient_id` returns that specific instance's patient_id. Without `self` you'd be looking for a local variable instead.
26
What is the mutable default argument bug?
Using `[]` or `{}` as a default parameter creates ONE shared object for all calls. `def __init__(self, items=[])` means every instance shares the same list — add to one and it appears in all. Fix: assign `self.items = []` inside the body instead.
27
How do you correctly initialize a list attribute so each instance gets its own?
Assign it directly in the `__init__` body: `self.diagnoses = []`. Never use `def __init__(self, diagnoses=[])` — that default `[]` is shared across all instances.
28
What does `__repr__` do?
Defines what gets printed when you call `print(obj)` or `repr(obj)`. Should return a string that clearly identifies the object. Example: `return f'Patient(id={self.patient_id}, name={self.name})'
29
What is inheritance in Python?
A child class automatically gets all the attributes and methods of the parent class. You only write the parts that are new or different in the child.
30
What is the syntax to make a child class inherit from a parent?
Put the parent class name in parentheses: `class VIPPatient(Patient):`. VIPPatient now has everything Patient has.
31
What does `super()` do in Python?
Gives you access to the parent class from inside the child class. Most commonly used to call the parent's `__init__` without rewriting it: `super().__init__(arg1, arg2)`.
32
Why should you NOT pass `self` to `super().__init__()`?
super() already knows which instance you're working with. Passing `self` explicitly makes Python treat it as a regular argument — shifting all other args and causing a TypeError.
33
What is method overriding?
Defining a method in a child class with the same name as a parent method. The child's version runs instead of the parent's when called on a child instance.
34
When do you use `super().method()` vs rewriting a method entirely?
Use `super().method()` when the child version should do everything the parent does PLUS something extra. Rewrite entirely when the child version completely replaces the parent's logic.
35
What is the difference between a class variable and an instance variable?
A class variable is defined outside `__init__` and shared by ALL instances. An instance variable is defined with `self.` inside `__init__` and belongs to ONE specific instance.
36
What is the 'is-a vs has-a' rule in OOP?
Use inheritance (is-a) when the child IS a type of the parent: VIPPatient IS a Patient. Use composition (has-a) when one class CONTAINS another: WaitingRoom HAS patients — store them in a list inside the class instead of inheriting.
37
What is composition in OOP?
When a class holds instances of another class as attributes rather than inheriting from it. Example: `WaitingRoom` has `self.queue = []` which stores `Patient` objects. The room has patients; it is not a patient.
38
What happens when you call a method on a child class instance that was only defined in the parent?
Python finds it through the method resolution order (MRO) — it looks in the child class first, then walks up to the parent. If the child didn't override it, the parent's version runs.
39
What is `__init__` vs a regular method?
Both are methods. The difference: `__init__` is called automatically by Python when you create an instance with `ClassName(...)`. Regular methods are only called when you explicitly call them: `patient.greet()`.