NEW
Font size
WorksheetsQuiz2-OOP-MakScie
Total questions: 49
Worksheet time: 49mins
Which element in a class diagram shows the attributes of a class?
Methods
Attributes section
Operations section
Associations
In UML class diagrams, what does the symbol '+' before an attribute mean?
Protected
Private
Public
Static
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...")
Matches
Missing methods
Missing attributes
Wrong constructor
In UML, inheritance is represented by:
Dotted line
Solid line with arrowhead
Solid line with hollow triangle
Dashed line with arrow
If Circle inherits from Shape, which is correct in Python?
Correct implementation
Needs __init__ in Shape
Invalid inheritance
Circle cannot extend Shape
Aggregation in class diagrams is represented by:
Empty diamond
Filled diamond
Hollow arrow
Dashed arrow
Composition in class diagrams means:
Objects exist independently
Objects cannot exist without parent
Classes are unrelated
Inheritance relationship
What does this diagram mean?
Teacher 1 --- * Student
A teacher has one student
A teacher can have many students
A student can have many teachers
No relationship
Which keyword represents inheritance in Python?
extend
super
class Child(Parent)
derive
What does <
Abstract class
Inheritance
Encapsulation
Method overloading
If UML shows a private attribute - age: int, how is it best represented in Python?
self.age
self.__age
age only
private age
Which relationship best fits 'Car has an Engine'?
Inheritance
Aggregation
Composition
Dependency
If a class diagram shows multiplicity 0..1, what does it mean?
Zero or one instance
Many instances
At least one instance
Exactly one instance
Which UML diagram focuses on object structure?
Sequence diagram
Activity diagram
Class diagram
State diagram
Which special method is used as a constructor in Python?
__create__
__init__
__constructor__
__start__
What will this print?
class A:
def __init__(self):
print('Hello') obj = A()
Nothing
Error
Hello
None
If a class has no __init__, what happens?
Error
Python auto-provides default constructor
Object cannot be created
super() is required
What is output?
class A:
def __init__(self, x=5):
self.x = x
obj = A()
print(obj.x)
Error
None
5
x
Which statement is correct?
class Car:
def __init__(self, brand):
self.brand = brand
Constructor requires no argument
Requires 1 argument when creating object
Error
Default brand is None
What happens here?
class A:
def __init__(self):
print('First')
def __init__(self):
print('Second') obj = A()
Prints 'First'
Prints 'Second'
Error
None
What will this output?
class Student:
def __init__(self, name='John'):
self.name = name
s = Student()
print(s.name)
Error
None
John
name
How do you call parent constructor?
Parent.init()
super().__init__()
parent.__init__()
constructor()
What is output?
class A:
def __init__(self):
self.value = 10
a = A()
print(hasattr(a, 'value'))
Error
False
True
None
Can a class have multiple __init__ in Python?
Yes, all run
No, last one overrides
Yes, depending on arguments
Only in Java
Can a class have multiple __init__ in Python?
Yes, all run
No, last one overrides
Yes, depending on arguments
Only in Java
Which is best to simulate multiple constructors?
Overloading with *args
Multiple __init__
Static methods
Both A and C
What is printed?
class Demo:
def __init__(self, x=1, y=2):
self.sum = x + y
d = Demo(3)
print(d.sum)
3
5
Error
None
Which is true?
class A:
def __init__(self, a, b=0):
self.result = a+b
Requires exactly 2 args
At least 1 arg
No arg needed
Invalid
If you forget self in constructor, what happens?
Works fine
Error: missing positional argument
Self is auto-added
Skips constructor
Which constructor creates objects without attributes?
class A:
def __init__(self):
pass
Valid, empty constructor
Error
Private constructor
Abstract
Does Python support true method overloading?
Yes, like Java
No, last method overrides
Yes, with @overload keyword
Only in C++
Which technique simulates overloading?
Default arguments
*args
@singledispatch
All of the above
What is output?
class Test:
def add(self, a, b=0):
return a+b
t = Test()
print(t.add(5))
5
0
Error
None
What happens?
class A:
def greet(self, name=None):
if name:
print("Hello", name)
else:
print("Hello")
a = A()
a.greet()
Error
Hello
Hello None
Nothing
Which is method overriding, not overloading?
class Parent:
def show(self):
print("Parent")
class Child(Parent):
def show(self):
print("Child")
Overloading
Overriding
Both
Error
Output?
class Math:
def multiply(self, a, b=None):
if b:
return a*b
return a*a
m = Math()
print(m.multiply(5))
5
25
None
Error
Which package provides formal type-based overloading?
typing
functools
collections
itertools
What is output?
class Demo:
def display(self, *args):
return len(args)
d = Demo()
print(d.display(1,2,3))
1
2
3
Error
Which is correct for static overloading simulation?
Use classmethod as factory
Use multiple __init__
Use inheritance
Not possible
What is printed?
class A:
def process(self, a, b=10):
return a+b
obj = A()
print(obj.process(5))
5
15
Error
None
Which is correct about *args?
Accepts variable arguments
Must be last parameter
Helps simulate overloading
All of the above
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))
25
50
Error
None
What does this simulate?
class Converter:
def to_str(self, value):
return str(value)
Constructor
Overloading
Overriding
Type casting method
What happens if multiple methods have same name in Python?
Overloaded
Only last remains
Error
All coexist
Output?
class Adder:
def add(self, *args):
return sum(args)
a = Adder()
print(a.add(1,2,3,4))
4
10
Error
None
Which keyword allows method overloading in typing module?
@dispatch
@overload
@multiple
@typing
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"))
Alex
Alex, None
No data
Error
What is the advantage of method overloading simulation?
Cleaner code
Multiple behaviors in one method
More flexible function calls
All of the above
Which is NOT a Python way to simulate overloading?
Default values
*args
@singledispatch
Multiple methods with same name
