0% found this document useful (0 votes)
106 views4 pages

OOP Final Exam - Solution

The document contains 5 questions related to OOP concepts in Python. Question 1 demonstrates multiple inheritance by defining an Admin class that inherits from both the Student and Test classes. Question 2 shows encapsulation by defining protected members in the base TriangleParam class that are accessed in the derived ComputeArea class. Question 3 demonstrates polymorphism by defining Calculator and Concatenation classes that both have an addition method but perform different operations. Question 4 shows a class decorator that takes function arguments and calls the decorated function.

Uploaded by

Abdul Haseeb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views4 pages

OOP Final Exam - Solution

The document contains 5 questions related to OOP concepts in Python. Question 1 demonstrates multiple inheritance by defining an Admin class that inherits from both the Student and Test classes. Question 2 shows encapsulation by defining protected members in the base TriangleParam class that are accessed in the derived ComputeArea class. Question 3 demonstrates polymorphism by defining Calculator and Concatenation classes that both have an addition method but perform different operations. Question 4 shows a class decorator that takes function arguments and calls the decorated function.

Uploaded by

Abdul Haseeb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Q#.

2 (From Multiple Inheritance)


class Student:
def __init__(self):
self.name = input("Enter your name:")
self.cname = input("Enter your college name:")
self.roll = int(input("Enter your roll number:"))
self.marks = int(input("Enter your marks:"))
class Test():
def check(self):
if self.marks > 60:
print("Your are promoted to next level:", self.marks)
else:
print("Your are not promoted to next level:", self.marks)
class Admin(Test,Student):
def display(self):
print("============ Student info is ==========")
print("Name is : ", self.name)
print("College Name is : ", self.cname)
print("Roll number is : ", self.roll)

obj = Admin()
obj.display()
obj.check()

Q#. 3 (From Encapsulation)


class TriangleParam:
def __init__(self):
# Protected members
self._height = 2
self._base = 3

class ComputeArea(TriangleParam):
def __init__(self):

# Calling constructor of
# Base class
TriangleParam.__init__(self)
self.area=((self._height*self._base)/2)
print("Area of a Triangle: ",
self.area)

obj1 = ComputeArea()
obj2 = TriangleParam()
print("While, the Base has a value: ", obj2._base)
print("And, the Height has a value: ", obj2._height)

Q#. 4 (From Polymorphism)

class Calculator:
def __init__(self, value1, value2):
self.fvalue = value1
self.svalue = value2

def info(self):
print("The first value:",self.fvalue)
print("The second value:",self.svalue)
def addition(self):
self.result=self.fvalue+self.svalue
print("The result:",self.result)

class Concatenation:
def __init__(self, fstring, sstring):
self.fstring = fstring
self.sstring = sstring

def info(self):
print("The first String:",self.fstring)
print("The second String:",self.sstring)

def addition(self):
self.concat=self.fstring+self.sstring
print("The result:",self.concat)

cal = Calculator(5, 2.5)


concat = Concatenation("Hello", "World")
cal.info()
cal.addition()
concat.info()
concat.addition()

Q#. 5 (From Decorator)


# Python program showing
# class decorator with *args
# and **kwargs
class MyDecorator:
def __init__(self, function):
self.function = function
def __call__(self, *args, **kwargs):
# We can add some code
# before function call
self.function(*args, **kwargs)
# We can also add some code
# after function call.
# adding class decorator to the function
@MyDecorator
def function(name, message ='Hello'):
for i in range(1):
for j in range(10):
print("*",end="")
print()
print("{}\n{}\n".format(name.upper(), message.upper()))
for i in range(1):
for j in range(10):
print("*",end="")
print()
function("Welcome to ", "Object Oriented Programming Class")

You might also like