Practicing program-based questions is essential for mastering Object-Oriented Programming concepts in Java ☕
Most examinations and lab assignments ask questions in a structured, descriptive format, where the problem statement clearly specifies:
- Attributes of a class
- Required methods
- Expected operations
This article contains 10 detailed, exam-oriented OOPs practice questions, each followed by a complete Java program and explanation.
All programs are based only on classes, objects, constructors, and methods — no inheritance is used ❌.
📋 List of Practice Questions
1️⃣ Student Class – Accept and Display Details
2️⃣ Employee Class – Calculate Annual Salary
3️⃣ Bank Account Class – Deposit and Withdraw
4️⃣ Rectangle Class – Area and Perimeter
5️⃣ Marks Class – Total and Percentage
6️⃣ Book Class – Constructor Initialization
7️⃣ Product Class – Bill Amount Calculation
8️⃣ Circle Class – Area and Circumference
9️⃣ Time Class – Add Two Time Objects
🔟 Counter Class – Count Number of Objects
🟢 Question 1: Student Class
❓ Question
Design a class Student with the following attributes:
rollNo(int)name(String)
Create the following methods:
setData()to assign valuesdisplay()to display student details
Create another class containing the main() method to create a Student object and invoke the methods.
✅ Program
class Student {
int rollNo;
String name;
void setData(int r, String n) {
rollNo = r;
name = n;
}
void display() {
System.out.println("Roll No: " + rollNo);
System.out.println("Name: " + name);
}
}
public class StudentDemo {
public static void main(String[] args) {
Student s = new Student();
s.setData(101, "Ajay");
s.display();
}
}
🧠 Explanation
Studentclass defines attributes and methodssetData()initializes object datadisplay()prints object values- Object is created and used in
main()
🟢 Question 2: Employee Class
❓ Question
Design a class Employee with attributes:
id(int)name(String)monthlySalary(double)
Create methods:
setData()to initialize valuescalculateAnnualSalary()to calculate yearly salarydisplay()to display details
✅ Program
class Employee {
int id;
String name;
double monthlySalary;
void setData(int i, String n, double s) {
id = i;
name = n;
monthlySalary = s;
}
double calculateAnnualSalary() {
return monthlySalary * 12;
}
void display() {
System.out.println("ID: " + id);
System.out.println("Name: " + name);
System.out.println("Annual Salary: " + calculateAnnualSalary());
}
}
public class EmployeeDemo {
public static void main(String[] args) {
Employee e = new Employee();
e.setData(1, "Rahul", 40000);
e.display();
}
}
🧠 Explanation
- Calculation logic is kept inside a method
- Method calls another method internally
- Promotes modular design 📦
🟢 Question 3: Bank Account Class
❓ Question
Design a class BankAccount with:
accountNobalance
Create methods:
deposit(amount)withdraw(amount)displayBalance()
✅ Program
class BankAccount {
int accountNo;
double balance;
void setAccount(int acc, double bal) {
accountNo = acc;
balance = bal;
}
void deposit(double amount) {
balance += amount;
}
void withdraw(double amount) {
balance -= amount;
}
void displayBalance() {
System.out.println("Account No: " + accountNo);
System.out.println("Balance: " + balance);
}
}
public class BankDemo {
public static void main(String[] args) {
BankAccount acc = new BankAccount();
acc.setAccount(12345, 5000);
acc.deposit(2000);
acc.withdraw(1000);
acc.displayBalance();
}
}
🧠 Explanation
- Object maintains state (
balance) - Methods modify object data
- Demonstrates real-world modeling 🏦
🟢 Question 4: Rectangle Class
❓ Question
Design a class Rectangle with:
lengthbreadth
Create methods:
area()perimeter()
✅ Program
class Rectangle {
int length, breadth;
void setValues(int l, int b) {
length = l;
breadth = b;
}
int area() {
return length * breadth;
}
int perimeter() {
return 2 * (length + breadth);
}
}
public class RectangleDemo {
public static void main(String[] args) {
Rectangle r = new Rectangle();
r.setValues(10, 5);
System.out.println("Area: " + r.area());
System.out.println("Perimeter: " + r.perimeter());
}
}
🧠 Explanation
- Methods return computed values
- Data is stored inside the object
- Clear separation of logic
🟢 Question 5: Marks Class
❓ Question
Design a class Marks with marks of three subjects.
Create methods to:
- Calculate total marks
- Calculate percentage
✅ Program
class Marks {
int m1, m2, m3;
void setMarks(int a, int b, int c) {
m1 = a;
m2 = b;
m3 = c;
}
int total() {
return m1 + m2 + m3;
}
double percentage() {
return total() / 3.0;
}
}
public class MarksDemo {
public static void main(String[] args) {
Marks m = new Marks();
m.setMarks(70, 80, 90);
System.out.println("Total: " + m.total());
System.out.println("Percentage: " + m.percentage());
}
}
🟢 Question 6: Book Class (Constructor)
❓ Question
Design a class Book with attributes:
titleprice
Initialize values using a constructor and display details.
✅ Program
class Book {
String title;
double price;
Book(String t, double p) {
title = t;
price = p;
}
void display() {
System.out.println("Title: " + title);
System.out.println("Price: " + price);
}
}
public class BookDemo {
public static void main(String[] args) {
Book b = new Book("Java Programming", 499);
b.display();
}
}
🟢 Question 7: Product Class
❓ Question
Design a class Product with:
pricequantity
Create a method to calculate total bill amount.
✅ Program
class Product {
double price;
int quantity;
void setData(double p, int q) {
price = p;
quantity = q;
}
double billAmount() {
return price * quantity;
}
}
public class ProductDemo {
public static void main(String[] args) {
Product p = new Product();
p.setData(250, 4);
System.out.println("Total Bill: " + p.billAmount());
}
}
🟢 Question 8: Circle Class
❓ Question
Design a class Circle with radius.
Create methods to calculate:
- Area
- Circumference
✅ Program
class Circle {
double radius;
void setRadius(double r) {
radius = r;
}
double area() {
return 3.14 * radius * radius;
}
double circumference() {
return 2 * 3.14 * radius;
}
}
public class CircleDemo {
public static void main(String[] args) {
Circle c = new Circle();
c.setRadius(7);
System.out.println("Area: " + c.area());
System.out.println("Circumference: " + c.circumference());
}
}
🟢 Question 9: Time Class
❓ Question
Design a class Time with hours and minutes.
Create a method to add two time objects.
✅ Program
class Time {
int hours;
int minutes;
void setTime(int h, int m) {
hours = h;
minutes = m;
}
Time add(Time t) {
Time temp = new Time();
temp.minutes = this.minutes + t.minutes;
temp.hours = this.hours + t.hours + (temp.minutes / 60);
temp.minutes = temp.minutes % 60;
return temp;
}
void display() {
System.out.println(hours + " hrs " + minutes + " mins");
}
}
public class TimeDemo {
public static void main(String[] args) {
Time t1 = new Time();
Time t2 = new Time();
t1.setTime(2, 45);
t2.setTime(1, 30);
Time t3 = t1.add(t2);
t3.display();
}
}
🟢 Question 10: Counter Class
❓ Question
Design a class Counter to count how many objects are created using a static variable.
✅ Program
class Counter {
static int count = 0;
Counter() {
count++;
}
}
public class CounterDemo {
public static void main(String[] args) {
new Counter();
new Counter();
new Counter();
System.out.println("Objects Created: " + Counter.count);
}
}
🎯 Key Practice Outcomes
- Understanding class design
- Writing object-oriented logic
- Separating data and behavior
- Applying constructors and methods
- Developing exam-ready coding skills 💪
✅ Conclusion
Practicing structured, exam-oriented OOPs programs is the best way to strengthen conceptual clarity 🚀
These 10 programs help build confidence in:
- Class design
- Object creation
- Method usage
- Real-world problem modeling
With consistent practice, OOPs concepts become intuitive and natural, forming a strong base for advanced Java topics.