Object-Oriented Programming (OOP) Practice Questions
1. Create a class BankAccount with private fields: accountNumber and balance. Use getter/setter methods
to access/update them.
2. Make a class Student with private fields: name, grade. Create methods to set values and print them.
3. Write a program where Employee details (name, ID, salary) are hidden using encapsulation, and
accessed via public methods.
4. Define a class Product with fields id, price. Ensure fields are private and values are set only via setters.
5. Create a class Patient with a private field temperature. Use public methods to set and get the temperature
value.
6. Create a base class Animal with a method makeSound(). Create a subclass Dog that overrides this
method.
7. Design a program that accepts two integer inputs from the user and generates all the prime numbers
between those two values (inclusive). The program should validate that the starting number is smaller
than the ending number and provide appropriate error messages if the input is invalid (e.g., negative
numbers or non-integers). Additionally, the program should display the total number of prime numbers
found within the range.
8. Define a class Person with fields name and age. Create a subclass Teacher that adds a subject.
9. Write a class Vehicle with a method start(). Derive a class Car that adds fuelType and overrides start().
10. Make a base class Shape with a method area(). Create a subclass Rectangle that calculates area.
11. Create a program that generates the first N numbers in the Fibonacci sequence, where N is an input by
the user. Ensure that the program handles large values of N efficiently. Additionally, add an option for
the user to specify whether they want the Fibonacci sequence to be displayed as a list or as a comma-
separated string. Include error handling to prevent invalid inputs, such as negative numbers or non-
integer values.
12. Create a class Employee and a subclass Manager that adds a field teamSize. Use constructors in both
classes.
13. Create a class Calculator with overloaded methods add() for int, double, and three parameters.
14. Define a class Printer with a method print() overloaded to print strings, integers, and arrays.
15. Create a base class Bird with a method fly(). Override it in Sparrow and Eagle.
16. Write a class Shape with a method draw(). Override it in Circle and Triangle.
17. Implement method overloading for greet() in a class Welcome: one version takes no input, another takes
a name.
18. Create an abstract class Shape with an abstract method area(). Implement it in the subclass Square.
19. Define an abstract class Appliance with a method turnOn(). Implement it in the WashingMachine class.
20. Write a program that demands the number of units from the user if the units are greater than or equal to
500. The per-unit cost will be 70 rupees if the number of units is greater than 300 and less than 500. If
the number of units is less than 300, the per-unit cost will be 20 rupees. Additionally, 200 rupees of
fixed tax will be added in each case. Furthermore, if the total amount after the fixed tax is equal to or
greater than 20,000, a tax of 10% will be added. If the total amount is greater than or equal to 10,000
and less than 20,000, the tax amount will be 5%. Otherwise, a fine of 1,000 rupees will be collected.
Develop the output if a user enters the units equal to 200, 300, and 500.
21. Create an interface Flyable with a method fly(). Implement it in Airplane and Bird.
22. Write an abstract class Account with an abstract method calculateInterest(). Implement in
SavingsAccount.
23. Define interface Playable with method play(). Create classes Guitar and Piano that implement the
interface.