[go: up one dir, main page]

0% found this document useful (0 votes)
6 views15 pages

Java Mid-I

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)
6 views15 pages

Java Mid-I

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/ 15

UNIT-I

1.Explain the concept of Bytecode in Java and why it is called Java’s


"Magic".

2. Compare Object-Oriented Programming (OOP) and Procedure-


Oriented Programming (POP) paradigms.

3. List and explain any Six important features of Java with examples.
4. Compare Java with C and C++ in terms of memory management,
platform independence, and OOP features.
5. Explain the principles of Object-Oriented Programming with examples.
6. Write the syntax for declaring and initializing one-dimensional and two-
dimensional arrays in Java. Write a program to find the largest element in
an array.
UNIT-II
1)Explain the fundamentals of a class in Java with an example, and
demonstrate how objects are declared, object reference variables are
assigned, and analyse the importance of object reference variables in
program execution.
A)Fundamentals of a class in Java
A class in Java is a blueprint for creating objects. It defines fields (variables)
and methods (functions) that describe the behavior of the object.
Objects are instances of classes, created using the new keyword.
PROGRAM:
class Student {
String name;
int age;
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Rahul";
s1.age = 20;
s1.display();
}
}
How objects are declared: Student s1; (reference created, no memory yet).
🔹 How memory is assigned: s1 = new Student(); (object created in heap,
reference stored in s1).
🔹 Importance of reference variables: They point to objects and allow us to
access fields and methods.
2. Differentiate between methods and constructors in Java, and illustrate
with a program how constructors (including overloaded ones) are invoked
along with method calls in object creation.

PROGRAM:
class Car {
String model;
// Constructor
Car(String model) {
this.model = model;
}
// Method
void display() {
System.out.println("Car model: " + model);
}
}
public class Test {
public static void main(String[] args) {
Car c1 = new Car("BMW"); // constructor called
c1.display(); // method called
}
}
3. Explain how objects are used in method handling by passing them as
parameters and returning them from methods, and justify how this
approach enhances code reusability with a suitable Java program.

Passing & Returning Objects

Objects can be passed as parameters and returned from methods for code
reusability.

PROGRAM:

class Calculator {

int value;

Calculator(int v) { value = v; }

Calculator add(Calculator c) {

return new Calculator(this.value + c.value); // Returning object

public class Main {

public static void main(String[] args) {

Calculator c1 = new Calculator(10);

Calculator c2 = new Calculator(20);

Calculator c3 = c1.add(c2); // Pass + Return object

System.out.println("Result: " + c3.value);

}
4. Explain constructors in Java and discuss their types (default and
parameterized). Demonstrate the concept of constructor overloading with a
Ticket Booking System program, including discount logic.
• Design a Java program for Ticket Booking using constructors. The
program should:
• Create a default constructor that initializes ticket details with default
values.
• Use a parameterized constructor to initialize booking details such as
passenger name, source, destination, and fare.
• Implement constructor overloading by allowing tickets to be booked with
and without discounts.
• Apply a discount logic (e.g., 10% discount on fare for online booking).
• Display the final ticket details, including passenger name, source,
destination, original fare, discount applied, and final payable fare.
A)
PROGRAM:
class Ticket {
String passenger, source, destination;
double fare, finalFare;
Ticket() {
passenger = "Unknown";
source = "NA";
destination = "NA";
fare = 0.0;
finalFare = fare;
}
Ticket(String p, String s, String d, double f) {
passenger = p;
source = s;
destination = d;
fare = f;
finalFare = f;
}
Ticket(String p, String s, String d, double f, boolean onlineBooking) {
passenger = p;
source = s;
destination = d;
fare = f;
if (onlineBooking)
finalFare = fare - (0.1 * fare);
else
finalFare = fare;
}
void display() {
System.out.println("Passenger: " + passenger);
System.out.println("From: " + source + " To: " + destination);
System.out.println("Original Fare: " + fare);
System.out.println("Final Payable Fare: " + finalFare);
}
}

public class Main {


public static void main(String[] args) {
Ticket t1 = new Ticket();
Ticket t2 = new Ticket("Alice", "Delhi", "Mumbai", 5000);
Ticket t3 = new Ticket("Bob", "Chennai", "Bangalore", 2000, true);

t1.display();
t2.display();
t3.display();
}
}

5. Explain the concept of Garbage Collection in Java, compare it with


memory management in C++, and evaluate its role in ensuring efficient
memory utilization in large applications.
A) Garbage Collection in Java vs C++

• Java: Automatic garbage collection (no delete required).


• C++: Manual memory management (new + delete).
• Java Advantage: Prevents memory leaks, dangling pointers → ensures
efficiency in large apps.
6. Discuss the use of the static and final keywords in Java, illustrate their
role with suitable programs, and analyze their impact on object-oriented
programming design.
static: Belongs to class, not objects.

final: Makes variables constant, prevents method overriding & inheritance.

PROGRAM:
class Example {
static int count = 0;
final int ID;
Example(int id) {
ID = id; // final must be initialized once
count++;
}
static void showCount() {
System.out.println("Objects created: " + count);
}
}
public class Main {
public static void main(String[] args) {
Example e1 = new Example(101);
Example e2 = new Example(102);
Example.showCount();
}
}

7. Explain the fundamentals of Strings in Java, demonstrate at least five


important String methods with examples, and analyse how immutability
and memory management affects string handling and performance.
A) Strings are immutable.
Stored in String pool.

PROGRAM:
public class Main {
public static void main(String[] args) {
String s = "Hello World";
System.out.println("Length: " + s.length());
System.out.println("Substring: " + s.substring(0, 5));
System.out.println("Uppercase: " + s.toUpperCase());
System.out.println("Replace: " + s.replace("World", "Java"));
System.out.println("IndexOf: " + s.indexOf("o"));
}
}
8. Differentiate between String and StringBuffer classes with examples,
demonstrate append(), insert(), and delete() methods of StringBuffer, and
evaluate how StringBuffer improves efficiency in string manipulation.
Design a Java program to manage student remarks using StringBuffer.
The program should:
a. Start with a remark "Student performance: ".
b. Append the student's grade (e.g., "Excellent").
c. Insert the student’s name at the beginning of the remark.
d. Delete unnecessary words (e.g., remove "performance" if not required).
e. Display the final remark.
Sample Output:
After append: Student performance: Excellent
After insert: John - Student performance: Excellent
After delete: John - Student : Excellent
Final Remark: John - Student : Excellent
A)
String: Immutable → new object created on modification.

StringBuffer: Mutable → more efficient in modifications.

PROGRAM:

public class StudentRemarks {


public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Student performance: ");

sb.append("Excellent");
System.out.println("After append: " + sb);

sb.insert(0, "John - ");


System.out.println("After insert: " + sb);

sb.delete(14, 25); // remove "performance"


System.out.println("After delete: " + sb);

System.out.println("Final Remark: " + sb);


}
}
9. Explain the concepts of method overloading and method overriding in
Java with suitable programs. Demonstrate how method overloading
achieves compile-time polymorphism with an example.

A) Overloading: Same name, different parameters → Compile-time


polymorphism.

Overriding: Subclass redefines parent method → Runtime polymorphism.

PROGRAM:

class MathOps {

int add(int a, int b) { return a + b; }

double add(double a, double b) { return a + b; } // Overloading

class Animal {

void sound() { System.out.println("Animal sound"); }

class Dog extends Animal {

void sound() { System.out.println("Bark"); } // Overriding

10. Explain the purpose of the StringTokenizer class in Java, discuss its
constructors and key methods, and demonstrate its usage in parsing a
sentence into words with a program. A library system needs to parse a
string containing book details (title, author, and year) separated by
commas. Write a program using StringTokenizer that:
a. Reads the string "Java The Complete Reference,Herbert Schildt,2024".
b. Extracts each token (book title, author name, and year).
c. Displays them neatly.
A)PROGRAM:
import java.util.StringTokenizer;
public class LibrarySystem {
public static void main(String[] args) {
String data = "Java The Complete Reference,Herbert Schildt,2024";
StringTokenizer st = new StringTokenizer(data, ",");
String title = st.nextToken();
String author = st.nextToken();
String year = st.nextToken();
System.out.println("Book Title: " + title);
System.out.println("Author: " + author);
System.out.println("Year: " + year);
}
}

11. Explain the concept of nested and inner classes.

A) Nested Class: Defined inside another class.

Inner Class: Non-static nested class → tied to outer object

PROGRAM:

class Outer {

int x = 10;
class Inner {
void display() {
System.out.println("Value: " + x);
}
}
}

12. Explain multilevel inheritance in Java with a program, highlighting the


sequence of constructor calls and order of execution. Demonstrate the use
of the super keyword for accessing superclass members and constructors,
and differentiate it from the this keyword Design a University
Administration System in Java to manage staff records. The system should
model both Teaching Staff and Non-Teaching Staff, sharing common
details (name, ID, salary) while maintaining category-specific information.
Implement single, multilevel, and hierarchical inheritance, and
demonstrate the use of the super keyword for invoking superclass methods
and constructors. The program must accept user input to create and
display records for different staff types.

PROGRAM:
class Staff {
String name; int id; double salary;
Staff(String n, int i, double s) {
name=n; id=i; salary=s;
}
void display() { System.out.println(name + " | ID: " + id + " | Salary: " +
salary); }
}

class TeachingStaff extends Staff {


String subject;
TeachingStaff(String n, int i, double s, String sub) {
super(n,i,s); // Call parent constructor
subject = sub;
}
void display() {
super.display();
System.out.println("Subject: " + subject);
}
}

class NonTeachingStaff extends Staff {


String dept;
NonTeachingStaff(String n, int i, double s, String d) {
super(n,i,s);
dept = d;
}
void display() {
super.display();
System.out.println("Department: " + dept);
}
}

public class UniversitySystem {


public static void main(String[] args) {
TeachingStaff t = new TeachingStaff("Alice", 101, 50000, "Math");
NonTeachingStaff nt = new NonTeachingStaff("Bob", 201, 30000,
"Library");

t.display();
nt.display();
}
}

You might also like