Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Q1.

Differentiate between procedural oriented programming and object oriented


programming with example.
Ans.
Procedural Oriented Programming Object-Oriented Programming
In procedural programming, the In object-oriented programming, the
program is divided into small parts program is divided into small parts
called functions. called objects.
Procedural programming follows a top- Object-oriented programming follows a
down approach. bottom-up approach.
There is no access specifier in Object-oriented programming has
procedural programming access specifiers like private, public,
protected, etc.
Adding new data and functions is not Adding new data and function is easy.
easy.
Procedural programming does not have Object-oriented programming provides
any proper way of hiding data so it is data hiding so it is more secure.
less secure.
In procedural programming, Overloading is possible in object-
overloading is not possible. oriented programming.
In procedural programming, there is no In object-oriented programming, the
concept of data hiding and inheritance. concept of data hiding and inheritance
is used.

Q2. Write the differences between instance method, class method and static
method.
Ans. The instance methods are bound to the class instance and perform a set of
actions on the data/value given by the object (instance) variables. If we use the
instance variable inside the methods, these methods are called instance methods. It
can modify the object state. The self keyword is the first parameter to work with
the instance method, and the self refers to the current object.
The class methods are bound to the class, not to the instance. It can modify the
class state means it can change class configuration globally. It can access only the
class variable. The class methods are used to create the factory methods. The
syntax of class methods is different; instead of taking self parameter, they accept
cls as a parameter that points to the class. It can't modify the instance state.
However, the changes made by the class method reflect all instances of the class.
The @classemethod decorator or classmethod() defines the class methods.
The Static methods neither use self nor cls parameter; general utility methods
perform the task in isolation. Static methods in Python are similar to those found in
Java and C++, and they can't modify the behavior of the class or instance. The
@staticmethod decorator or staticmethod() method defines the static methods.

Q3. What is class constructor? How we declare constructor in Python.


Ans. Constructors are generally used for instantiating an object. The task of
constructors is to initialize(assign values) to the data members of the class when an
object of the class is created. In Python the __init__() method is called the
constructor and is always called when an object is created.

Q4. What is inheritance? Explain the different types of inheritance with example.
Ans. Inheritance is the process of creating a new Class, called the Derived Class ,
from the existing class, called the Base Class . The Inheritance has many
advantages, the most important of them being the reusability of code. Rather than
developing new Objects from scratch, new code can be based on the work of other
developers, adding only the new features that are needed. The reuse of existing
classes saves time and effort.
Different types of inheritance are –
 Single Inheritance
 Multi Level Inheritance
 Hierarchical Inheritance
 Hybrid Inheritance
 Multiple Inheritance
Single inheritance-
In this inheritance, a derived class is created from a single base class.
class Parent:
def func1(self):
print("this is function one")
class Child(Parent):
def func2(self):
print(" this is function 2 ")
ob = Child()
ob.func1()
ob.func2(

Multi level inheritance-


In this inheritance, a derived class is created from another derived class.
class Parent:
def func1(self):
print("this is function 1")
class Child(Parent):
def func2(self):
print("this is function 2")
class Child2(Child):
def func3("this is function 3")
ob = Child2()
ob.func1()
ob.func2()
ob.func3()

Multiple inheritance-
In this inheritance, a derived class is created from more than one base class.
class Parent:
def func1(self):
print("this is function 1")
class Parent2:
def func2(self):
print("this is function 2")
class Child(Parent , Parent2):
def func3(self):
print("this is function 3")

ob = Child()
ob.func1()
ob.func2()
ob.func3()

Hybrid inheritance-
This is a combination of more than one inheritance. Hence, it may be a
combination of Multilevel and Multiple inheritance or Hierarchical and Multilevel
inheritance Hierarchical and Multipath inheritance, or Hierarchical, Multilevel and
Multiple inheritances.
class Parent:
def func1(self):
print("this is function one")

class Child(Parent):
def func2(self):
print("this is function 2")

class Child1(Parent):
def func3(self):
print(" this is function 3"):

class Child3(Parent , Child1):


def func4(self):
print(" this is function 4")

ob = Child3()
ob.func1()

Hierarchical Inheritance-
In this inheritance, more than one derived class is created from a single base class
and further child classes act as parent classes for more than one child class.
class Parent:
def func1(self):
print("this is function 1")
class Child(Parent):
def func2(self):
print("this is function 2")
class Child2(Parent):
def func3(self):
print("this is function 3")
ob = Child()
ob1 = Child2()
ob.func1()
ob.func2()

Q5. What is polymorphism? Explain with the help of a program.


Ans. The literal meaning of polymorphism is the condition of occurrence in
different forms.

Polymorphism is a very important concept in programming. It refers to the use of a


single type entity (method, operator or object) to represent different types in
different scenarios.
from math import pi

class Shape:
def __init__(self, name):
self.name = name

def area(self):
pass

def fact(self):
return "I am a two-dimensional shape."

def __str__(self):
return self.name

class Square(Shape):
def __init__(self, length):
super().__init__("Square")
self.length = length

def area(self):
return self.length**2
def fact(self):
return "Squares have each angle equal to 90 degrees."

class Circle(Shape):
def __init__(self, radius):
super().__init__("Circle")
self.radius = radius

def area(self):
return pi*self.radius**2

a = Square(4)
b = Circle(7)
print(b)
print(b.fact())
print(a.fact())
print(b.area())

Q6. Define the following:


a. Method Overloading-Overloading is a method or operator that can do
different functionalities with the same name.
b. class edpresso:
c.     def Hello(self, name=None):
d.         if name is not None:
e.             print('Hello ' + name)
f.         else:
g.             print('Hello ')
h.            
i. # Create an instance
j. obj = edpresso()
k.    
l. # Call the method
m. obj.Hello()
n.    
o. # Call the method with a parameter
p. obj.Hello('Kadambini')
b. Operator Overloading-Operator Overloading means giving extended meaning
beyond their predefined operational meaning. For example operator + is used to
add two integers as well as join two strings and merge two lists. It is achievable
because ‘+’ operator is overloaded by int class and str class. You might have
noticed that the same built-in operator or function shows different behavior for
objects of different classes, this is called Operator Overloading.
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y

def __str__(self):
return "({0},{1})".format(self.x, self.y)

def __add__(self, other):


x = self.x + other.x
y = self.y + other.y
return Point(x, y)

p1 = Point(1, 2)
p2 = Point(2, 3)

print(p1+p2)
Q7. Write a program that takes an input from the user and asks to enter filename. It
then opens the file for reading it. If the file is not found, catch the exception and
give a suitable message.
Ans.
filenm=input(“enter the file name”)
try:
fp=open(filenm,”r”)
print(fp.read())
except FileNotFoundError:
print(f “{filenm} does not exist”)

You might also like