Preview

16 - General OOP #2

 1. Methods are also inherited. Any method the parent has, the child class will have too. But what if we have a method in both the child and parent class?

  Methods may be overridden by a child class to provide different functionality.

  We can run them both with super() keyword

  Using super() followed by a dot operator, and then finally a method name allows you to call the parent's version of the method

  All of the above are valid options

 2. Classes have two main types of relationships. They are ______________________

  class and object relationships

  “is a” and “has a” relationships

  inheritance and encapsulation relationships

  higher and lower relationships

 3. A parent class should always be a more general, abstract version of the child class. This type of child to parent relationship is called an is a relationship.

  TRUE

  FALSE

 4. Another example, a dolphin is a mammal. It does not work the other way, a mammal is not necessarily a dolphin. So the class Dolphin should ________________

  never be a parent to a class Mammal

  be a plain object, but preferrably not exist at all

  be a parent to a class Mammal

  be a parent and a child to class Mammal

 5. Look at the following code. Which of the following statements are correct.
class Person():
    def __init__(self):
        self.name = ""
 
    def report(self):
        # Basic report
        print("Report for", self.name)
 
class Employee(Person):
    def __init__(self):
        # Call the parent/super class constructor first
        super().__init__()
 
        # Now set up our variables
        self.job_title = ""
 
    def report(self):
        # Here we override report and just do this:
        print("Employee report for", self.name)
 
class Customer(Person):
    def __init__(self):
        super().__init__()
        self.email = ""
 
    def report(self):
        # Run the parent report:
        super().report()
        # Now add our own stuff to the end so we do both
        print("Customer e-mail:", self.email)
 
john_smith = Person()
john_smith.name = "John Smith"
 
jane_employee = Employee()
jane_employee.name = "Jane Employee"
jane_employee.job_title = "Web Developer"
 
bob_customer = Customer()
bob_customer.name = "Bob Customer"
bob_customer.email = "[email protected]"
 
john_smith.report()
jane_employee.report()
bob_customer.report()

  Customer is a person.

  Employee has a job title.

  All of the above are correct

  Employee is a person.

 6. Analyse the code below for ClassA and ClassB. Which of the following statements is correct?
class ClassA():
    def __init__(self):
        self.y = 3
 

class ClassB():
    x = 7
 

a = ClassA()
b = ClassB()
 

# The second way is the proper way to do it.
print(b.x)
print(ClassB.x)
 

print(a.y)
print(ClassA.y)

  ClassA creates an instance variable. ClassB creates a static variable.

  All of the above statements are correct

  The two class instances that have been created are 'a' and 'b'

  With static variables the value is the same for every single instance of the class

 7. Which is the correct class definition for an alien?
Option 1
========
class alien():
    def __init__(self):
        alien.name = ""
        alien.height = 7.2
        alien.weight = 156


Option 2
========
class Alien():
    def __init__(self):
        self.name = ""
        self.height = 7.2
        self.weight = 156

  Option 1

  Option 2

  Option 1 and 2 are valid

  Neither Option 1 or 2 are valid. Something else would have to be used.

 8. What does this code do? (refer to the below code)
d1 = Dog()
d2 = Dog()

  Creates two classes, of type Dog.

  None of the above

  Creates one object, of type Dog.

  Creates two objects, of type Dog.

 9. What does this code do? Think carefully about the creation of objects and references (pointers)
d1 = Dog()
d2 = d1

  None of the above

  Creates two classes, of type Dog.

  Creates two objects, d1 and d2, of type Dog.

  There's one dog (object), and two pointers to that dog.

 10. Can you spot what is wrong with this code (look at both syntax and structure)
class Book():
    def open(self):
        print("You opened the book")
 
    def __init__(self):
        self.pages = 347

  There should be a self. in front of pages.

  Book should not be capitalized.

  The __init__ with attributes should be listed first.

  open should be capitalized.