โฑ๏ธ Time: 2 Hours
๐ Maximum Marks: 50
๐ Instructions (Read Carefully):
- Attempt all questions.
- Each program must be written in separate classes.
- Use proper indentation and comments.
- Follow OOP principles strictly.
- Wherever input is required, you may use Scanner class.
- Display output in a clear and readable format.
๐น Section A โ Core Programming (30 Marks)
Q1. Class, Object & Constructor (10 Marks)
A vehicle rental company wants to store and display details of vehicles available for rent.
๐ You are required to write a Java program with the following specifications:
- Create a class named
Vehicle. - Declare the following data members:
- vehicleNumber (String)
- vehicleName (String)
- rentPerDay (double)
- Create a parameterized constructor that:
- Accepts values for all data members
- Initializes them using
thiskeyword
- Create a method named
displayVehicleDetails()that:- Prints all the details in a proper format
- Example format: Vehicle Number: MH12AB1234
Vehicle Name: Swift
Rent per Day: 1500
- In the
main()method:- Create at least 3 vehicle objects with different values
- Call the display method for each object
Q2. Inheritance & Method Overriding (10 Marks)
A school management system calculates exam results differently for different types of students.
๐ Write a Java program as per the following instructions:
- Create a base class
Student:- Data member: name
- Method:
calculateResult()- This method should print:
"Result calculation not defined"
- This method should print:
- Create two subclasses:
RegularStudentDistanceStudent
- Override the
calculateResult()method:- RegularStudent:
- Assume marks = 80
- Print:
"Regular Student Passed"if marks โฅ 40
- DistanceStudent:
- Assume marks = 35
- Print:
"Distance Student Passed"or"Failed"accordingly
- RegularStudent:
- In the
main()method:- Create objects of both subclasses
- Call the overridden method for each
- Ensure that:
- Method overriding is clearly demonstrated
- Output messages are clearly distinguishable
Q3. Encapsulation (10 Marks)
A banking system must ensure that account balance is handled securely.
๐ Write a Java program with the following requirements:
- Create a class
BankAccount. - Declare private data members:
- accountNumber (int)
- accountHolderName (String)
- balance (double)
- Provide public:
- Setter methods to assign values
- Getter methods to retrieve values
- Add validation inside setter:
- Balance should not be negative
- If negative value is given, display:
"Invalid balance"
- Create a method
deposit(double amount):- Adds amount to balance
- Print updated balance
- Create a method
withdraw(double amount):- Deduct amount if sufficient balance
- Otherwise display:
"Insufficient balance"
- In
main():- Create an object
- Set values using setters
- Perform deposit and withdrawal operations
- Display final account details
๐น Section B โ Advanced OOP Concepts (20 Marks)
Q4. Abstract Class (10 Marks)
A company uses different types of employees, and their bonus calculation varies.
๐ Write a Java program following these steps:
- Create an abstract class
Employee:- Data member: name
- Abstract method:
calculateBonus()
- Create two subclasses:
ManagerDeveloper
- Implement
calculateBonus():- Manager โ Bonus = โน10,000
- Developer โ Bonus = โน5,000
- Each method should:
- Print employee type
- Print bonus amount
- In
main():- Use reference of abstract class
- Create objects of both subclasses
- Call methods using dynamic binding
Q5. Runtime Polymorphism (10 Marks)
An online shopping system applies different discount strategies.
๐ Write a Java program as follows:
- Create a class
Product:- Method
applyDiscount(double price) - Default behavior: return original price
- Method
- Create subclasses:
ElectronicsClothing
- Override the method:
- Electronics โ 10% discount
- Clothing โ 20% discount
- In
main():- Use parent class reference
- Assign objects of both subclasses
- Call method and print final price
- Clearly show:
- Same method behaving differently for different objects
โญ Bonus Question (Optional โ 5 Marks)
Q6. Exception Handling (Detailed)
๐ Write a Java program to:
- Take two integers as input from the user:
- numerator
- denominator
- Perform division: result = numerator / denominator
- Handle the following cases:
- If denominator = 0 โ display
"Cannot divide by zero" - If invalid input โ handle using appropriate exception
- If denominator = 0 โ display
- Use:
trycatchfinallyblock (to print"Operation completed")