OOPS (Object-Oriented Programming) Practice
Core OOP concepts: classes, objects, methods, attributes and design.
1
Create a Simple Class
Easy
Write a Python class Person with an attribute name. Create an object, set name to the input, and print it.
Input Format
A single line containing a name.
Output Format
Print the person's name.
Sample Test Cases
|
Sample Input 1
Alice |
Sample Output 1
Alice |
|
Sample Input 2
Bob |
Sample Output 2
Bob |
|
Sample Input 3
Sam |
Sample Output 3
Sam |
2
Class with Greet Method
Easy
Write a class Person with a method greet() that prints 'Hello, [name]!'. Create an object and call greet().
Input Format
A single line containing a name.
Output Format
Print the greeting message.
Sample Test Cases
|
Sample Input 1
Bob |
Sample Output 1
Hello, Bob! |
|
Sample Input 2
Alice |
Sample Output 2
Hello, Alice! |
|
Sample Input 3
|
Sample Output 3
Hello, ! |
3
Store and Print Age
Easy
Write a class Student with attributes name and age. Read name and age, create an object, and print both.
Input Format
First line: name. Second line: age.
Output Format
Print 'Name: [name]' and 'Age: [age]' on separate lines.
Sample Test Cases
|
Sample Input 1
Alice 20 |
Sample Output 1
Name: Alice Age: 20 |
|
Sample Input 2
Bob 30 |
Sample Output 2
Name: Bob Age: 30 |
|
Sample Input 3
Cara 18 |
Sample Output 3
Name: Cara Age: 18 |
4
Class Method to Return Square
Easy
Write a class Calculator with a method square(n) that returns n*n. Create an object and print the square of the input.
Input Format
A single line containing integer n.
Output Format
Print the square.
Sample Test Cases
|
Sample Input 1
7 |
Sample Output 1
49 |
|
Sample Input 2
8 |
Sample Output 2
64 |
|
Sample Input 3
0 |
Sample Output 3
0 |
5
Class with Multiple Methods
Easy
Write a class Rectangle with methods area() and perimeter() using length and breadth attributes.
Input Format
A single line containing two space-separated integers length and breadth.
Output Format
Print area on line 1 and perimeter on line 2.
Sample Test Cases
|
Sample Input 1
4 6 |
Sample Output 1
24 20 |
|
Sample Input 2
2 3 |
Sample Output 2
6 10 |
|
Sample Input 3
1 1 |
Sample Output 3
1 4 |
6
Book Class with Title
Easy
Write a class Book with attribute title. Read a title, create an object, and print it.
Input Format
A single line containing the book title.
Output Format
Print the book title.
Sample Test Cases
|
Sample Input 1
Python Basics |
Sample Output 1
Python Basics |
|
Sample Input 2
Learn Python |
Sample Output 2
Learn Python |
|
Sample Input 3
OOP |
Sample Output 3
OOP |
7
Circle Class — Area
Easy
Write a class Circle with radius attribute and a method area() that returns 3.14 * r * r.
Input Format
A single line containing the radius r.
Output Format
Print the area rounded to 2 decimal places.
Sample Test Cases
|
Sample Input 1
5 |
Sample Output 1
78.50 |
|
Sample Input 2
10 |
Sample Output 2
314.00 |
|
Sample Input 3
1 |
Sample Output 3
3.14 |
8
Object Attribute Update
Easy
Write a class Car with attribute speed. Create an object with speed 0, read a new speed, update the attribute, and print it.
Input Format
A single line containing the new speed.
Output Format
Print the updated speed.
Sample Test Cases
|
Sample Input 1
60 |
Sample Output 1
60 |
|
Sample Input 2
100 |
Sample Output 2
100 |
|
Sample Input 3
0 |
Sample Output 3
0 |
9
Two Objects of Same Class
Easy
Write a class Dog with method bark() that prints 'Woof!'. Create two objects and call bark() on both.
Input Format
No input.
Output Format
Print 'Woof!' twice, once per line.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
Woof! Woof! |
|
Sample Input 2
|
Sample Output 2
Woof! Woof! |
|
Sample Input 3
|
Sample Output 3
Woof! Woof! |
10
Class with Display Method
Easy
Write a class Employee with name and salary attributes and a display() method that prints both.
Input Format
First line: name. Second line: salary.
Output Format
Print 'Name: [name]' and 'Salary: [salary]' on separate lines.
Sample Test Cases
|
Sample Input 1
Ravi 45000 |
Sample Output 1
Name: Ravi Salary: 45000 |
|
Sample Input 2
Priya 50000 |
Sample Output 2
Name: Priya Salary: 50000 |
|
Sample Input 3
Amit 30000 |
Sample Output 3
Name: Amit Salary: 30000 |
1
Class with __str__ Method
Medium
Write a class Point with x and y attributes and a __str__ method that returns '(x, y)'. Read x and y and print the object.
Input Format
A single line containing two space-separated integers x and y.
Output Format
Print the point as (x, y).
Sample Test Cases
|
Sample Input 1
3 4 |
Sample Output 1
(3, 4) |
|
Sample Input 2
0 0 |
Sample Output 2
(0, 0) |
|
Sample Input 3
5 -2 |
Sample Output 3
(5, -2) |
2
Bank Account with Methods
Medium
Write a class BankAccount with methods deposit(amount) and get_balance(). Start with balance 0, perform a deposit, and print the balance.
Input Format
A single line containing the deposit amount.
Output Format
Print the balance after deposit.
Sample Test Cases
|
Sample Input 1
500 |
Sample Output 1
500 |
|
Sample Input 2
1000 |
Sample Output 2
1000 |
|
Sample Input 3
0 |
Sample Output 3
0 |
3
Class with Class Variable
Medium
Write a class Employee with a class variable company = 'TechBridge'. Read an employee name, create an object, and print 'name works at TechBridge'.
Input Format
A single line containing an employee name.
Output Format
Print the sentence with the company name.
Sample Test Cases
|
Sample Input 1
Alice |
Sample Output 1
Alice works at TechBridge |
|
Sample Input 2
Bob |
Sample Output 2
Bob works at TechBridge |
|
Sample Input 3
|
Sample Output 3
works at TechBridge |
4
Object Count Using Class Variable
Medium
Write a class Counter with a class variable count that increments every time an object is created. Create 3 objects and print the count.
Input Format
No input.
Output Format
Print the number of objects created.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
3 |
|
Sample Input 2
|
Sample Output 2
3 |
|
Sample Input 3
|
Sample Output 3
3 |
5
Instance vs Class Attributes
Medium
Write a class Student with class attribute school = 'Green Valley' and instance attribute name. Read a name, create an object, and print both.
Input Format
A single line containing a name.
Output Format
Print 'Name: [name]' and 'School: [school]' on separate lines.
Sample Test Cases
|
Sample Input 1
Alice |
Sample Output 1
Name: Alice School: Green Valley |
|
Sample Input 2
Bob |
Sample Output 2
Name: Bob School: Green Valley |
|
Sample Input 3
|
Sample Output 3
Name: School: Green Valley |
6
Method Calling Another Method
Medium
Write a class Calculator with methods add(a, b) and add_then_double(a, b) that calls add() and doubles the result.
Input Format
A single line containing two space-separated integers.
Output Format
Print the doubled sum.
Sample Test Cases
|
Sample Input 1
5 6 |
Sample Output 1
22 |
|
Sample Input 2
3 4 |
Sample Output 2
14 |
|
Sample Input 3
0 1 |
Sample Output 3
2 |
7
Simple Shopping Cart
Medium
Write a class Cart with an items list and methods add(item) and show(). Add 2 items from input and print the cart contents.
Input Format
Two lines, each containing an item name.
Output Format
Print the list of items.
Sample Test Cases
|
Sample Input 1
apple mango |
Sample Output 1
['apple', 'mango'] |
|
Sample Input 2
tea coffee |
Sample Output 2
['tea', 'coffee'] |
|
Sample Input 3
juice water |
Sample Output 3
['juice', 'water'] |
8
Comparing Two Objects
Medium
Write a class Student with name and marks. Read two students' data, create objects, and print the name of the student with higher marks.
Input Format
First line: name and marks of student 1. Second line: name and marks of student 2.
Output Format
Print the name of the student with higher marks.
Sample Test Cases
|
Sample Input 1
Alice 85 Bob 90 |
Sample Output 1
Bob |
|
Sample Input 2
X 60 Y 80 |
Sample Output 2
Y |
|
Sample Input 3
P 90 Q 85 |
Sample Output 3
P |
9
Class Method for Greeting
Medium
Write a class Greeter with a static-like method greet(name) accessed via the class (classmethod) that returns 'Hello, [name]!'.
Input Format
A single line containing a name.
Output Format
Print the greeting.
Sample Test Cases
|
Sample Input 1
Sam |
Sample Output 1
Hello, Sam! |
|
Sample Input 2
Tom |
Sample Output 2
Hello, Tom! |
|
Sample Input 3
Jerry |
Sample Output 3
Hello, Jerry! |
10
Product Class with Discount
Medium
Write a class Product with price and discount attributes and a method final_price() that returns price minus discount.
Input Format
A single line containing price and discount space-separated.
Output Format
Print the final price.
Sample Test Cases
|
Sample Input 1
1000 150 |
Sample Output 1
850 |
|
Sample Input 2
500 50 |
Sample Output 2
450 |
|
Sample Input 3
2000 0 |
Sample Output 3
2000 |
1
Design a Library System
Hard
Write classes Book and Library. Library has a list of books and methods add_book(book) and show_books(). Add 3 books and print all titles.
Input Format
Three lines, each containing a book title.
Output Format
Print each book title on a new line.
Sample Test Cases
|
Sample Input 1
Python Java C++ |
Sample Output 1
Python Java C++ |
|
Sample Input 2
A B C |
Sample Output 2
A B C |
|
Sample Input 3
X Y Z |
Sample Output 3
X Y Z |
2
Shape Hierarchy with Area
Hard
Write a base class Shape with method area() and derived classes Circle and Rectangle that override it. Read shape type and dimensions, and print the area.
Input Format
First line: shape type ('circle' or 'rectangle'). Next lines contain the needed dimensions.
Output Format
Print the area rounded to 2 decimal places.
Sample Test Cases
|
Sample Input 1
circle 5 |
Sample Output 1
78.50 |
|
Sample Input 2
rectangle 4 6 |
Sample Output 2
24.00 |
|
Sample Input 3
circle 1 |
Sample Output 3
3.14 |
3
Student Report Card System
Hard
Write a class Student with marks list and methods add_mark(m), average(), and grade(). Add 3 marks and print average and grade.
Input Format
A single line containing three space-separated marks.
Output Format
Print 'Average: [avg]' and 'Grade: [grade]' on separate lines (A ≥ 90, B ≥ 75, C ≥ 60, else F).
Sample Test Cases
|
Sample Input 1
80 90 85 |
Sample Output 1
Average: 85.0 Grade: B |
|
Sample Input 2
50 60 70 |
Sample Output 2
Average: 60.0 Grade: C |
|
Sample Input 3
95 98 99 |
Sample Output 3
Average: 97.33 Grade: A |
4
Employee with Salary Raise
Hard
Write a class Employee with name, base salary, and a method apply_raise(percent) that updates the salary.
Input Format
First line: name and salary. Second line: raise percent.
Output Format
Print the updated salary.
Sample Test Cases
|
Sample Input 1
Alice 40000 10 |
Sample Output 1
44000.0 |
|
Sample Input 2
Bob 30000 20 |
Sample Output 2
36000.0 |
|
Sample Input 3
Cara 50000 0 |
Sample Output 3
50000.0 |
5
Simple Banking with Withdrawal
Hard
Write a class BankAccount with balance and methods deposit(amount) and withdraw(amount) that print 'Insufficient funds' if balance is not enough.
Input Format
First line: initial balance. Second line: deposit amount. Third line: withdrawal amount.
Output Format
Print balance after deposit, then after withdrawal.
Sample Test Cases
|
Sample Input 1
1000 500 1200 |
Sample Output 1
1500 Insufficient funds 1500 |
|
Sample Input 2
100 0 50 |
Sample Output 2
100 150 |
|
Sample Input 3
500 100 700 |
Sample Output 3
600 Insufficient funds 600 |
6
Inventory Management
Hard
Write a class Inventory with a dictionary of items and methods add(item, qty) and total_items() that sums all quantities.
Input Format
Multiple lines: each line contains an item and quantity separated by space. End with empty line.
Output Format
Print the total quantity of all items.
Sample Test Cases
|
Sample Input 1
apple 10 banana 5 mango 8 |
Sample Output 1
23 |
|
Sample Input 2
a 1 b 2 |
Sample Output 2
3 |
|
Sample Input 3
x 5 x 5 |
Sample Output 3
10 |
7
OOP — Matrix Class
Hard
Write a class Matrix that stores a 2D list, and a method transpose() that returns the transposed matrix.
Input Format
First line: rows and cols. Next rows lines: row values space-separated.
Output Format
Print the transposed matrix, one row per line.
Sample Test Cases
|
Sample Input 1
2 3 1 2 3 4 5 6 |
Sample Output 1
1 4 2 5 3 6 |
|
Sample Input 2
2 2 1 2 3 4 |
Sample Output 2
1 3 2 4 |
|
Sample Input 3
1 3 1 2 3 |
Sample Output 3
1 2 3 |
8
Vending Machine Simulation
Hard
Write a class VendingMachine with a dict of item prices and methods insert_money(amount), select(item), and balance().
Input Format
First line: amount inserted. Second line: item name.
Output Format
Print 'Dispensed: [item]' and 'Balance: [remaining]'.
Sample Test Cases
|
Sample Input 1
50 chips |
Sample Output 1
Dispensed: chips Balance: 20 |
|
Sample Input 2
20 water |
Sample Output 2
Dispensed: water Balance: 5 |
|
Sample Input 3
100 soda |
Sample Output 3
Dispensed: soda Balance: 65 |
9
OOP — Fraction Class
Hard
Write a class Fraction with numerator and denominator and a method simplify() that reduces the fraction using GCD.
Input Format
A single line containing numerator and denominator space-separated.
Output Format
Print the simplified fraction as 'num/den'.
Sample Test Cases
|
Sample Input 1
8 12 |
Sample Output 1
2/3 |
|
Sample Input 2
6 9 |
Sample Output 2
2/3 |
|
Sample Input 3
10 4 |
Sample Output 3
5/2 |
10
Hotel Room Booking System
Hard
Write a class Hotel with rooms dict and methods book(room) and available_rooms(). Book 2 rooms and print the remaining available rooms.
Input Format
Two lines, each containing a room number.
Output Format
Print the list of remaining available rooms.
Sample Test Cases
|
Sample Input 1
101 102 |
Sample Output 1
[103, 104, 105] |
|
Sample Input 2
201 202 |
Sample Output 2
[103, 104, 105] |
|
Sample Input 3
103 |
Sample Output 3
[104, 105] |
Competitive MCQs — OOPS (Object-Oriented Programming)
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score
0/ 31
Q1
What does OOP stand for?
Correct!
Wrong — correct answer is .
OOP is Object-Oriented Programming — organizing code around objects.
Q2
Which of the following is NOT a core OOP principle?
Correct!
Wrong — correct answer is .
Compilation is not an OOP principle; the pillars are encapsulation, inheritance, polymorphism, abstraction.
Q3
What is an object?
Correct!
Wrong — correct answer is .
A class is the blueprint; an object is a concrete instance of it.
Q4
What is a class in Python?
Correct!
Wrong — correct answer is .
A class defines attributes and methods that its objects will have.
Q5
Which keyword creates an object from a class?
Correct!
Wrong — correct answer is .
In Python, MyClass() calls the constructor and returns an instance.
Q6
What does the __init__ method do?
Correct!
Wrong — correct answer is .
__init__ is the constructor, called automatically on creation.
Q7
What is the first parameter of an instance method?
Correct!
Wrong — correct answer is .
Python passes the instance as the first argument, conventionally named self.
Q8
Which pillar hides internal details and bundles data with methods?
Correct!
Wrong — correct answer is .
Encapsulation bundles data and methods and hides implementation.
Q9
Which pillar lets a child class reuse a parent class's code?
Correct!
Wrong — correct answer is .
Inheritance lets a subclass reuse and extend parent behaviour.
Q10
What is the output of this code?
python
1
2
3
4
5
2
3
4
5
class A:
def greet(self):
return "Hello"
obj = A()
print(obj.greet())Correct!
Wrong — correct answer is .
obj.greet() calls the method, returning "Hello".
Q11
What is the output of this code?
class A:
def greet(self):
return "Hello"
obj = A()
print(obj.greet())
class A:
def greet(self):
return "Hello"
obj = A()
print(obj.greet())
Correct!
Wrong — correct answer is .
obj.greet() calls the method, returning "Hello".
Q12
Which of these is the correct way to define a method inside a class?
Correct!
Wrong — correct answer is .
Methods are def statements whose first parameter is self by convention.
Q13
Which of these is NOT an OOP pillar?
Correct!
Wrong — correct answer is .
Sequencing is a control-flow idea; the OOP pillars are the other three plus abstraction.
Q14
What is the output of this code?
class A:
pass
print(A())
class A:
pass
print(A())
Correct!
Wrong — correct answer is .
Printing an instance shows its class and memory address by default.
Q15
What does abstraction mean in OOP?
Correct!
Wrong — correct answer is .
Abstraction gives users a simple interface while hiding implementation.
Q16
What is the output of this code?
class A:
x = 1
class B(A):
x = 2
print(A.x, B.x)
class A:
x = 1
class B(A):
x = 2
print(A.x, B.x)
Correct!
Wrong — correct answer is .
A.x is 1; B overrides x with its own class attribute 2.
Q17
Which term describes hiding data with getter/setter methods?
Correct!
Wrong — correct answer is .
Encapsulation bundles data and controls access through methods.
Q18
What is the output of this code?
class A:
def __init__(self):
self.x = 1
obj = A()
print(hasattr(obj, "x"))
class A:
def __init__(self):
self.x = 1
obj = A()
print(hasattr(obj, "x"))
Correct!
Wrong — correct answer is .
hasattr() returns True because obj carries the attribute x.
Q19
Which built-in shows the attributes of an object?
Correct!
Wrong — correct answer is .
vars() returns the instance __dict__ holding its attributes.
Q20
What is the output of this code?
class A:
pass
a = A()
print(a.__class__.__name__)
class A:
pass
a = A()
print(a.__class__.__name__)
Correct!
Wrong — correct answer is .
__class__.__name__ gives the class name as a string.
Q21
Which symbol names a class attribute shared by all instances?
Correct!
Wrong — correct answer is .
Assignments in the class body create class-level attributes shared by instances.
Q22
What is the output of this code?
class A:
x = 5
a1 = A()
a2 = A()
print(a1.x + a2.x)
class A:
x = 5
a1 = A()
a2 = A()
print(a1.x + a2.x)
Correct!
Wrong — correct answer is .
Both instances read the same class attribute 5, summing to 10.
Q23
Which statement is TRUE about objects?
Correct!
Wrong — correct answer is .
Objects combine identity, state, and behavior.
Q24
What is the output of this code?
class Dog:
def __init__(self, name):
self.name = name
d = Dog("Rex")
print(d.name)
class Dog:
def __init__(self, name):
self.name = name
d = Dog("Rex")
print(d.name)
Correct!
Wrong — correct answer is .
The constructor stores "Rex" as the instance attribute name.
Q25
Which is the term for calling a method on an object?
Correct!
Wrong — correct answer is .
obj.method() sends a message to the object, invoking its behavior.
Q26
What is the output of this code?
class A:
def __init__(self, v):
self.v = v
def show(self):
return self.v
a = A(7)
print(a.show())
class A:
def __init__(self, v):
self.v = v
def show(self):
return self.v
a = A(7)
print(a.show())
Correct!
Wrong — correct answer is .
show() returns the stored instance value 7.
Q27
What does polymorphism allow in OOP?
Correct!
Wrong — correct answer is .
Polymorphism means one name or interface takes many forms.
Q28
Which concept models "is-a" relationships?
Correct!
Wrong — correct answer is .
A Dog is-an Animal, expressed with class Dog(Animal).
Q29
What is the output of this code?
class Animal:
def speak(self):
return "..."
class Dog(Animal):
def speak(self):
return "Woof"
for a in [Animal(), Dog()]:
print(a.speak(), end=" ")
class Animal:
def speak(self):
return "..."
class Dog(Animal):
def speak(self):
return "Woof"
for a in [Animal(), Dog()]:
print(a.speak(), end=" ")
Correct!
Wrong — correct answer is .
Each object calls its own version of speak(), demonstrating polymorphism.
Q30
Which statement about class and instance attributes is TRUE?
Correct!
Wrong — correct answer is .
Assigning self.x creates an instance attribute that hides the class x.
Q31
What is the output of this code?
class A:
x = 10
class B(A):
pass
print(B.x)
class A:
x = 10
class B(A):
pass
print(B.x)
Correct!
Wrong — correct answer is .
B inherits the class attribute x = 10 from its parent A.