08 Classes Flashcards

1
Q

What is object-oriented programming (OOP)?

A

It’s a way of programming based on the concept of objects. That increases reusability and modularity.

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

In Python, what is an object?

A

A combination of variables, functions and data structures. It can contain data (a state) and methods (behavior).

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

What are attributes in a Python class?

A

They describe a state of an object, i.e. the size, the amount, the colour,…

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

What are methods in a Python class?

A

They describe the behavior of an object, i.e. what it does, how it grows,…

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

Using the example of the class “Dog”, what could be some fitting attributes?

A

name, fur colour, size, age, race, …

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

Using the example of the class “Dog”, what could be some fitting methods?

A

wagTail()
bark()
givePaw()
eatKnibble()

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

Use a house and its blue-print to give an example on how classes and objects work.

A

When you create a class “House”, this would be a blue-print you draw for a house you want to build.
The class contains attributes like “numberOfWindows” and “numberOfRooms”.
It also contains methods like “calculateCostOfLiving(numberOfPersonsLivingHere)”.

You can use this blue-print to actually build a house, this would be the object of the class. It exists uniquely among other objects/houses.

You can use the same blue-print to build another house, then you have two objects/houses. If you give them both the same number of rooms and windows, then they look the same. They have the same attributes and methods but two unique addresses (because you can’t put the second house on the same address where the first is already standing).

But you can also use the blue-print to build the same house with a different amount of rooms, turn it into a skyscraper. It’s still a house (still of the same class/type) but it looks a bit different.

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

What are subclasses/child classes in object-oriented programming (OOP)?

A

A class that inherits attributes and method definitions from another class (superclass/base class/parent class).
These attributes and methods can be changed or extended without having an effect on the attributes and methods of the superclass.

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

Using the example superclass “Animal”, what could be fitting subclasses?

A

Dog, Cat, Bird, Dinosaur, Elephant,..

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

What’s the syntax to create a new class?

A

class ClassName:
attribute_1 = “value”

    def \_\_init\_\_(self, attr_2):
          self.attr_2 = attr_2
   
    def method(self):
          #do smth
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Using the following example, match the various terms to their name.

class Dog:
species = “canine”

 def \_\_init\_\_(self, name):
     self.name = name
  
 def speak(self):
     print(f"{self.name}: woof!")
  1. Dog
  2. species = “canine”
  3. def __init__(self, name)
  4. self.name = name
  5. def speak(self)
  6. {self.name}
A
  1. class name
  2. class attribute (has the same value for all objects)
  3. special method for object initialization
  4. object attribute (is individually set for each object)
  5. method (is called by an object of this class)
  6. placeholder that accesses the above created object attribute “name”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Using this class “Dog” as an example, what’s the code to access the species?

class Dog:
species = “canine”

 def \_\_init\_\_(self, name):
     self.name = name
  
 def speak(self):
     print(f"{self.name}: woof!")
A

Dog.species

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

Using this class “Dog” as an example, what’s the code to access the name?

class Dog:
species = “canine”

 def \_\_init\_\_(self, name):
     self.name = name
  
 def speak(self):
     print(f"{self.name}: woof!")
A

my_dog = Dog(“Rex”)
my_dog.name

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

Using this class “Dog” as an example, what’s the code to access the method and what is its output?

class Dog:
species = “canine”

 def \_\_init\_\_(self, name):
     self.name = name
  
 def speak(self):
     print(f"{self.name}: woof!")
A

my_dog = Dog(“Rex”)
my_dog.speak()

output:
Rex: woof!

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

What’s the syntax to extend a base class (create a subclass)?

A

class SubClass(SuperClass)

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

Using this class “Dog” as an example, what’s the code to creating a subclass “GuardDog” with the additional attribute “power”?

class Dog:
species = “canine”

 def \_\_init\_\_(self, name):
     self.name = name
  
 def speak(self):
     print(f"{self.name}: woof!")
A

class GuardDog(Dog):

  def \_\_init\_\_(self, power):
       super().\_\_init\_\_(name)
       self.power = power
17
Q

True or False: Python supports multiple inheritances.

A

True

18
Q

What is a Graphical Class Notation?

A

It’s a class diagram using UML (unified modeling language). It shows a class as a table with the name in the title and the attributes and methods listed below. Arrows show inheritances to other class diagrams.

19
Q

What is the method resolution order (MRO) and how does it work for this example?

my_object.some_method()

A

It determines which method is invoked when inheritence is involved.
It works like a bottom-to-top search:
1. get the class of my_object
2. does the class contain an implementation for some_method() ?
3. if yes: execute this method
if no: get the parent class and start again from step 2
4. repeat until the first implementation is found

20
Q

What built-in function returns the class of an object?

A

type(object)

21
Q

What built-in function checks if x is an instance of class y or an instance of a subclass of y?

A

isinstance(x, y)

22
Q

What built-in function checks if x is a subclass of class y?

A

issubclass(x, y)