0% found this document useful (0 votes)
5 views7 pages

Exercises On Classes and Objects, Files and Exceptions

The document outlines exercises related to classes and objects in Python, including creating a Bus class that inherits from a Vehicle class, implementing a BankAccount class, and handling exceptions. It emphasizes concepts like polymorphism, method overriding, and managing student records using file handling and exception handling. Additionally, it provides sample input/output scenarios for various programming tasks.

Uploaded by

Pratik
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)
5 views7 pages

Exercises On Classes and Objects, Files and Exceptions

The document outlines exercises related to classes and objects in Python, including creating a Bus class that inherits from a Vehicle class, implementing a BankAccount class, and handling exceptions. It emphasizes concepts like polymorphism, method overriding, and managing student records using file handling and exception handling. Additionally, it provides sample input/output scenarios for various programming tasks.

Uploaded by

Pratik
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/ 7

Exercises on Classes and Objects

1. Create a child class Bus that will


inherit all of the variables and methods
of the Vehicle class

2. Define a property that must have


the same value for every class instance
(object)
Define a class attribute”color” with a default value white. I.e.,
Every Vehicle should be white.
3. Create a Bus child class that inherits from the Vehicle class.
The default fare charge of any vehicle is seating capacity *
100. If Vehicle is Bus instance, we need to add an extra 10%
on full fare as a maintenance charge. So total fare for bus
instance will become the final amount = total fare + 10%
of the total fare.

Note: The bus seating capacity is 50. so the final fare amount
should be 5500. You need to override the fare() method of a
Vehicle class in Bus class.

Use the following code for your parent Vehicle class. We need
to access the parent class from inside a method of a child
class.
4) Create a class BankAccount with:

 Attributes: account_number, balance


 Methods:
o deposit(amount): Adds money to the account
o withdraw(amount): Deducts money if sufficient balance is available

Input:

Output:

5) Create a class Circle with a method area() that returns the area of the circle. Take the radius
as input while creating an object.
6) Create a parent class Vehicle with attributes brand and model. Create a child class Car that
inherits Vehicle and adds a new attribute seats. Create an object of Car and display all attributes.

7) Polymorphism with Method Overriding

Create a base class Shape with a method area(). Create two child classes Rectangle and Circle
that override the area() method.

Exception Handling

1) Write a Python program that takes two numbers as input and divides the first number by the
second. Handle the case where the second number is zero.

Sample Input / Output

2) Write a Python program that takes a number from the user and converts it to an integer.
Handle cases where the input is not a valid number.

Sample Input / Output


3) Write a Python program that attempts to open a file and reads its contents. Handle the case
where the file does not exist.
Sample Input / Output

4) Write a Python program that takes two numbers as input, performs division, and ensures
that a message is printed regardless of whether an exception occurs.

Sample Input / Output


Concept: Student Records Management Using File Handling
& Exception Handling

Key Features:

1. Uses a dictionary to store student records (Name -> Marks).


2. Reads and writes data from a plain text file (students.txt).
3. Uses exception handling for:
o Handling missing files (FileNotFoundError).
o Handling invalid inputs (ValueError).
o Ensuring smooth file operations.

Sample Input / Output


How This Works

1. load_data() → Reads data from students.txt and loads it into a dictionary.


2. save_data(data) → Writes dictionary data back into students.txt in Name:Marks
format.
3. Exception Handling:
o FileNotFoundError → If file is missing, it creates a new one.
o ValueError → Handles invalid inputs (e.g., non-numeric marks).
o try-except inside load_data() → Prevents crashing due to malformed file data.
4. User Menu:
o Allows adding, viewing, searching, updating, and deleting student records.

You might also like