wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Quiz2-OOP-MakScie

Total questions: 49

Worksheet time: 49mins

Name
Class
Date
1.

Which element in a class diagram shows the attributes of a class?

a)

Methods

b)

Attributes section

c)

Operations section

d)

Associations

2.

In UML class diagrams, what does the symbol '+' before an attribute mean?

a)

Protected

b)

Private

c)

Public

d)

Static

3.

Given the class diagram: Car

----------------

+ brand: str

+ speed: int

----------------

+ accelerate()

+ brake()

Which Python class matches it?

class Car:

def init(self, brand, speed):

self.brand = brand

self.speed = speed

def accelerate(self):

print("Accelerating...")

def brake(self):

print("Braking...")

a)

Matches

b)

Missing methods

c)

Missing attributes

d)

Wrong constructor

4.

In UML, inheritance is represented by:

a)

Dotted line

b)

Solid line with arrowhead

c)

Solid line with hollow triangle

d)

Dashed line with arrow

5.

If Circle inherits from Shape, which is correct in Python?

a)

Correct implementation

b)

Needs __init__ in Shape

c)

Invalid inheritance

d)

Circle cannot extend Shape

6.

Aggregation in class diagrams is represented by:

a)

Empty diamond

b)

Filled diamond

c)

Hollow arrow

d)

Dashed arrow

7.

Composition in class diagrams means:

a)

Objects exist independently

b)

Objects cannot exist without parent

c)

Classes are unrelated

d)

Inheritance relationship

8.

What does this diagram mean?

Teacher 1 --- * Student

a)

A teacher has one student

b)

A teacher can have many students

c)

A student can have many teachers

d)

No relationship

9.

Which keyword represents inheritance in Python?

a)

extend

b)

super

c)

class Child(Parent)

d)

derive

10.

What does <> in UML mean?

a)

Abstract class

b)

Inheritance

c)

Encapsulation

d)

Method overloading

11.

If UML shows a private attribute - age: int, how is it best represented in Python?

a)

self.age

b)

self.__age

c)

age only

d)

private age

12.

Which relationship best fits 'Car has an Engine'?

a)

Inheritance

b)

Aggregation

c)

Composition

d)

Dependency

13.

If a class diagram shows multiplicity 0..1, what does it mean?

a)

Zero or one instance

b)

Many instances

c)

At least one instance

d)

Exactly one instance

14.

Which UML diagram focuses on object structure?

a)

Sequence diagram

b)

Activity diagram

c)

Class diagram

d)

State diagram

15.

Which special method is used as a constructor in Python?

a)

__create__

b)

__init__

c)

__constructor__

d)

__start__

16.

What will this print?

class A:

def __init__(self):

print('Hello') obj = A()

a)

Nothing

b)

Error

c)

Hello

d)

None

17.

If a class has no __init__, what happens?

a)

Error

b)

Python auto-provides default constructor

c)

Object cannot be created

d)

super() is required

18.

What is output?

class A:

def __init__(self, x=5):

self.x = x

obj = A()

print(obj.x)

a)

Error

b)

None

c)

5

d)

x

19.

Which statement is correct?

class Car:

def __init__(self, brand):

self.brand = brand

a)

Constructor requires no argument

b)

Requires 1 argument when creating object

c)

Error

d)

Default brand is None

20.

What happens here?

class A:

def __init__(self):

print('First')

def __init__(self):

print('Second') obj = A()

a)

Prints 'First'

b)

Prints 'Second'

c)

Error

d)

None

21.

What will this output?

class Student:

def __init__(self, name='John'):

self.name = name

s = Student()

print(s.name)

a)

Error

b)

None

c)

John

d)

name

22.

How do you call parent constructor?

a)

Parent.init()

b)

super().__init__()

c)

parent.__init__()

d)

constructor()

23.

What is output?

class A:

def __init__(self):

self.value = 10

a = A()

print(hasattr(a, 'value'))

a)

Error

b)

False

c)

True

d)

None

24.

Can a class have multiple __init__ in Python?

a)

Yes, all run

b)

No, last one overrides

c)

Yes, depending on arguments

d)

Only in Java

25.

Can a class have multiple __init__ in Python?

a)

Yes, all run

b)

No, last one overrides

c)

Yes, depending on arguments

d)

Only in Java

26.

Which is best to simulate multiple constructors?

a)

Overloading with *args

b)

Multiple __init__

c)

Static methods

d)

Both A and C

27.

What is printed?

class Demo:

def __init__(self, x=1, y=2):

self.sum = x + y

d = Demo(3)

print(d.sum)

a)

3

b)

5

c)

Error

d)

None

28.

Which is true?

class A:

def __init__(self, a, b=0):

self.result = a+b

a)

Requires exactly 2 args

b)

At least 1 arg

c)

No arg needed

d)

Invalid

29.

If you forget self in constructor, what happens?

a)

Works fine

b)

Error: missing positional argument

c)

Self is auto-added

d)

Skips constructor

30.

Which constructor creates objects without attributes?

class A:

def __init__(self):

pass

a)

Valid, empty constructor

b)

Error

c)

Private constructor

d)

Abstract

31.

Does Python support true method overloading?

a)

Yes, like Java

b)

No, last method overrides

c)

Yes, with @overload keyword

d)

Only in C++

32.

Which technique simulates overloading?

a)

Default arguments

b)

*args

c)

@singledispatch

d)

All of the above

33.

What is output?

class Test:

def add(self, a, b=0):

return a+b

t = Test()

print(t.add(5))

a)

5

b)

0

c)

Error

d)

None

34.

What happens?

class A:

def greet(self, name=None):

if name:

print("Hello", name)

else:

print("Hello")

a = A()

a.greet()

a)

Error

b)

Hello

c)

Hello None

d)

Nothing

35.

Which is method overriding, not overloading?

class Parent:

def show(self):

print("Parent")

class Child(Parent):

def show(self):

print("Child")

a)

Overloading

b)

Overriding

c)

Both

d)

Error

36.

Output?

class Math:

def multiply(self, a, b=None):

if b:

return a*b

return a*a

m = Math()

print(m.multiply(5))

a)

5

b)

25

c)

None

d)

Error

37.

Which package provides formal type-based overloading?

a)

typing

b)

functools

c)

collections

d)

itertools

38.

What is output?

class Demo:

def display(self, *args):

return len(args)

d = Demo()

print(d.display(1,2,3))

a)

1

b)

2

c)

3

d)

Error

39.

Which is correct for static overloading simulation?

a)

Use classmethod as factory

b)

Use multiple __init__

c)

Use inheritance

d)

Not possible

40.

What is printed?

class A:

def process(self, a, b=10):

return a+b

obj = A()

print(obj.process(5))

a)

5

b)

15

c)

Error

d)

None

41.

Which is correct about *args?

a)

Accepts variable arguments

b)

Must be last parameter

c)

Helps simulate overloading

d)

All of the above

42.

Output?

class A: def area(self, *args):

if len(args)==1:

return args[0]*args[0]

elif len(args)==2:

return args[0]*args[1]

a = A()

print(a.area(5,10))

a)

25

b)

50

c)

Error

d)

None

43.

What does this simulate?

class Converter:

def to_str(self, value):

return str(value)

a)

Constructor

b)

Overloading

c)

Overriding

d)

Type casting method

44.

What happens if multiple methods have same name in Python?

a)

Overloaded

b)

Only last remains

c)

Error

d)

All coexist

45.

Output?

class Adder:

def add(self, *args):

return sum(args)

a = Adder()

print(a.add(1,2,3,4))

a)

4

b)

10

c)

Error

d)

None

46.

Which keyword allows method overloading in typing module?

a)

@dispatch

b)

@overload

c)

@multiple

d)

@typing

47.

What is printed?

class Display:

def show(self, name=None, age=None):

if name and age:

return f"{name}, {age}"

elif name:

return name return "No data"

d = Display()

print(d.show("Alex"))

a)

Alex

b)

Alex, None

c)

No data

d)

Error

48.

What is the advantage of method overloading simulation?

a)

Cleaner code

b)

Multiple behaviors in one method

c)

More flexible function calls

d)

All of the above

49.

Which is NOT a Python way to simulate overloading?

a)

Default values

b)

*args

c)

@singledispatch

d)

Multiple methods with same name