20.10. Multiple Choice QuestionsΒΆ
- A person can work. A person can work. An employee works. A person eats food.
- Try again!
- A person can work. An employee works. A person eats food.
- Try again!
- The code won't compile.
- Try again!
- A person can work. An employee works. A person eats food. A person eats food.
- Correct! Since Employee inherited from Person, the food method works for the Employee class as well.
- A person can work. A person can work. An employee works. A person eats food. A person eats food.
- Try again!
Q-1: Given the below code, what would be printed (ignore newlines and spacing between sentences)?
class Person:
def work(self):
print("A person can work.")
def food(self):
print("A person eats food.")
class Employee(Person):
def work(self):
print("An employee works.")
p1 = Person()
p2 = Employee()
p1.work()
p2.work()
p1.food()
p2.food()
- A person can work. None. A person eats food. None.
- Try again!
- A person can work. A person can work. A person eats food. A person eats food.
- Correct! Since Employee inherited from Person, the methods work and food work for Employee as well.
- The code won't compile.
- Try again!
- A person can work. A person eats food.
- Try again!
Q-2: Given the below code, what would be printed (ignore newlines and spacing between sentences)?
class Person:
def work(self):
print("A person can work.")
def food(self):
print("A person eats food.")
class Employee(Person):
pass
p1 = Person()
p2 = Employee()
p1.work()
p2.work()
p1.food()
p2.food()
- A person can work. I can work. A person eats food. I can eat.
- Try again! There are 2 sentences missing: "A person can work." and "A person eats food.".
- A person can work. None. I can work. A person eats food. None. I can eat.
- Try again! Using super().method in the child class Employee method inherits the method (print statements, in this case) from the parent class Person.
- The code won't compile.
- Try again! The code compiles.
- A person can work. A person can work. I can work. A person eats food. A person eats food. I can eat.
- Correct! Using super() reduces code duplication, and the child class method can duplicate the functionality of the parent class method.
Q-3: Given the below code, what would be printed (ignore newlines and spacing between sentences)?
class Person:
def work(self):
print("A person can work.")
def food(self):
print("A person eats food.")
class Employee(Person):
def work(self):
super().work()
print("I can work.")
def food(self):
super().food()
print("I can eat.")
p1 = Person()
p2 = Employee()
p1.work()
p2.work()
p1.food()
p2.food()
- Keyboard inherits from Computer.
- Try again! Keyboard isn't a type of Computer, so inheritance can't be used.
- Student inherits from Person.
- Correct! Student is a type of Person, so inheritance can be used.
- Encyclopedia inherits from Book.
- Correct! Encyclopedia is a type of Book, so inheritance can be used.
- Person inherits House.
- Try again! Person isn't a type of House, so inheritance can't be used.
Q-4: For which of the following can inheritance be used?
- Yes, a class can only inherit from another class if it is a type of the class.
- Correct! A class can only inherit from another class if it is a type of the class. For example, an encyclopedia is a type of book.
- No, a class can inherit from another class if they're associated.
- Try again!
- Yes and no. A class can inherit from another class if they're associated or if it is a type of the class.
- Try again!
Q-5: Can a class only inherit from another class if it is a type of the class?
- A (class) has a (class).
- Try again! Classes that are associated can't use inheritance.
- A (class) can be substituted with (another class).
- Correct! Classes that can be substituted can use inheritance.
- A (class) can be associated with (another class).
- Try again! Classes that are associated can't use inheritance.
- A (class) is a type of (other class).
- Correct! Classes that can be substituted can use inheritance.
- A (class) is a specialization of (other class).
- Correct! Classes that can be substituted can use inheritance.
Q-6: What phrases represent inheritance?
- parent().method
- Do not use parent.
- parent.method
- Do not use parent. Do use ().
- super().method
- Correct! Use super().method to call parent class methods.
- super.method
- You need the ()
Q-7: How do you call a method in a parent class?
- Book.__init__(self)
- Correct! Using ParentClass.method is one way to invoke a method in the ParentClass to the ChildClass.
- Dictionary.__init__(self)
- Try again!
- super().__init__()
- Correct! Using super().method is one way to invoke a method in the ParentClass to the ChildClass.
- Book.super().__init__(self)
- Try again!
- Book.__init__(Dictionary)
- Try again!
Q-8: If Dictionary is a subclass of Book, what line of code is needed to invoke the __init__ method in Book to Dictionary?
- True
- Correct! p2 is assigned to an Employee object, and the Employee object inherits from Person. Therefore, p2 is an instance of Person.
- False
- Try again!
- The code won't compile.
- Try again! The code compiles even with the pass statements.
- None
- Try again! Printing isinstance() will print either True or False if it compiles.
Q-9: What will be printed?
class Person:
pass
class Employee(Person):
pass
p2 = Employee()
print(isinstance(p2, Person))
- Inheritance is a key idea of Object-oriented programming (OOP).
- Try again! This statement about inheritance is correct.
- The child class is also called a subclass or a derived class.
- Try again! This statement about inheritance is correct.
- Private members of a class can be inherited.
- Correct! This statement about inheritance is incorrect. Private members of a class can't be inherited.
- Protected members of a class can be inherited.
- Try again! This statement about inheritance is correct.
Q-10: Which statement about inheritance is incorrect?
- I
- All methods take self (the current object).
- II
- The test_create method takes self and you must use self to access i1 and i2.
- III
- Correct! Remember to pass in self to methods and use self when accessing attributes.
- IV
- The setUp method takes self and you must use self in the setUp so other methods will still have access.
Q-11: Given that there is an Item
class with a name and price with a __str__
method that returns "name: price"
, which of the following is correct?
I
class TestItem(unittest.TestCase):
def setUp():
i1 = Item("Coke", 2.99)
i2 = Item("Burger", 6.99)
def test_str_item():
self.assertEqual(i1.__str__(), "Coke: 2.99")
self.assertEqual(i2.__str__(), "Burger: 6.99")
II
class TestItem(unittest.TestCase):
def setUp(self):
self.i1 = Item("Coke", 2.99)
self.i2 = Item("Burger", 6.99)
def test_str_item():
self.assertEqual(i1.__str__(), "Coke: 2.99")
self.assertEqual(i2.__str__(), "Burger: 6.99")
III
class TestItem(unittest.TestCase):
def setUp(self):
self.i1 = Item("Coke", 2.99)
self.i2 = Item("Burger", 6.99)
def test_str_item(self):
self.assertEqual(self.i1.__str__(), "Coke: 2.99")
self.assertEqual(self.i2.__str__(), "Burger: 6.99")
IV
class TestItem(unittest.TestCase):
def setUp():
i1 = Item("Coke", 2.99)
i2 = Item("Burger", 6.99)
def test_str_item(self):
self.assertEqual(self.i1.__str__(), "Coke: 2.99")
self.assertEqual(self.i2.__str__(), "Burger: 6.99")
- I
- All methods take self (the current object).
- II
- The assertEqual methods need to be called on self (the current object).
- III
- All methods take self (the current object).
- IV
- Correct! All methods take self and you must call assertEqual on self and use self for accessing attributes.
Q-12: Given that there is an Car
class with attributes of make
and color
, which of the following is correct?
I
class TestCar(unittest.TestCase):
def setUp():
c1 = Car("Ford", "blue")
c2 = Car("Toyota", "red")
def test_create_car():
assertEqual(c1.make, "Ford")
assertEqual(c1.color, "blue")
assertEqual(c2.make, "Toyota")
assertEqual(c2.color, "red")
II
class TestCar(unittest.TestCase):
def setUp(self):
self.c1 = Car("Ford", "blue")
self.c2 = Car("Toyota", "red")
def test_create_car(self):
assertEqual(self.c1.make, "Ford")
assertEqual(self.c1.color, "blue")
assertEqual(self.c2.make, "Toyota")
assertEqual(self.c2.color, "red")
III
class TestCar(unittest.TestCase):
def setUp(self):
self.c1 = Car("Ford", "blue")
self.c2 = Car("Toyota", "red")
def test_create_car():
self.assertEqual(self.c1.make, "Ford")
self.assertEqual(self.c1.color, "blue")
self.assertEqual(self.c2.make, "Toyota")
self.assertEqual(self.c2.color, "red")
IV
class TestCar(unittest.TestCase):
def setUp(self):
self.c1 = Car("Ford", "blue")
self.c2 = Car("Toyota", "red")
def test_create_car(self):
self.assertEqual(self.c1.make, "Ford")
self.assertEqual(self.c1.color, "blue")
self.assertEqual(self.c2.make, "Toyota")
self.assertEqual(self.c2.color, "red")
- I
- All methods take self (the current object).
- II
- Check the original and changed color on each car object.
- III
- Correct!
- IV
- Check the original and changed color on each car object.
Q-13: Given that there is an Car
class with attributes of make
and color
and a method set_color
which changes the color, which of the following is correct?
I
class TestCar(unittest.TestCase):
def setUp():
c1 = Car("Ford", "blue")
c2 = Car("Toyota", "red")
def test_change_color():
assertEqual(c1.color, "blue")
c1.set_color("yellow")
assertEqual(c1.color, "yellow")
assertEqual(c2.color, "red")
c2.set_color("blue")
assertEqual(c2.color, "blue")
II
class TestCar(unittest.TestCase):
def setUp(self):
self.c1 = Car("Ford", "blue")
self.c2 = Car("Toyota", "red")
def test_change_color(self):
self.assertEqual(self.c1.color, "yellow")
self.c1.set_color("yellow")
self.assertEqual(self.c1.color, "blue")
self.assertEqual(self.c2.color, "red")
self.c2.set_color("blue")
self.assertEqual(self.c2.color, "blue")
III
class TestCar(unittest.TestCase):
def setUp(self):
self.c1 = Car("Ford", "blue")
self.c2 = Car("Toyota", "red")
def test_change_color(self):
self.assertEqual(self.c1.color, "blue")
self.c1.set_color("yellow")
self.assertEqual(self.c1.color, "yellow")
self.assertEqual(self.c2.color, "red")
self.c2.set_color("blue")
self.assertEqual(self.c2.color, "blue")
IV
class TestCar(unittest.TestCase):
def setUp(self):
self.c1 = Car("Ford", "blue")
self.c2 = Car("Toyota", "red")
def test_change_color(self):
self.assertEqual(self.c1.color, "blue")
self.c1.set_color("yellow")
self.assertEqual(self.c1.color, "yellow")
self.assertEqual(self.c2.color, "blue")
self.c2.set_color("blue")
self.assertEqual(self.c2.color, "red")
- I
- All methods take self (the current object).
- II
- All methods take self (the current object).
- III
- Are the test cases correct? What was the original make and changed make for each?
- IV
- Correct!
Q-14: Given that there is an Car
class with attributes of make
and color
and a method set_make
which changes the make, which of the following is correct?
I
class TestCar(unittest.TestCase):
def setUp():
c1 = Car("Ford", "blue")
c2 = Car("Toyota", "red")
def test_change_make():
assertEqual(c1.make, "Ford"")
c1.set_make("Toyota")
assertEqual(c1.make, "Toyota")
assertEqual(c2.make, "Toyota")
c2.set_make("Ford")
assertEqual(c2.make, "Ford")
II
class TestCar(unittest.TestCase):
def setUp(self):
self.c1 = Car("Ford", "blue")
self.c2 = Car("Toyota", "red")
def test_change_make():
self.assertEqual(self.c1.make, "Toyota")
self.c1.set_make("Ford")
self.assertEqual(self.c1.make, "Ford")
self.assertEqual(self.c2.make, "Ford")
self.c2.set_make("Toyota")
self.assertEqual(self.c2.make, "Toyota")
III
class TestCar(unittest.TestCase):
def setUp(self):
self.c1 = Car("Ford", "blue")
self.c2 = Car("Toyota", "red")
def test_change_make(self):
self.assertEqual(c1.make, "Toyota")
self.c1.set_make("Ford")
self.assertEqual(c1.make, "Ford")
self.assertEqual(c2.make, "Ford")
self.c2.set_make("Toyota")
self.assertEqual(c2.make, "Toyota")
IV
class TestCar(unittest.TestCase):
def setUp(self):
self.c1 = Car("Ford", "blue")
self.c2 = Car("Toyota", "red")
def test_change_make(self):
self.assertEqual(self.c1.make, "Ford"")
self.c1.set_make("Toyota")
self.assertEqual(self.c1.make, "Toyota")
self.assertEqual(self.c2.make, "Toyota")
self.c2.set_make("Ford")
self.assertEqual(self.c2.make, "Ford")
- I
- Correct!
- II
- Is 9.99 correct?
- III
- The method total returns a number, not a string.
- IV
- Is 8.98 correct?
Q-15: Given that there is an Item
class with a name
and price
and an Order
class that takes a list of Item
objects and has a total
method which returns the total price for all items, which of the following is correct?
I
class TestItem(unittest.TestCase):
def setUp(self):
i1 = Item("Coke", 2.99)
i2 = Item("Burger", 6.99)
self.o1 = Order([i1, i2])
def test_create_order_total(self):
self.assertAlmostEqual(self.o1.total(), 9.98, 2)
II
class TestItem(unittest.TestCase):
def setUp(self):
i1 = Item("Coke", 2.99)
i2 = Item("Burger", 6.99)
self.o1 = Order([i1, i2])
def test_create_order_total(self):
self.assertAlmostEqual(self.o1.total(), 9.99, 2)
III
class TestItem(unittest.TestCase):
def setUp(self):
i1 = Item("Coke", 2.99)
i2 = Item("Burger", 6.99)
self.o1 = Order([i1, i2])
def test_create_order_total(self):
self.assertAlmostEqual(self.o1.total(), "8.98", 2)
IV
class TestItem(unittest.TestCase):
class TestItem(unittest.TestCase):
def setUp(self):
i1 = Item("Coke", 2.99)
i2 = Item("Burger", 6.99)
self.o1 = Order([i1, i2])
def test_create_order_total(self):
self.assertAlmostEqual(self.o1.total(), 8.98, 2)