Search Header Logo
DCS2103 - Week 13 (Inheritance)

DCS2103 - Week 13 (Inheritance)

Assessment

Presentation

Computers

University

Practice Problem

Easy

Created by

Yasmin Basiran

Used 1+ times

FREE Resource

24 Slides • 13 Questions

1

media

2

media

3

Multiple Choice

Which of the following best describes inheritance in object-oriented programming?

1
Inheritance allows a class to create new classes without any properties.
2
Inheritance is a method to combine multiple classes into one.
3
Inheritance restricts a class from accessing its parent class's methods.
4
Inheritance allows a class to inherit properties and methods from another class.

4

5

media

6

media

7

media

8

media

9

media

10

media

11

media

12

media

13

media

14

media

15

media

16

media

17

media

18

media

19

Multiple Choice

In Python, what does the super() function do?

1
The super() function allows access to methods from a parent class.
2
The super() function retrieves attributes from a sibling class.
3
The super() function is used to delete a parent class.
4
The super() function creates a new instance of a class.

20

Multiple Choice

What is the output of:

class Parent:

def init(self):

self.value = 5

class Child(Parent):

def init(self):

super().__init__()

self.value = 10

c = Child()

print(c.value)

1
10
2
0
3
15
4
5

21

Multiple Choice

What is the output of
class A:

def init(self, val):

self.val = val

class B(A):

def init(self):

super().__init__(10)

b = B()

print(b.val)

1
5
2
15
3
20
4
10

22

Multiple Choice

Is there an error in this code?

class A:

def init(self):

self.x = 5

class B(A):

def init(self):

self.y = 10

print(B().x)

1
No, it runs without issues.
2
Yes, there is an error in the code.
3
Yes, but the error is minor.
4
No, the code is correct.

23

Multiple Choice

If a child class defines its own init but doesn’t call super().__init__(), what happens?

1
The child class's __init__ method overrides the parent class's __init__ completely.
2
The parent class's __init__ method is called automatically without needing super().
3
The child class will inherit all attributes from the parent class without initialization.
4
The parent class's __init__ method is not called, leading to possible uninitialized attributes.

24

media

25

media

26

media

27

media

28

media

29

media

30

Multiple Choice

Which of the following is true about multiple inheritance in Python?

1

A class can inherit from multiple parent classes

2

Python does not support multiple inheritance

3

Only methods are inherited, not attributes

4

Constructors cannot be inherited

31

Multiple Choice

What is the output of

class Engine:

def init(self, hp):

self.hp = hp

class Car:

def init(self):

self.engine = Engine(150)

c = Car()

print(c.engine.hp)

1
1500
2
150
3
200
4
100

32

Multiple Choice

What is the main benefit of composition over inheritance?

1
Composition promotes flexibility and reusability over the rigid structure of inheritance.
2
Inheritance provides better performance in large applications.
3
Composition leads to more complex code structures.
4
Inheritance allows for easier debugging and maintenance.

33

Draw

Draw a multiple inheritance diagram: Bird and Animal as parents, FlyingAnimal as child.

34

Open Ended

Explain composition in Python with a real-life example.

35

Open Ended

What is one concept, function, or trick in Python that you are most proud of learning?

36

Open Ended

What one piece of advice would you give someone taking this course next semester?

37

media
media

Show answer

Auto Play

Slide 1 / 37

SLIDE