โฑ๏ธ Time: 2 Hours
๐ Maximum Marks: 50
๐ Instructions (Read Carefully):
- Attempt all questions.
- Write each program in a separate class/file.
- Follow proper OOP principles.
- Use Scanner class for input where required.
- Output must be clearly formatted and labeled.
- Add comments explaining your logic.
๐น Section A โ Core Programming (30 Marks)
Q1. Class, Constructor & Conditional Logic (10 Marks)
๐ Scenario: Parking Ticket System
A parking system calculates charges based on hours parked.
๐ Write a Java program with the following requirements:
- Create a class
ParkingTicket. - Declare instance variables:
- vehicleNumber (String)
- hoursParked (int)
- ratePerHour (double)
- Create a parameterized constructor:
- Initialize all variables using
this
- Initialize all variables using
- Create a method
calculateCharges():- If hours โค 2 โ charge = hours ร rate
- If hours > 2 โ extra โน20 added as service fee
- Create method
displayDetails():- Display:
- Vehicle number
- Hours parked
- Total charge
- Display:
- In the
main()method:- Take input from user
- Create object
- Display result
Q2. Inheritance + Overriding + Decision Logic (10 Marks)
๐ Scenario: Delivery Charge System
A logistics company charges delivery fees based on service type and distance.
๐ Write a Java program as follows:
- Create base class
DeliveryService:- Method
calculateCharge(double distance) - Default message:
Delivery charge not defined
- Method
- Create subclasses:
LocalDeliveryIntercityDelivery
- Override method:
- LocalDelivery:
- โน10 per km
- If distance > 50 km โ add โน100 extra
- IntercityDelivery:
- โน20 per km
- LocalDelivery:
- In the
main()method:- Take distance as input
- Create objects of both classes
- Display charges
- Output must show:
- Delivery type
- Distance
- Final charge
Q3. Encapsulation + Multi-Step Logic (10 Marks)
๐ Scenario: E-Wallet Transaction System
An e-wallet system handles balance and transactions securely.
๐ Write a Java program with the following requirements:
- Create class
EWallet. - Declare private variables:
- walletId (int)
- userName (String)
- balance (double)
- Provide getter and setter methods with validation:
- Balance cannot be negative
- Create methods:
addMoney(double amount)transferMoney(double amount)
- Add conditions:
- Transfer allowed only if:
- balance โฅ amount
- minimum balance after transfer = โน100
- Otherwise display:
Transaction not allowed
- Transfer allowed only if:
- In the
main()method:- Create object
- Perform multiple operations
- Display final balance
๐น Section B โ Advanced OOP Concepts (20 Marks)
Q4. Abstract Class with Logic (10 Marks)
๐ Scenario: Examination System
Different exams calculate grades differently.
๐ Write a Java program as follows:
- Create an abstract class
Exam:- Abstract method
calculateGrade(int marks)
- Abstract method
- Create subclasses:
SchoolExamCompetitiveExam
- Implement method:
- SchoolExam:
- โฅ 90 โ A
- โฅ 70 โ B
- else โ C
- CompetitiveExam:
- โฅ 80 โ Qualified
- else โ Not Qualified
- SchoolExam:
- In the
main()method:- Take marks as input
- Use abstract class reference
- Display results for both exams
Q5. Runtime Polymorphism + Conditional Output (10 Marks)
๐ Scenario: Ride Fare System
Different ride types calculate fare differently.
๐ Write a Java program:
- Create class
RideService:- Method
calculateFare(double distance) - Default: return 0
- Method
- Create subclasses:
MiniRideLuxuryRide
- Override method:
- MiniRide โ โน8 per km
- LuxuryRide โ โน15 per km + โน50 base charge
- In the
main()method:- Take distance input
- Use parent reference
- Assign subclass objects
- Display fare
- Output must show:
- Ride type
- Distance
- Total fare
โญ Bonus Question (Optional โ 5 Marks)
Q6. Exception Handling + Multiple Conditions
๐ Scenario: Age Verification System
๐ Write a Java program to:
- Ask user to enter age
- Validate:
- Age must be โฅ 18
- If:
- Age < 18 โ throw exception
"Underage" - Age < 0 โ throw exception
"Invalid age"
- Age < 18 โ throw exception
- Use:
try-catch- Multiple conditions
- Finally block should print: