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

Python Classes Inheritance Practice

The document provides practice questions and solutions for Python classes and inheritance, covering topics such as creating classes, methods, and various types of inheritance (single, multilevel, hierarchical, multiple, and hybrid). It includes examples for classes like Student, Rectangle, BankAccount, Vehicle, Employee, and more. Additionally, it lists important theory questions related to inheritance and object-oriented programming concepts in Python.

Uploaded by

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

Python Classes Inheritance Practice

The document provides practice questions and solutions for Python classes and inheritance, covering topics such as creating classes, methods, and various types of inheritance (single, multilevel, hierarchical, multiple, and hybrid). It includes examples for classes like Student, Rectangle, BankAccount, Vehicle, Employee, and more. Additionally, it lists important theory questions related to inheritance and object-oriented programming concepts in Python.

Uploaded by

Jay Khinchi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Classes and Inheritance Practice Questions with

Solutions

1. Create a Student class with attributes name, roll_no, and marks. Add a display() method.

Solution:
class Student:
def __init__(self, name, roll_no, marks):
[Link] = name
self.roll_no = roll_no
[Link] = marks

def display(self):
print([Link], self.roll_no, [Link])

s1 = Student("Rahul", 1, 89)
[Link]()

2. Create a Rectangle class with area() and perimeter() methods.

Solution:
class Rectangle:
def __init__(self, length, breadth):
[Link] = length
[Link] = breadth

def area(self):
return [Link] * [Link]

def perimeter(self):
return 2 * ([Link] + [Link])

r = Rectangle(10, 5)
print([Link]())
print([Link]())

3. Create a BankAccount class with deposit(), withdraw(), and show_balance() methods.

Solution:
class BankAccount:
def __init__(self, name, balance):
[Link] = name
[Link] = balance

def deposit(self, amount):


[Link] += amount

def withdraw(self, amount):


[Link] -= amount

def show_balance(self):
print([Link])

b = BankAccount("Aman", 5000)
[Link](1000)
[Link](500)
b.show_balance()

4. Demonstrate Single Inheritance using Vehicle and Car classes.


Solution:
class Vehicle:
def start(self):
print("Vehicle Started")

class Car(Vehicle):
def display(self):
print("This is a Car")

c = Car()
[Link]()
[Link]()

5. Demonstrate Single Inheritance using Employee and Manager classes.

Solution:
class Employee:
def __init__(self, name, salary):
[Link] = name
[Link] = salary

class Manager(Employee):
def __init__(self, name, salary, department):
super().__init__(name, salary)
[Link] = department

def display(self):
print([Link], [Link], [Link])

m = Manager("Ravi", 70000, "HR")


[Link]()

6. Demonstrate Multilevel Inheritance using Animal, Dog, and Puppy classes.

Solution:
class Animal:
def eat(self):
print("Eating")

class Dog(Animal):
def bark(self):
print("Barking")

class Puppy(Dog):
def weep(self):
print("Weeping")

p = Puppy()
[Link]()
[Link]()
[Link]()

7. Demonstrate Hierarchical Inheritance using Shape, Circle, and Square classes.

Solution:
class Shape:
def message(self):
print("This is Shape")

class Circle(Shape):
def area(self, r):
print(3.14 * r * r)

class Square(Shape):
def area(self, s):
print(s * s)
c = Circle()
[Link]()
[Link](5)

sq = Square()
[Link]()
[Link](4)

8. Demonstrate Multiple Inheritance using Father, Mother, and Child classes.

Solution:
class Father:
def skills1(self):
print("Gardening")

class Mother:
def skills2(self):
print("Cooking")

class Child(Father, Mother):


def skills3(self):
print("Coding")

c = Child()
c.skills1()
c.skills2()
c.skills3()

9. Demonstrate Hybrid Inheritance using classes A, B, C, and D.

Solution:
class A:
def showA(self):
print("Class A")

class B(A):
def showB(self):
print("Class B")

class C(A):
def showC(self):
print("Class C")

class D(B, C):


def showD(self):
print("Class D")

d = D()
[Link]()
[Link]()
[Link]()
[Link]()

10. Create a Person and Teacher class using super().

Solution:
class Person:
def __init__(self, name, age):
[Link] = name
[Link] = age

class Teacher(Person):
def __init__(self, name, age, subject):
super().__init__(name, age)
[Link] = subject

def display(self):
print([Link], [Link], [Link])
t = Teacher("Neha", 30, "Math")
[Link]()

Important Theory Questions

• What is inheritance in Python?


• Difference between class and object.
• What is the use of super()?
• Difference between single and multiple inheritance.
• What is method overriding?

You might also like