โฑ๏ธ Time: 2 Hours
๐ Maximum Marks: 50
๐ Instructions:
- Attempt all questions.
- Write each program in separate classes.
- Use proper OOP principles.
- Use Scanner class wherever input is required.
- Output must be clearly labeled.
- Add comments to explain your code.
๐น Section A โ Core Programming (30 Marks)
Q1. Class, Object & Constructor (10 Marks)
An online course platform wants to store and display course information.
๐ Write a Java program following the steps below:
- Create a class named
Course. - Declare the following instance variables:
- courseId (int)
- courseName (String)
- duration (in hours, int)
- fee (double)
- Create a parameterized constructor:
- It should accept values for all variables
- Initialize them using the
thiskeyword
- Create a method named
displayCourseDetails():- This method should print all course details in a proper format
- Example: Course ID: 201
Course Name: Java Programming
Duration: 40 hours
Fee: 5000.0
- In the
main()method:- Ask the user to enter details for 2 courses
- Create objects using constructor
- Display details of both courses
Q2. Inheritance & Method Overriding (10 Marks)
A utility billing system calculates electricity bills differently for different consumers.
๐ Write a Java program as per the following instructions:
- Create a base class
ElectricityBill:- Method
calculateBill(int units) - Default implementation should print: Bill calculation not defined
- Method
- Create two subclasses:
DomesticUserCommercialUser
- Override the method in both classes:
- DomesticUser:
- Bill = units ร โน3
- CommercialUser:
- Bill = units ร โน5
- DomesticUser:
- In the
main()method:- Ask user to enter number of units consumed
- Create objects of both subclasses
- Call the method and display bill
- Output should clearly indicate:
- Type of user
- Units consumed
- Total bill
Q3. Encapsulation (10 Marks)
A payroll system must securely handle employee salary details.
๐ Write a Java program with the following requirements:
- Create a class
Payroll. - Declare private variables:
- employeeId (int)
- employeeName (String)
- basicSalary (double)
- Provide:
- Getter and Setter methods
- Add validation:
- basicSalary should be greater than 0
- If invalid, display
"Invalid salary entered"
- Create a method
calculateNetSalary():- HRA = 20% of basic salary
- DA = 10% of basic salary
- Net Salary = basic + HRA + DA
- In the
main()method:- Create object
- Set values using setters
- Call method to calculate and display net salary
๐น Section B โ Advanced OOP Concepts (20 Marks)
Q4. Abstract Class (10 Marks)
A notification system sends alerts using different channels.
๐ Write a Java program as follows:
- Create an abstract class
Notification:- Abstract method
sendNotification(String message)
- Abstract method
- Create subclasses:
EmailNotificationSMSNotification
- Implement method:
- Email โ print
"Sending Email: message" - SMS โ print
"Sending SMS: message"
- Email โ print
- In the
main()method:- Use abstract class reference
- Create objects of both subclasses
- Call methods
- Ensure:
- Proper abstraction
- Dynamic method calling
Q5. Runtime Polymorphism (10 Marks)
A parking system calculates parking fees differently based on vehicle type.
๐ Write a Java program with the following steps:
- Create a class
Parking:- Method
calculateFee(int hours) - Default behavior: return 0
- Method
- Create subclasses:
TwoWheelerFourWheeler
- Override method:
- TwoWheeler โ โน10 per hour
- FourWheeler โ โน20 per hour
- In the
main()method:- Use parent class reference
- Assign objects of both subclasses
- Calculate and display fees
- Output must clearly show:
- Vehicle type
- Hours
- Total fee
โญ Bonus Question (Optional โ 5 Marks)
Q6. Exception Handling with Multiple Catches
๐ Write a Java program to:
- Take input from user:
- Array size
- Array elements
- Then ask user for index to access element
- Handle exceptions:
- Invalid input โ
InputMismatchException - Invalid index โ
ArrayIndexOutOfBoundsException
- Invalid input โ
- Use:
- Multiple
catchblocks finallyblock to print:
- Multiple