โฑ๏ธ Time: 2 Hours
๐ Maximum Marks: 50
๐ Instructions (Read Carefully):
- Attempt all questions.
- Each question must be written in a separate class/file.
- Follow proper OOP concepts and structure.
- Use Scanner class wherever input is required.
- Output must be clearly formatted and labeled.
- Add comments to explain important logic.
๐น Section A โ Core Programming (30 Marks)
Q1. Class, Object, Constructor & Method Logic (10 Marks)
๐ Scenario: Electricity Usage Tracker
An electricity department wants to track usage and calculate monthly bill.
๐ Write a Java program with the following requirements:
- Create a class named
ElectricityUsage. - Declare the following instance variables:
- consumerId (int)
- consumerName (String)
- unitsConsumed (int)
- Create a parameterized constructor:
- Initialize all variables using
thiskeyword
- Initialize all variables using
- Create a method
calculateBill():- If units โค 100 โ โน2 per unit
- If units โค 300 โ โน4 per unit
- If units > 300 โ โน6 per unit
- Create a method
displayDetails():- Display consumer details
- Display calculated bill
- In the
main()method:- Take input for one consumer
- Create object
- Display details
Q2. Inheritance + Overriding with Extra Logic (10 Marks)
๐ Scenario: Online Course Pricing
Different courses have different pricing rules.
๐ Write a Java program as follows:
- Create base class
Course:- Method
calculateFee(int duration) - Default message:
Fee calculation not defined
- Method
- Create subclasses:
ProgrammingCourseDesignCourse
- Override method:
- ProgrammingCourse:
- Fee = duration ร โน100
- If duration > 30 โ apply 10% discount
- DesignCourse:
- Fee = duration ร โน80
- ProgrammingCourse:
- In the
main()method:- Take duration as input
- Create objects of both subclasses
- Display calculated fee
- Output must clearly show:
- Course type
- Duration
- Final fee
Q3. Encapsulation with Business Logic (10 Marks)
๐ Scenario: Mobile Recharge System
A telecom company manages user balance securely.
๐ Write a Java program with the following steps:
- Create a class
RechargeAccount. - Declare private variables:
- mobileNumber (String)
- userName (String)
- balance (double)
- Provide:
- Getter and Setter methods
- Add validation:
- Balance cannot be negative
- Create methods:
recharge(double amount)โ add balancemakeCall(int minutes):- Deduct โน1 per minute
- If insufficient balance โ display
"Low balance"
- In the
main()method:- Create object
- Perform recharge
- Simulate call
- Display final balance
๐น Section B โ Advanced OOP Concepts (20 Marks)
Q4. Abstract Class with Multiple Methods (10 Marks)
๐ Scenario: Report Generation System
Different report types generate output differently.
๐ Write a Java program as follows:
- Create an abstract class
Report:- Abstract method
generateReport() - Concrete method
printHeader()โ prints"Report Generated"
- Abstract method
- Create subclasses:
SalesReportStudentReport
- Implement abstract method:
- SalesReport โ
"Generating Sales Report" - StudentReport โ
"Generating Student Report"
- SalesReport โ
- In the
main()method:- Use abstract class reference
- Call both methods (
printHeader()andgenerateReport())
Q5. Runtime Polymorphism with Return Values (10 Marks)
๐ Scenario: Tax Calculation System
Different income types have different tax rules.
๐ Write a Java program with the following steps:
- Create a class
Tax:- Method
calculateTax(double income) - Default: return 0
- Method
- Create subclasses:
SalariedTaxBusinessTax
- Override method:
- Salaried โ 10% of income
- Business โ 20% of income
- In the
main()method:- Take income as input
- Use parent class reference
- Assign subclass objects
- Display tax for both
- Output must clearly show:
- Income type
- Tax calculated
โญ Bonus Question (Optional โ 5 Marks)
Q6. Exception Handling + Custom Validation
๐ Scenario: Password Validation System
๐ Write a Java program to:
- Ask user to enter password
- Validate:
- Length must be at least 6 characters
- If invalid:
- Throw exception with message
"Weak Password"
- Throw exception with message
- Use:
try-catchthrowkeyword
- Finally block should print: