0% found this document useful (0 votes)
7 views

Java Paper Final Solution

This document outlines the Mid Semester Examination for the B.Tech Programming with Java course at ADIT/GCET, detailing the exam structure, instructions, and questions covering Java concepts such as platform independence, instance variables, polymorphism, and class inheritance. It includes specific questions on Java programming, including code examples for dynamic method dispatch and employee management. The document also provides instructions for command-line execution of a Java program related to student results.

Uploaded by

bataviyaharsh511
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Java Paper Final Solution

This document outlines the Mid Semester Examination for the B.Tech Programming with Java course at ADIT/GCET, detailing the exam structure, instructions, and questions covering Java concepts such as platform independence, instance variables, polymorphism, and class inheritance. It includes specific questions on Java programming, including code examples for dynamic method dispatch and employee management. The document also provides instructions for command-line execution of a Java program related to student results.

Uploaded by

bataviyaharsh511
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Enrolment No.

_______________

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]

(i) Java is platform independent language. Justify. CO1 E


When you compile a Java program, it is converted into an intermediate form called
bytecode. This bytecode is not specific to any hardware or operating system. Instead, it is
designed to be executed by the JVM.
(ii) Define instance variable. CO1 R

Instance variable is declared inside a class, but outside a method.


It exists for each instantiated object of the class, meaning each object has its own separate
copy of the instance variable.
(iii) What problem is being solved by this keyword? CO1 R

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]

Interface Abstract class


Abstract class can have abstract and Interface can have only
non-abstract methods. abstract methods. Since Java 8, it can
have default and static methods also.
Abstract class doesn't support multiple Interface supports multiple inheritance.
inheritance.
Abstract class can have final, non-final, Interface has only static and final
static and non-static variables. variables.
The abstract keyword is used to declare The interface keyword is used to declare
abstract class. interface.
An abstract class can extend another An interface can extend another Java
Java class and implement multiple Java interface only.
interfaces.
An abstract class can be extended using An interface can be implemented using
keyword "extends". keyword "implements".
A Java abstract class can have class Members of a Java interface are public by
members like private, protected, etc. default.

1 mark for 1 difference.


(B) CO2 U [04]
How to achieve static polymorphism in Java? Explain different uses of super keyword.
Enrolment No._______________

Static polymorphism is achieved through Method Overloading. ------ 1 Mark


The super keyword serves a special purpose in the context of inheritance, allowing a
subclass to access members (methods or fields) of its superclass.
Accessing Superclass Methods:
• You can use super to call a method from the superclass that has been overridden
in the subclass.
Accessing Superclass Constructors:
• The super keyword can be used to call the constructor of the superclass. This is
particularly useful when the superclass has a parameterized constructor.
Accessing Superclass Variables:
• If a subclass has a field with the same name as one in its superclass, super can be
used to refer to the superclass field.
class Parent {
int data;
Parent(String name)
{System.out.println("Parent constructor: " + name);}
Void display()
{ System.out.println("Parent class” ); }
}
class Child extends Parent {
Child(String name) {
super(name); // Calls Parent's constructor
System.out.println("Child constructor");}
Void display()
{ super.dispay();
System.out.println("data = ”+super.data);
System.out.println("Child class");
System.out.println("data = ”+data);}
}
1 mark for each three uses.
OR
(B) How to achieve dynamic polymorphism in Java? Explain different uses of final CO2 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.

final class Parent {


final int MAX_VALUE = 100;
final void display() {
System.out.println("This is a final method.");
}
Enrolment No._______________

}
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).

// Base class ---------------1 mark


class Vehicle {
double distance_covered;
double fuel_consumed;

// Constructor to initialize distance and fuel


public Vehicle(double distance_covered, double fuel_consumed) {
this.distance_covered = distance_covered;
this.fuel_consumed = fuel_consumed;
}

// Method to display message


public void milage() {
System.out.println("Milage of the vehicle");
}
}

// Derived class Car -------------------1 mark


class Car extends Vehicle {
// Constructor to initialize distance and fuel for Car
public Car(double distance_covered, double fuel_consumed) {
super(distance_covered, fuel_consumed);
}

// Overriding milage method for Car


@Override
public void milage() {
double milage = distance_covered / fuel_consumed;
System.out.println("Milage of the Car: " + milage + " km/l");
}
}

// Derived class Bike -------------------1 mark


class Bike extends Vehicle {
// Constructor to initialize distance and fuel for Bike
public Bike(double distance_covered, double fuel_consumed) {
super(distance_covered, fuel_consumed);
}

// Overriding milage method for Bike


Enrolment No._______________

@Override
public void milage() {
double milage = distance_covered / fuel_consumed;
System.out.println("Milage of the Bike: " + milage + " km/l");
}
}

// Main class to demonstrate dynamic method dispatch


public class Main { -------------------1 mark
public static void main(String[] args) {
// Creating Car and Bike objects
Vehicle myCar = new Car(500, 25);
Vehicle myBike = new Bike(300, 10);

// Calling the milage() method using Vehicle reference


myCar.milage(); // This will call the milage() method in Car class
myBike.milage(); // This will call the milage() method in Bike class
}
}
(B) Create a class Employee with data members: EName, Experience and Eid. Define default CO1 A [04]

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
}

// Method to display employee details


public void display() {
System.out.println("Employee ID: " + Eid);
System.out.println("Employee Name: " + EName);
System.out.println("Experience: " + Experience + " years");
System.out.println();
}
Enrolment No._______________

// Class to test the Employee class


public class Employee_Demo { ------1 mark
public static void main(String[] args) {
// Creating Employee instances using different constructors
Employee emp1 = new Employee(); // Default constructor
Employee emp2 = new Employee("Rahul Sen", 5); // Parameterized constructor

// Displaying details of each employee


System.out.println("Employee 1 Details:");
emp1.display();

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.

/ Class to represent a Student


public class Student { ------1 mark
private int studentID;
private String studentName;
private int totalMarks;

// Constructor to initialize Student details


public Student(int studentID, String studentName, int totalMarks) {
this.studentID = studentID;
this.studentName = studentName;
this.totalMarks = totalMarks;
}

// Method to display the result of the student


public void result() { ------1 mark
System.out.println("Student ID: " + studentID);
System.out.println("Student Name: " + studentName);
System.out.println("Total Marks: " + totalMarks);

if (totalMarks >= 50) {


System.out.println("Result: Pass");
} else {
System.out.println("Result: Fail");
}
}

// Main method to read command-line arguments and display the result


Enrolment No._______________

public static void main(String[] args) { ------1 mark


if (args.length != 3) {
System.out.println(" Error:");
return;
}
int studentID = Integer.parseInt(args[0]);
String studentName = args[1];
int totalMarks = Integer.parseInt(args[2]);

Student student = new Student(studentID, studentName, totalMarks);


student.result();
}
}
Compilation and Execution command ---------------1 mark
javac Student.java
java Student 10 Ram 50
* * * * * * * * ALL THE BEST * * * * * * * *

You might also like