Ds With Python Question Bank: Unit-1: Object-Oriented Concepts in Python 2M Questions
Ds With Python Question Bank: Unit-1: Object-Oriented Concepts in Python 2M Questions
8M Questions:
1. Write the differences between method overloading and method overriding. Explain with
example program
2. Write a program to demonstrate student class and calculate CGPA of student and display
the result.
3. Define Inheritance. Explain each category of inheritance with an example program.
4. What is a constructor? Write a program for demonstrate different types of constructor
5. Explain in detail about Multiple Inheritance and Multilevel Inheritance along with an
example python program.
6. a) What is a Constructor? Is Constructor Overloading possible in Python? Explain.
b) Differences between Parameterized and Default constructors.
7. Define Polymorphism and explain in detail about Runtime Polymorphism with an example
python program.
8. What is Operator Overloading? Explain Overloading in Comparison operator (< less than)
with an example python program.
9. Define data structure. Discuss different types of data structure and their implementation
applications.
10. Explain the features of OOPs? Describe Encapsulation, Inheritance and Polymorphism.
11. Explain in detail about Abstraction with an example program.
12. Differences between Run time Polymorphism and Compile time polymorphism.
13. What is Encapsulation. Explain with an example program.
14. Write a python program which calculates areas of square, rectangle, circle using
Abstraction concept.
ALL LAB PROGRAMS OF UNIT-1
8M Questions:
1. Explain the following searching techniques with examples 1.Linear search 2.Binary
search
2. What is a single linked list? Explain Different operations on single linked list
3. Write algorithm and program for implementing Binary search
4. Write algorithm and program for implementing Linear search
5. Write a program for implementing a singly linked list.
6. Explain in detail about Insertion Sort with an example python program.
7. Explain in detail about Selection Sort with an example python program.
8. Explain in detail about Bubble Sort with an example python program.
9. Write a Python program to Insert a node at the beginning and end and to delete a node at the
beginning and end of circular linked list.
10. Write a Python program to Insert a node at the beginning and end and to delete a node at the
beginning and end of doubly linked list.
DS WITH PYTHON QUESTION BANK
Unit-1: Object-Oriented Concepts in
Python 2M Questions:
1. List out features of OOP’s in Python.
A)
The 4 Principles Of Object-Oriented Programming. Encapsulation,
Abstraction,Polymorphism, Inheritance.
Major Python OOP's concept-
Class.
Object.
Method.
Inheritance.
Encapsulation.
Polymorphism.
Data Abstraction.
2. Define class with syntax.
A)
A class is a code template for creating objects. Objects have member
variables and have behaviour associated with them. In python a class is
created by the keyword class.An object is created using the constructor of
the class. This object will then be called the instance of the class.A Class
is like an object constructor, or a "blueprint" for creating objects.
6. Define Polymorphism
A)
Polymorphism is a concept of object oriented programming, which means
multiple forms or more than one form. Polymorphism enables using a
single interface with input of different datatypes, different class or may be
for different number of inputs.
8. List out the areas in which data structures are applied extensively.
A)
Data structure is applied in the following areas: numerical analysis,
operating system,A.I., compiler design, database management, graphics,
and statistical analysis, to name a few.
● Compiler Design,
● Operating System,
● Database Management System,
● Statistical analysis package,
● Numerical Analysis,
● Graphics,
● Artificial Intelligence,
● and Simulation.
8M Questions:
1. Write the differences between method overloading and method
overriding. Explain with example program
A) Method Overloading:
Method Overloading is an example of Compile time polymorphism. In
this, more than one method of the same class shares the same method
name having different signatures. Method overloading is used to add
more to the behavior of methods and there is no need of more than one
class for method overloading.
Note: Python does not support method overloading. We may overload
the methods but can only use the latest defined method.
Example:
# Function to take multiple arguments
def add(datatype, *args):
# if datatype is int
# initialize answer as 0
if datatype =='int':
answer = 0
# if datatype is str
# initialize answer as ''
if datatype =='str':
answer =''
print(answer)
# Integer
add('int', 5, 6)
# String
add('str', 'Hi ', 'Geeks')
Output:
11
Hi Geeks
Method Overriding:
Method overriding is an example of run time polymorphism. In this, the
specific implementation of the method that is already provided by the
parent class is provided by the child class. It is used to change the
behavior of existing methods and there is a need for at least two classes
for method overriding. In method overriding, inheritance always required
as it is done between parent class(superclass) and child class(child class)
methods.
Example of Method Overriding in python:
class A:
def fun1(self):
print('feature_1 of class A')
def fun2(self):
print('feature_2 of class A')
class B(A):
def fun3(self):
print('feature_3 of class B')
# Create instance
obj = B()
class Student:
def __init__(self):
self.__roll=0
self.__name=””
self.__marks=[]
self.__total=0
self.__per=0
self.__grade=””
self.__result=””
def setStudent(self):
self.__name=input(“Enter Name: “)
for i in range(5):
for x in self.__marks:
self.__total+=x
def Percentage(self):
self.__per=self.__total/5
def Grade(self):
if self.__per>=85:
self.__grade=”S”
elif self.__per>=75:
self.__grade=”A”
elif self.__per>=65:
self.__grade=”B”
elif self.__per>=55:
self.__grade=”C”
elif self.__per>=50:
self.__grade=”D”
else:
self.__grade=”F”
def Result(self):
count=0
for x in self.__marks:
if x>=50:
count+=1
if count==5:
self.__result=”PASS”
elif count>=3:
self.__result=”COMP.”
else:
self.__result=”FAIL”
def printStudent(self):
self.Total()
self.Percentage()
self.Grade()
self.Result()
print(“Roll
No:”,self.__roll,”\t”,”Name:”,self.__name,”\t”,”Total:”,self.__total,”\t”,”Percen
tage:”,self.__per,”\t”,”Grade:”,self.__grade,”\t”,”Result Class:”,self.__result)
def main():
#Student object
s=Student()
s.setStudent()
s.printStudent()
if __name__==”__main__”:
main()
Output :
Enter Roll: 12
Subject 1: 70
Subject 2: 75
Subject 3: 80
Subject 4: 85
Subject 5: 90
Roll No: 12
# Base class
class Parent:
def func1(self):
print("This function is in parent class.")
# Derived class
class Child(Parent):
def func2(self):
print("This function is in child class.")
# Driver's code
object = Child()
object.func1()
object.func2()
Output:
class Base2(object):
def __init__(self):
self.str2 = "Geek2"
print("Base2")
def printStrs(self):
print(self.str1, self.str2)
ob = Derived()
ob.printStrs()
Output:
Base1
Base2
Derived
Geek1 Geek2
# Constructor
def __init__(self, name):
self.name = name
# To get name
def getName(self):
return self.name
# Constructor
def __init__(self, name, age):
Base.__init__(self, name)
self.age = age
# To get name
def getAge(self):
return self.age
# Constructor
def __init__(self, name, age, address):
Child.__init__(self, name, age)
self.address = address
# To get address
def getAddress(self):
return self.address
# Driver code
g = GrandChild("Geek1", 23, "Noida")
print(g.getName(), g.getAge(), g.getAddress())
Output:
Geek1 23 Noida
# Base class
class Parent:
def func1(self):
print("This function is in parent class.")
# Derived class1
class Child1(Parent):
def func2(self):
print("This function is in child 1.")
# Derivied class2
class Child2(Parent):
def func3(self):
print("This function is in child 2.")
# Driver's code
object1 = Child1()
object2 = Child2()
object1.func1()
object1.func2()
object2.func1()
object2.func3()
Output:
This function is in parent class.
This function is in child 1.
This function is in parent class.
This function is in child 2.
class School:
def func1(self):
print("This function is in school.")
class Student1(School):
def func2(self):
print("This function is in student 1. ")
class Student2(School):
def func3(self):
print("This function is in student 2.")
# Driver's code
object = Student3()
object.func1()
object.func2()
Output:
This function is in school.
This function is in student 1.
class GeekforGeeks:
# default constructor
def __init__(self):
self.geek = "GeekforGeeks"
class Addition:
first = 0
second = 0
answer = 0
# parameterized constructor
def __init__(self, f, s):
self.first = f
self.second = s
def display(self):
print("First number = " + str(self.first))
print("Second number = " + str(self.second))
print("Addition of two numbers = " + str(self.answer))
def calculate(self):
self.answer = self.first + self.second
# perform Addition
obj.calculate()
# display result
obj.display()
Output :
First number = 1000
Second number = 2000
Addition of two numbers = 3000
class Base2(object):
def __init__(self):
self.str2 = "Geek2"
print("Base2")
def printStrs(self):
print(self.str1, self.str2)
ob = Derived()
ob.printStrs()
Output:
Base1
Base2
Derived
Geek1 Geek2
# Constructor
def __init__(self, name):
self.name = name
# To get name
def getName(self):
return self.name
# Constructor
def __init__(self, name, age):
Base.__init__(self, name)
self.age = age
# To get name
def getAge(self):
return self.age
# Constructor
def __init__(self, name, age, address):
Child.__init__(self, name, age)
self.address = address
# To get address
def getAddress(self):
return self.address
# Driver code
g = GrandChild("Geek1", 23, "Noida")
print(g.getName(), g.getAge(), g.getAddress())
Output:
Geek1 23 Noida
Python does not support method overloading, that is, it is not possible to define more
than one method with the same name in a class in python. This is because method
arguments in python do not have a type. A method accepting one argument can be
called with an integer value, a string or a double.
B)
8. Define Polymorphism and explain in detail about Runtime
Polymorphism with an example python program.
A)
Polymorphism:-
The word polymorphism means having many forms. In programming,
polymorphism means the same function name (but different signatures)
being used for different types.
Runtime Polymorphism:
It is called “dynamic binding”.
It is method “overriding technique”.
Overriding is the concept of defining a method in Child class with the
same name and same set of arguments in Parent class method.
A Child object can shows the functionality of Parent and Child.
Hence it is called Polymorphic
class Grand:
def fun(self):
print("Grand")
return
class Parent(Grand):
def fun(self):
print("Parent")
return
class Child(Parent):
def fun(self):
print("Child")
return
class Override:
def main():
obj = Child()
obj.fun()
super(Child,obj).fun()
super(Parent,obj).fun()
return
Override.main()
9. What is Operator Overloading? Explain Overloading in
Comparison operator (< less than) with an example python program.
A)
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.
Overloading in Comparison operator (< less than) example:-
# Python program to overload
equality
# and less than operators
class A:
def __init__(self, a):
self.a = a
def __lt__(self, other):
if(self.a<other.a):
return "ob1 is lessthan
ob2"
else:
return "ob2 is less than
ob1"
def __eq__(self, other):
if(self.a == other.a):
return "Both are equal"
else:
return "Not equal"
ob1 = A(2)
ob2 = A(3)
print(ob1 < ob2)
ob3 = A(4)
ob4 = A(4)
print(ob1 == ob2)
Output :
Array.
Stack.
Applications: Evaluation of expressions, Backtracking, Runtime memory
management, Arrangement of books in a library
Queue.
Applications: Disk scheduling, CPU scheduling, File IO, Data
transmission
Linked list.
Applications: Representation of sparse matrices, Non-contiguous data
storage, Implementation of non-binary tree or other data structures,
Dynamic memory management, Equalizing parenthesis, Symbol tables
Tree.
Applications: Representation of data lists, Quickly accessible data
storage, Representation of hierarchal data, Routing of algorithms
Graph.
Applications: Computer networking, Problem solutions involving ‘Depth-
First’ search or ‘Breadth-First’ search algorithms, Representation of
matrices, Study of molecular interactions in Chemistry
File
Applications: Implementation of computer programs, Data comparison,
Storage of data having varying data types
Hash table.
Applications: Unique data representation, Implementation of caches,
Array association, Locating entries in a table, Representation of objects,
Database indexing
class Person:
def __init__(self, name, age=0):
self.name = name
self.age = age
def display(self):
print(self.name)
print(self.age)
Python Inheritance
Inheritance enables us to define a class that takes all the functionality from a
parent class and allows us to add more. In this tutorial, you will learn to use
inheritance in Python.
Inheritance in Python
class BaseClass:
class DerivedClass(BaseClass):
Derived class inherits features from the base class where new features can
be added to it. This results in re-usability of code.
class Person(object):
# Constructor
self.name = name
# To get name
def getName(self):
return self.name
return False
class Employee(Person):
def isEmployee(self):
return True
# Driver code
print(emp.getName(), emp.isEmployee())
print(emp.getName(), emp.isEmployee())
Output:
Geek1 False
Geek2 True
polymorphism
class Cat:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(f"I am a cat. My name is {self.name}. I am {self.age} years old.")
def make_sound(self):
print("Meow")
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(f"I am a dog. My name is {self.name}. I am {self.age} years
old.")
def make_sound(self):
print("Bark")
Output
Meow
I am a cat. My name is Kitty. I am 2.5 years old.
Meow
Bark
I am a dog. My name is Fluffy. I am 4 years old.
Bark
Abstraction is used to hide the internal functionality of the function from the
users. The users only interact with the basic implementation of the
function, but inner working is hidden. User is familiar with that "what
function does" but they don't know "how it does."
A class that consists of one or more abstract method is called the abstract
class. Abstract methods do not contain their implementation. Abstract
class can be inherited by the subclass and abstract method gets its
definition in the subclass. Abstraction classes are meant to be the
blueprint of the other class. An abstract class can be useful when we are
designing large functions. An abstract class is also helpful to provide the
standard interface for different implementations of components. Python
provides the abc module to use the abstraction in the Python program.
Let's see the following syntax.
Syntax
Example -
Output:
A)
Encapsulation:
class Person:
def __init__(self, name, age=0):
self.name = name
self.age = age
def display(self):
print(self.name)
print(self.age)