2nd Module Java Solution

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

1.

Java Garbage Collection Mechanism: 3 Generations of Java Heap (6


marks)

Java uses Garbage Collection (GC) to automatically manage memory by reclaiming


memory used by objects that are no longer needed. Java heap memory is divided into three
generations to optimize GC performance:

1. Young Generation:
o Newly created objects are allocated in this region.
o Divided into Eden Space and Survivor Spaces (S0 and S1).
o Most objects are short-lived and quickly become eligible for garbage
collection.
o Minor GC occurs when the Young Generation fills up, and live objects are
moved to the Old Generation.
2. Old (Tenured) Generation:
o Objects that survive multiple GC cycles in the Young Generation are moved to
the Old Generation.
o Major GC or Full GC happens here less frequently but takes more time.
3. Permanent Generation (Metaspace):
o Contains metadata required by the JVM (such as class definitions, method
objects).
o In Java 8 and later, Metaspace replaces PermGen, which dynamically adjusts
in size, improving performance.

2. Java Program to Find Area of Rectangle, Circle, and Triangle Using


Method Overloading (10 marks)
java
Copy code
public class AreaCalculator {

// Area of rectangle
public static double area(double length, double width) {
return length * width;
}

// Area of circle
public static double area(double radius) {
return Math.PI * radius * radius;
}

// Area of triangle
public static double area(double base, double height, String triangle)
{
return 0.5 * base * height;
}

public static void main(String[] args) {


// Example inputs
System.out.println("Area of Rectangle: " + area(5, 7)); //
Rectangle: length=5, width=7
System.out.println("Area of Circle: " + area(3)); //
Circle: radius=3
System.out.println("Area of Triangle: " + area(5, 12, "t")); //
Triangle: base=5, height=12
}
}

3. General Form of Class with Example (4 marks)

The general form of a class in Java consists of:

 Modifiers (optional)
 Class Keyword followed by the class name
 Fields (variables) to store object states
 Methods to perform actions

Example:

java
Copy code
public class MyClass {
// Fields (or instance variables)
int num;
String name;

// Constructor
public MyClass(int num, String name) {
this.num = num;
this.name = name;
}

// Method to display data


public void display() {
System.out.println("Num: " + num + ", Name: " + name);
}

public static void main(String[] args) {


MyClass obj = new MyClass(10, "Alice");
obj.display();
}
}

4. Explanation of Keywords: this and static (8 marks)

a. this Keyword:

 Refers to the current object instance.


 Used to differentiate between instance variables and method parameters with the same
name.

Example:

java
Copy code
public class Demo {
int num;

Demo(int num) {
this.num = num; // `this` refers to the current object instance
}

void display() {
System.out.println("Number: " + this.num);
}
}

b. static Keyword:

 Used for class-level variables and methods.


 static members belong to the class, not individual instances.

Example:

java
Copy code
public class DemoStatic {
static int count = 0; // Static variable

DemoStatic() {
count++; // Increment count whenever a new object is created
}

static void displayCount() {


System.out.println("Count: " + count); // Static method accessing
static variable
}

public static void main(String[] args) {


new DemoStatic();
new DemoStatic();
DemoStatic.displayCount(); // Output: Count: 2
}
}

5. Java Program to Create Employee Class and Read/Write Employee


Information (10 marks)
java
Copy code
import java.util.Scanner;

public class Employee {


String name, designation;
int empid;
double basicSalary;

// Method to read employee information


public void read(Scanner sc) {
System.out.print("Enter name: ");
name = sc.nextLine();
System.out.print("Enter designation: ");
designation = sc.nextLine();
System.out.print("Enter employee ID: ");
empid = sc.nextInt();
System.out.print("Enter basic salary: ");
basicSalary = sc.nextDouble();
sc.nextLine(); // Consume the leftover newline
}

// Method to display employee information


public void write() {
System.out.println("Employee ID: " + empid);
System.out.println("Name: " + name);
System.out.println("Designation: " + designation);
System.out.println("Basic Salary: " + basicSalary);
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
Employee[] employees = new Employee[5];

// Read and write information for 5 employees


for (int i = 0; i < 5; i++) {
employees[i] = new Employee();
System.out.println("Enter details for employee " + (i + 1) +
":");
employees[i].read(sc);
}

System.out.println("\nEmployee Information:");
for (Employee emp : employees) {
emp.write();
System.out.println();
}

sc.close();
}
}

6. Types of Constructors with Example (4 marks)

There are two main types of constructors in Java:

1. Default Constructor: Provided by Java if no constructor is defined.


2. Parameterized Constructor: Takes arguments to initialize the object.

Example:

java
Copy code
public class Person {
String name;

// Default constructor
public Person() {
name = "Unknown";
}
// Parameterized constructor
public Person(String name) {
this.name = name;
}

public void display() {


System.out.println("Name: " + name);
}

public static void main(String[] args) {


Person p1 = new Person(); // Calls default constructor
Person p2 = new Person("Alice"); // Calls parameterized
constructor

p1.display(); // Output: Name: Unknown


p2.display(); // Output: Name: Alice
}
}

7. Method Overloading in Java with Example (8 marks)

Method Overloading occurs when multiple methods have the same name but different
parameter lists.

Example:

java
Copy code
public class Calculator {

// Overloaded method to add two integers


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

// Overloaded method to add three integers


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

public static void main(String[] args) {


Calculator calc = new Calculator();
System.out.println("Sum of 2 numbers: " + calc.add(10, 20)); //
Output: 30
System.out.println("Sum of 3 numbers: " + calc.add(10, 20, 30)); //
Output: 60
}
}

8. Stack Class with Push, Pop, and Display Methods (12 marks)
java
Copy code
class Stack {
private int maxSize;
private int[] stackArray;
private int top;

// Constructor to initialize stack


public Stack(int size) {
maxSize = size;
stackArray = new int[maxSize];
top = -1;
}

// Push element onto stack


public void push(int value) {
if (top < maxSize - 1) {
stackArray[++top] = value;
System.out.println(value + " pushed onto stack.");
} else {
System.out.println("Stack is full, cannot push.");
}
}

// Pop element from stack


public int pop() {
if (top >= 0) {
int value = stackArray[top--];
System.out.println(value + " popped from stack.");
return value;
} else {
System.out.println("Stack is empty, cannot pop.");
return -1;
}
}

// Display stack elements


public void display() {
if (top >= 0) {
System.out.println("Stack elements:");
for (int i = top; i >= 0; i--) {
System.out.println(stackArray[i]);
}
} else {
System.out.println("Stack is empty.");
}
}

public static void main(String[] args) {


Stack stack = new Stack(5);
stack.push(10);
stack.push(20);
stack.push(30);
stack.display(); // Displays 30, 20, 10
stack.pop(); // Pops 30
stack.display(); // Displays 20, 10
}
}

You might also like