100% found this document useful (1 vote)
3K views

Exercises With Solutions On OOP

The document provides exercises and solutions for learning object-oriented programming in Python. It includes exercises to create classes for rectangles, persons, bank accounts, circles, computations, books, geometry, strings, and Tkinter. The exercises demonstrate how to define classes, add attributes and methods, inherit from other classes, and create instances of classes.

Uploaded by

Bianca Balaita
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
3K views

Exercises With Solutions On OOP

The document provides exercises and solutions for learning object-oriented programming in Python. It includes exercises to create classes for rectangles, persons, bank accounts, circles, computations, books, geometry, strings, and Tkinter. The exercises demonstrate how to define classes, add attributes and methods, inherit from other classes, and create instances of classes.

Uploaded by

Bianca Balaita
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Exercises with solutions on OOP - object oriented programming in Python

https://pynative.com/python-object-oriented-programming-oop-exercise/

https://anandology.com/python-practice-book/object_oriented_programming.html

https://hub-courses.pages.pasteur.fr/python-solutions/
Object_Oriented_Programming.html

https://www.rithmschool.com/courses/python-fundamentals-part-2/python-object-
oriented-programming-exercises

https://gist.github.com/KirosG/f265f136bd97bd669632fa0f2f2721b4

https://pynative.com/python/object-oriented-programming/

Exercise 41. Rectangle class: ||  Solution

1. Write a Rectangle class in Python language, allowing you to build a rectangle


with length and width attributes.
2. Create a Perimeter() method to calculate the perimeter of the rectangle and
a Area() method to calculate the area of the rectangle.
3. Create a method display() that display the length, width, perimeter and area of an
object created using an instantiation on rectangle class.
4. Create a Parallelepipede child class inheriting from the Rectangle class and with
a height attribute and another Volume() method to calculate the volume of
the Parallelepiped.

Exercice 42: Person class and child Student class ||  Solution

1. Create a Python class Person with attributes: name and age of type string.


2. Create a display() method that displays the name and age of an object created via the
Person class.
3. Create a child class Student  which inherits from the Person class and which also
has a section attribute.
4. Create a method displayStudent() that displays the name, age and section of an
object created via the Student class.
5. Create a student object via an instantiation on the Student class and then test the
displayStudent method.
Exercise 43. Bank Account class: ||  Solution

1. Create a Python class called BankAccount which represents a bank account, having


as attributes: accountNumber (numeric type), name (name of the account owner as
string type), balance.
2. Create a constructor with parameters: accountNumber, name, balance.
3. Create a Deposit() method which manages the deposit actions.
4. Create a Withdrawal() method  which manages withdrawals actions.
5. Create an bankFees() method to apply the bank fees with a percentage of 5% of the
balance account.
6. Create a display() method to display account details.
7. Give the complete code for the  BankAccount class.

Exercise 44. Circle class || Solution

1 - Define a Circle class allowing to create a circleC (O, r) with center O(a, b) and radius


r using the constructor:

def __init__(self,a,b,r):

self.a = a

self.b = b

self.r = r

2 - Define a Area() method of the class which calculates the area of the circle.
3 - Define a Perimeter() method of the class which allows you to calculate the perimeter of
the circle.
4 - Define a testBelongs() method of the class which allows to test whether a point A(x,
y) belongs to the circle C(O, r) or not.

Exercise 45. Computation class || Solution

1 - Create a Coputation class with a default constructor (without parameters) allowing to


perform various calculations on integers numbers.
2 - Create a method called Factorial() which allows to calculate the factorial of an integer.
Test the method by instantiating the class.
3 - Create a method called Sum() allowing to calculate the sum of the first n integers 1 + 2 +
3 + .. + n. Test this method.
4 - Create a method called testPrim() in  the Calculation class to test the primality of a given
integer. Test this method.
4 - Create  a method called testPrims() allowing to test if two numbers are prime between
them.
5 - Create a tableMult() method which creates and displays the multiplication table of a
given integer. Then create an allTablesMult() method to display all the integer multiplication
tables 1, 2, 3, ..., 9.
6 - Create a static listDiv() method that gets all the divisors of a given integer on new list
called  Ldiv. Create another listDivPrim() method that gets all the prime divisors of a given
integer.

Exercise 46 Class Book || Solution

1.  Define a Book class with the following attributes: Title, Author (Full name), Price.
2. Define a constructor used to initialize the attributes of the method with values
entered by the user.
3. Set the View() method to display information for the current book.
4. Write a program to testing the Book class.

Exercise 47 Class Geometry || Solution

Write a Geometry class with default constructor having no parameters.

1. Write a methode in Geometry class called distance() that allow to compute a distance


between two points A = (a1, a2), B = (b1, b2) (with the convention: a point M is
identified with its pair of coordinates M = ( x, y)).
2.  Write a methode in Geometry class called middle() allowing to determine the midle
of bipoint (A,B).
3. Write method called trianglePerimeter() allowing to compute the perimeter of
triangle
4. Write method called triangleIsoscel() which returns a True if the triangle is
isoscel and False if not.

Exercise 48 Class String || Solution

Coding a class named myString inheriting from the str class allowing to endow strings


with append() and pop () methods doing the same operations as those of lists class.

Exercise 49 Class Tkinter Extended|| Solution

1- Create a class called TK_extended which inherits from TK class and having the attributes:
- Master: that represents the name of the main window
- title: that represents the title of the main window
2 - Create a method called create() that creates the window
3 - Create a method called resize(width, height) that can resize the window.
4 - Create a method called generate() to generate the window

The basic idea behind an object-oriented programming (OOP) is to combine


both data and associated procedures (known as methods) into a single unit
which operate on the data. Such a unit is called an object.

You may read our Python classes tutorial before solving the following


exercises.

[An editor is available at the bottom of the page to write and execute the
scripts.]

Python class, Basic exercises [12 exercises with solution]

1. Write a Python program to import built-in array module and display the
namespace of the said module. Go to the editor

Click me to see the solution

2. Write a Python program to create a class and display the namespace of the
said class. Go to the editor

Click me to see the solution


3. Write a Python program to create an instance of a specified class and
display the namespace of the said instance. Go to the editor

Click me to see the solution

4. 'builtins' module provides direct access to all 'built-in' identifiers of Python.


Write a python program which import the abs() function using the builtins
module, display the documentation of abs() function and find the absolute
value of -155. Go to the editor

Click me to see the solution

5. Define a Python function student(). Using function attributes display the


names of all arguments. Go to the editor

Click me to see the solution

6. Write a Python function student_data () which will print the id of a student


(student_id). If the user passes an argument student_name or student_class
the function will print the student name and class. Go to the editor

Click me to see the solution

7. Write a simple Python class named Student and display its type. Also,
display the __dict__ attribute keys and the value of the __module__ attribute
of the Student class. Go to the editor

Click me to see the solution

8. Write a Python program to crate two empty classes, Student and Marks.
Now create some instances and check whether they are instances of the said
classes or not. Also, check whether the said classes are subclasses of the
built-in object class or not. Go to the editor

Click me to see the solution

9. Write a Python class named Student with two attributes student_name,


marks. Modify the attribute values of the said class and print the original and
modified values of the said attributes. Go to the editor

Click me to see the solution


10. Write a Python class named Student with two attributes student_id,
student_name. Add a new attribute student_class and display the entire
attribute and their values of the said class. Now remove the student_name
attribute and display the entire attribute with values. Go to the editor

Click me to see the solution

11. Write a Python class named Student with two attributes student_id,


student_name. Add a new attribute student_class. Create a function to display
the entire attribute and their values in Student class. Go to the editor

Click me to see the solution

12. Write a Python class named Student with two instances student1,


student2 and assign given values to the said instances attributes. Print all the
attributes of student1, student2 instances with their values in the given
format. Go to the editor

Click me to see the solution

Python class, Basic application [12 exercises with solution]

1. Write a Python class to convert an integer to a roman numeral. Go to the


editor

Click me to see the solution

2. Write a Python class to convert a roman numeral to an integer. Go to the


editor

Click me to see the solution

3. Write a Python class to find validity of a string of parentheses, '(', ')', '{', '}', '['
and ']. These brackets must be close in the correct order, for example "()" and
"()[]{}" are valid but "[)", "({[)]" and "{{{" are invalid. Go to the editor

Click me to see the solution

4. Write a Python class to get all possible unique subsets from a set of distinct
integers. Go to the editor
Input : [4, 5, 6]
Output : [[], [6], [5], [5, 6], [4], [4, 6], [4, 5], [4, 5, 6]]
Click me to see the solution

5. Write a Python class to find a pair of elements (indices of the two numbers)
from a given array whose sum equals a specific target number.
Note: There will be one solution for each input and do not use the same
element twice. Go to the editor
Input: numbers= [10,20,10,40,50,60,70], target=50
Output: 3, 4

Difficulty: Medium. Company: Google, Facebook

Click me to see the solution

6. Write a Python class to find the three elements that sum to zero from a set
of n real numbers. Go to the editor
Input array : [-25, -10, -7, -3, 2, 4, 8, 10]
Output : [[-10, 2, 8], [-7, -3, 10]]

Click me to see the solution

7. Write a Python class to implement pow(x, n). Go to the editor

Click me to see the solution

8. Write a Python class to reverse a string word by word. Go to the editor


Input string : 'hello .py'
Expected Output : '.py hello'

Click me to see the solution

9. Write a Python class which has two methods get_String and print_String.
get_String accept a string from the user and print_String print the string in
upper case. Go to the editor
Click me to see the solution

10. Write a Python class named Rectangle constructed by a length and width


and a method which will compute the area of a rectangle. Go to the editor
Click me to see the solution

11. Write a Python class named Circle constructed by a radius and two


methods which will compute the area and the perimeter of a circle. Go to the
editor
Click me to see the solution

12. Write a Python program to get the class name of an instance in Python. Go to


the editor
Click me to see the solution

You might also like