Final test Flashcards

(25 cards)

1
Q

What is a class in Python?

A

It is a “blueprint” for creating objects, encapsulating data (attributes) and behavior (methods) into a single unit.

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

It initializes the attributes of an object when it is made.

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

What is an attribute?

A

It is a characteristic of an object.

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

What is a method?

A

It is a function that is associated with an object.

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

You call the class like a function.

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

You need to write the object’s name,then a dot, and after that, the name of the attribute you want to access.

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

First, an object of the class needs to be made. After that, the method is called by writing the object’s name with a dot, and then the method’s name, along with any needed arguments in parentheses.

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

You need the self keyword

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

You use self with a dot, and the method name.

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

What attributes does a Pet have?

A

self.name
self.energy
self.hunger
self.alive
self.x
self.y
self.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

True, 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? One should put the pet in its default location. The other should put it at an (x, y) location that is not the origin

A

Gary = pet(“Gary”)
Gary = (“pet”, “Gary”, 8, 2)

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 1 2 and 3

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

How do the turn methods ensure that the Pet’s direction attribute does not get assigned an invalid value?

A

It adds numbers until it gets to a specific number then it restarts the numbers.

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 its move() method is called?

A

1 pixel

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

How is the direction a Pet moves determined when its move() method is called?

A

By a keystroke

17
Q

Which methods modify the Pet’s energy attribute?

A

Eat, sleep, and play

18
Q

Which methods modify the Pet’s hunger attribute?

19
Q

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

A

It can be from 0 to any number for hunger and 0 to 10 for energy.

20
Q

How/where are guard clauses used to prevent the Pet from reaching invalid states?

A

It checks to see if the pet is dead then it stops the function if the pet is dead.

21
Q

In kill(), what are the meanings of self and other?

A

Self is the one doing the killing and other is the one being killed

22
Q

When should we put single blank lines in our code?

A

To separate things within a function

23
Q

When should we put double blank lines in our code?

A

To separate different functions

24
Q

What are the proper naming conventions for classes, variables, methods, and constants in Python?

A

Camel case for classes, snake_case for variables and methods, and constants in ALL_CAPS.

25
When naming classes, attributes, and methods, what parts of speech should we use? Why?
Nouns for classes, adjectives for attributes, and verbs for methods