Java Paper Final Solution
Java Paper Final Solution
_______________
ADIT / GCET
(A Constituent College of CVM University)
B.Tech - SEMESTER - V
Mid Semester Examination (September 2024)
Subject Code: 202044502 Date: 02/09/2024
Subject Name: Programming with Java A.Y.: 2024-25
Time: 11:30 AM – 12:30 PM Max. Marks: 20
Instructions:
• Figures to the right indicate full marks.
• Make suitable assumptions whenever necessary.
Q.1 (A) Answer the Following. (Each question carries one mark). CO BL [04]
When instance variable and local variable or Method parameter are created with the same
name, the local variable will hide the instance variable; this is called Variable Hiding. To
solve this problem, use this keyword to point to the instance variable instead of the local
variable.
(iv) finalize block performs Exception Handling. True or False. CO3
E
False. Finalize is a method that is called during garbage collection process.
Q. 2 (A) Differentiate Interface and Abstract class. U [04]
keyword.
Dynamic polymorphism is achieved through Method Overriding. -----1 mark
The final keyword in Java serves as a non-access modifier, which can be applied to
variables, methods, and classes.
Final Variables:
• When a variable is declared as final, its value cannot be changed once it is
initialized. This makes the variable a constant.
Final Methods:
• A method declared as final cannot be overridden by subclasses. This is useful
when you want to prevent altering the method’s behaviour in derived classes.
Final Classes:
• A class declared as final cannot be subclassed. This is useful when you want to
prevent inheritance.
}
1 mark for each three uses.
Q. 3 (A) Write the following Java program to demonstrate dynamic method dispatch. A
CO2 [04]
Create a base class Vehicle with instance variables distance_coverd and fuel_consumed.
Also define a method milage() to display message “milage of the vehicle”. Derive two
classes, Car and Bike, from Vehicle, and override the milage() method in each of the
derived classes to print milage of the vehicle. In the main method, create reference
of Vehicle class pointing to Car and Bike object, and call the milage() method using this
reference.
(Hint: Milage = distance_covered/fuel_consumed).
@Override
public void milage() {
double milage = distance_covered / fuel_consumed;
System.out.println("Milage of the Bike: " + milage + " km/l");
}
}
and parameterized constructor to initialize the objects. Define display() method to display
details of an Employee. Create class Employee_Demo with main method to create 2
employee instances and to initialize each with different constructor and call display() for
each. Eid should be auto incremented, starting with Eid 1 for first instance of the
Employee.
// Class to represent an Employee ------3 mark
class Employee {
private String EName;
private int Experience;
private int Eid;
private static int idCounter = 1; // Static counter for auto-incrementing IDs
// Default constructor
public Employee() {
this.EName = "Unknown";
this.Experience = 0;
this.Eid = idCounter++; // Assign and increment the ID
}
// Parameterized constructor
public Employee(String EName, int Experience) {
this.EName = EName;
this.Experience = Experience;
this.Eid = idCounter++; // Assign and increment the ID
}
System.out.println("Employee 2 Details:");
emp2.display();
}
}
OR
(B) Write a Java program that creates a class Student. CO1 A [04]
The program should take the following details as an input from the command line
argument: Student_ID (integer), Student_Name (String), Total_Marks(integer). Define
result() method to display the result of the student.
(Hint: if total marks >= 50 then Pass else Fail.)
Also show the compilation and execution command for the program.