๐Ÿงพ Java OOPs Scenario-Based Programming Questions (50 Questions)


๐Ÿ”น Section A: Classes & Objects

Q1. (Scenario Based)

A college wants to manage student information.
Write a Java program to:

  • Create a class Student
  • Include data members: id, name, marks
  • Add a method to display student details
  • Create at least 3 student objects and display their data

Q2. (Scenario Based)

A company needs to store employee details.
Write a Java program to:

  • Create a class Employee
  • Store empId, name, salary
  • Display employee details using a method

Q3. (Scenario Based)

A construction company wants to calculate area of plots.
Write a Java program to:

  • Create a class Rectangle
  • Add methods to calculate area and perimeter
  • Display results

Q4. (Scenario Based)

A sports academy tracks player performance.
Write a Java program to:

  • Create a class Player
  • Store player name and score
  • Display player details

Q5. (Scenario Based)

A library wants to manage books.
Write a Java program to:

  • Create a class Book
  • Include title, author, price
  • Display book information

๐Ÿ”น Section B: Constructors

1. Constructor with Validation and Method Logic

Create a class Student with:

  • Attributes: name (String), marks (int)
  • Constructor to initialize values (marks must be 0โ€“100, else set to -1)
  • Method getGrade() that returns:
    • A (โ‰ฅ90), B (โ‰ฅ75), C (โ‰ฅ50), F (<50)

Task: Create multiple students and print their grades.


2. Constructor Overloading with Business Logic

Create a class Product with:

  • Attributes: productId, name, price, discount
  • Constructors:
    • One without discount (default = 0)
    • One with discount
  • Method finalPrice() that calculates price after discount

Task: Show final price for different products.


3. Copy Constructor with Deep Copy Concept

Create a class Address with:

  • Attributes: city, pincode
    Create another class Employee with:
  • Attributes: name, Address address
  • Parameterized constructor
  • Copy constructor (ensure deep copy, not reference copy)

Task: Modify copied objectโ€™s address and verify original is unchanged.


4. Constructor Chaining with Calculation

Create a class Box with:

  • Attributes: length, width, height
  • Constructors:
    • Default (1,1,1)
    • Two parameters (height default = 1)
    • Three parameters
  • Use constructor chaining
  • Method volume()

Task: Calculate volume for different objects.


5. Static Members with Constructor Logic

Create a class BankAccount with:

  • Attributes: accountNumber, balance
  • Static variable: totalAccounts
  • Constructor increments count and auto-generates account number
  • Method deposit(double amt)

Task: Track total accounts created.


6. Immutable Class Using Constructor

Create a class ImmutableStudent:

  • Private final attributes: name, rollNo
  • Initialize using constructor
  • Only getter methods, no setters

Task: Prove object cannot be modified after creation.


7. Constructor with Array Initialization

Create a class Library with:

  • Attribute: array of Book objects
  • Constructor initializes array with given size
  • Method to add book details and display all books

Task: Manage multiple books using constructors.


8. Constructor with Exception Handling

Create a class User with:

  • Attributes: username, password
  • Constructor:
    • Username must not be empty
    • Password must be at least 6 characters
  • Throw exception if invalid

Task: Handle invalid inputs using try-catch.


9. Constructor + Object Composition

Create a class Engine with:

  • Attributes: type, horsePower

Create a class Car with:

  • Attributes: brand, Engine engine
  • Constructor initializes both objects
  • Method displayCarDetails()

Task: Demonstrate object composition using constructors.


10. Constructor + Method Overloading + Logic

Create a class Calculator with:

  • Constructor prints “Calculator Initialized”
  • Overloaded methods:
    • add(int a, int b)
    • add(double a, double b)
  • Method multiply(int a, int b)

Task: Demonstrate constructor execution and method overloading.


๐Ÿ”น Section C: Inheritance

Q11. (Scenario Based)

An animal simulation system needs common and specific behaviors.
Write a Java program to:

  • Create base class Animal
  • Derive class Dog
  • Display sound

Q12. (Scenario Based)

A company wants to manage employee hierarchy.
Write a Java program to:

  • Create classes Person and Employee
  • Inherit properties and display details

Q13. (Scenario Based)

A company has multiple levels of management.
Write a Java program to:

  • Implement multilevel inheritance (Person โ†’ Employee โ†’ Manager)

Q14. (Scenario Based)

A drawing application supports multiple shapes.
Write a Java program to:

  • Create base class Shape
  • Derive Circle and Rectangle
  • Calculate area

Q15. (Scenario Based)

A system needs to reuse parent class initialization.
Write a Java program to:

  • Use super keyword to call parent constructor

๐Ÿ”น Section D: Method Overloading

Q16. (Scenario Based)

A system needs to perform different operations using the same method name.
Write a Java program to:

  • Create class Calculator
  • Overload method calculate() for:
    • Adding two numbers
    • Adding three numbers
  • Display results

Q17. (Scenario Based)

A geometry tool calculates area for different shapes.
Write a Java program to:

  • Overload method area() for circle and rectangle

Q18. (Scenario Based)

A student system stores different types of data.
Write a Java program to:

  • Overload method display() for different parameters

Q19. (Scenario Based)

A calculator handles different data types.
Write a Java program to:

  • Overload method for int, double

Q20. (Scenario Based)

A printing system needs multiple formats.
Write a Java program to:

  • Overload method print()

๐Ÿ”น Section E: Method Overriding

Q21. (Scenario Based)

An animal system needs specific sound behavior.
Write a Java program to:

  • Override method sound() in subclass

Q22. (Scenario Based)

A vehicle system needs different running behavior.
Write a Java program to:

  • Override method run() in subclass

Q23. (Scenario Based)

A system needs runtime decision for method execution.
Write a Java program to:

  • Demonstrate runtime polymorphism

Q24. (Scenario Based)

A company customizes behavior of inherited methods.
Write a Java program to:

  • Use @Override annotation

Q25. (Scenario Based)

A system wants to extend parent functionality.
Write a Java program to:

  • Override method and call parent method using super

๐Ÿ”น Section F: Polymorphism

Q26. (Scenario Based)

A payroll system calculates salary differently for employees.
Write a Java program to:

  • Create class Employee
  • Subclasses: FullTime, PartTime
  • Use dynamic method dispatch

Q27. (Scenario Based)

A system processes different types of payments.
Write a Java program to:

  • Use parent reference for multiple payment types

Q28. (Scenario Based)

A system handles different object types using one reference.
Write a Java program demonstrating upcasting


Q29. (Scenario Based)

A system needs access to child-specific methods.
Write a Java program demonstrating downcasting


Q30. (Scenario Based)

A system needs flexible method execution.
Write a Java program using runtime polymorphism


๐Ÿ”น Section G: Encapsulation

Q31. (Scenario Based)

A bank needs to protect customer data.
Write a Java program to:

  • Create class BankAccount
  • Use private variables
  • Provide deposit and withdraw methods

Q32. (Scenario Based)

A system restricts direct access to data.
Write a Java program using getters and setters


Q33. (Scenario Based)

A system validates user input.
Write a Java program to:

  • Add validation in setter method

Q34. (Scenario Based)

A company hides internal data from users.
Write a Java program demonstrating data hiding


Q35. (Scenario Based)

A system ensures controlled access to variables.
Write a Java program using encapsulation


๐Ÿ”น Section H: Abstraction

Q36. (Scenario Based)

A drawing system defines common structure for shapes.
Write a Java program to:

  • Create abstract class Shape
  • Define abstract method area()

Q37. (Scenario Based)

A vehicle system defines common operations.
Write a Java program using abstract class


Q38. (Scenario Based)

A system uses both abstract and concrete methods.
Write a Java program demonstrating abstract class


Q39. (Scenario Based)

A payroll system needs flexible salary calculation.
Write a Java program using abstract class Employee


Q40. (Scenario Based)

A system models real-world abstraction.
Write a Java program using abstract class


๐Ÿ”น Section I: Interfaces

Q41. (Scenario Based)

A system defines common behavior for vehicles.
Write a Java program to:

  • Create interface Vehicle
  • Implement in Car and Bike

Q42. (Scenario Based)

A system requires multiple capabilities.
Write a Java program to:

  • Implement multiple interfaces

Q43. (Scenario Based)

A system extends interface functionality.
Write a Java program demonstrating interface inheritance


Q44. (Scenario Based)

A system uses default functionality in interfaces.
Write a Java program using default methods


Q45. (Scenario Based)

A system simulates multiple inheritance.
Write a Java program using interfaces


๐Ÿ”น Section J: Integrated Real-World Problems

Q46. (Scenario Based)

A library wants to manage books and users.
Design a Java program using OOP principles


Q47. (Scenario Based)

A school needs a result management system.
Design using classes and inheritance


Q48. (Scenario Based)

A bank needs a complete account management system.
Design using encapsulation and inheritance


Q49. (Scenario Based)

An e-commerce system processes different payment methods.
Design using polymorphism


Q50. (Scenario Based)

A vehicle rental company needs a system to manage vehicles and bookings.
Design using OOP concepts

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *