OOP Flashcards

(21 cards)

1
Q

What is a class in Python?

A

A class is a blueprint for creating objects. It defines attributes (data) and methods (functions) that the objects created from it will have.

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

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

A

__init__ is a special method that gets called automatically when a new object is created. It initializes the object’s attributes.

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

What is an attribute?

A

An attribute is a variable that belongs to an object (e.g., self.name, self.energy).

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

What is a method?

A

A method is a function that belongs to a class and can operate on objects of that class (e.g., move(), eat()).

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

How do you create an instance of a class?

A

By calling the class like a function: my_pet = Pet(“jimmy”).

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

How do you access an object’s attributes from outside the class using dot notation?

A

my_pet.name or my_pet.energy.

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

How do you call an object’s methods from outside the class using dot notation?

A

my_pet.move() or my_pet.sleep(2).

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

How do you access an object’s attributes from inside the class using dot notation?

A

Use self.attribute_name, like self.energy.

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

How do you call an object’s methods from inside the class using dot notation?

A

Use self.method_name(), like self.die().

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

What attributes does a Pet have?

A

name, x, y, energy, hunger, alive, direction

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

What are the default values for each attribute?

A

x = 0, y = 0, energy = 5, hunger = 5, alive = True, direction = 0

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

What are two ways to create a Pet?

A

pet1 = Pet(“jimmy”)
# Default location (0,0)
pet2 = Pet(“someone”, 5, 7)
# Custom location (5,7)

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

How are pet directions represented internally?

A

0 = up, 1 = right, 2 = down, 3 = left

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

How do the turn methods ensure that direction stays valid?

A

urn_right resets direction to 0 if it becomes 4.

turn_left resets direction to 3 if it becomes -1.

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

How far does a Pet move each time move() is called?

A

One unit in the direction it is facing.

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

How is the direction of movement determined in move()?

A

By checking the direction attribute and updating x or y accordingly.

17
Q

Which methods modify the Pet’s energy?

A

sleep() (increases)

play() (decreases)

18
Q

Which methods modify the Pet’s hunger?

A

eat() (decreases)

play() (increases)

19
Q

What is the range of possible values for energy and hunger?

A

energy: 0 to 10 (capped in sleep())

hunger: no explicit max/min, but implied 0 minimum in eat()

20
Q

How/where are guard clauses used to prevent invalid states?

A

In eat(), sleep(), play(), and kill(): checks if self.alive or other.alive is False.

21
Q

In kill(), what do self and other mean?

A

self: the Pet doing the attacking

other: the Pet being attacked