E SPR Ess O
E SPR Ess O
2. Describe the structure of a Java source file: A Java source file typically has the following
structure:
Example:
4. What is the role of the Java Virtual Machine (JVM)? The JVM is responsible for running
Java programs. It provides an environment that translates compiled bytecode into machine
code, which can then be executed by the host system. The JVM also manages memory,
garbage collection, and other system resources for Java applications.
5. What is the function of the main method in a Java program? The main method is the
entry point of any Java program. It is where the execution of the program begins. The JVM calls
this method to start the program when running the class.
Example:
○ Static members: Belong to the class, not instances of the class. They can be
accessed without creating an object of the class. Static members are shared
among all instances of the class.
○ Non-static members: Belong to individual instances of the class. They require
an object of the class to be accessed.
8. Give any four examples of JavaDoc Comments: JavaDoc comments are special
comments used to generate documentation. They are written with /** and */ and typically
appear above classes, methods, or fields. Example:
/**
* This is a class representing a person.
*/
public class Person {
/**
* This method returns the name of the person.
* @return the person's name
*/
public String getName() {
return name;
}
}
11. Write a Java program to check whether a given number is Prime or Not:
import java.util.Scanner;
if (num < 2) {
isPrime = false;
}
if (isPrime) {
System.out.println(num + " is a prime number.");
} else {
System.out.println(num + " is not a prime number.");
}
}
}
A package in Java is a namespace that organizes a set of related classes and interfaces. It
helps in avoiding name conflicts and makes it easier to manage and maintain code. For
example, the java.util package contains utility classes like ArrayList, HashMap, etc.
PART B
1. Explain how arrays are declared, initialized, and accessed in Java. Write a program to find
the maximum value in an integer array.
● Significance: JavaDoc comments are used to generate documentation for Java code.
They are important for describing classes, methods, parameters, and return values in a
clear and standard format. This helps other developers understand your code better.
● Sample Java Class with JavaDoc Comments:
/**
* This class represents a simple calculator.
* It provides methods for addition and subtraction.
*/
public class Calculator {
/**
* Adds two integers.
*
* @param a the first integer
* @param b the second integer
* @return the sum of a and b
*/
public int add(int a, int b) {
return a + b;
}
/**
* Subtracts the second integer from the first.
*
* @param a the first integer
* @param b the second integer
* @return the difference of a and b
*/
public int subtract(int a, int b) {
return a - b;
}
}
3. Enlist and describe the various salient features of Java Language in detail
with suitable examples
Java code is compiled into bytecode and interpreted by the JVM on any platform.
Platform Independence
Java's bytecode can run on any device with a JVM, ensuring Write Once, Run Anywhere
(WORA).
Object-Oriented
Robust
Java’s memory management, exception handling, and strong typing make it less error-
prone.
Security
Java uses bytecode verification, security manager, and sandboxing to protect against
malicious code.
Java automatically handles memory via Garbage Collection, freeing unused objects.
Dynamic Binding
Java uses dynamic method calls (runtime method binding) for flexibility.
High Performance
Java’s JIT compilation improves runtime performance by converting bytecode to machine code.
Multithreaded
Java eliminates manual memory management, and buffer overflows for safety.
This summary keeps the key points while simplifying the explanation.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Employee {
int id;
String name, department;
double salary;
void display() {
System.out.println("ID: " + id + ", Name: " + name + ", Department: " + department
Salary: " + salary);
}
}
switch (choice) {
case 1: addEmployee(); break;
case 2: displayEmployees(); break;
case 3: searchById(); break;
case 4: updateSalary(); break;
default: System.out.println("Invalid option!"); break;
}
}
}
5. Matrix Addition and Multiplication (if ye remember sem 2 this finna be ez af)
Addition
import java.util.Scanner;
// Input matrices
int[][] matrix1 = new int[rows1][cols1];
int[][] matrix2 = new int[rows2][cols2];
// Matrix addition
int[][] result = addMatrices(matrix1, matrix2);
// Input matrices
int[][] matrix1 = new int[rows1][cols1];
int[][] matrix2 = new int[rows2][cols2];
System.out.println("Enter elements for Matrix 1:");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
matrix1[i][j] = scanner.nextInt();
}
}
// Matrix multiplication
int[][] result = multiplyMatrices(matrix1, matrix2);
Encapsulation
Encapsulation involves bundling data (variables) and methods that operate on the data into a
single unit (class). It also restricts direct access to some object components using access
modifiers (e.g., private, public).
class Employee {
private String name;
Abstraction hides complex implementation details and exposes only essential features through
abstract classes or interfaces. It simplifies interaction with objects.
Inheritance
Inheritance allows a class to inherit properties and methods from another class, promoting code
reusability. The subclass can override methods of the superclass.
class Animal {
void eat() { System.out.println("Eating..."); }
}
Polymorphism
class Calculator {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
}
Association
Association defines relationships between objects. Types include:
class Person {
Address address; // Association: Person has an Address
}
Composition
Composition is a stronger form of association, where one object is contained within another.
The contained object’s lifecycle is tied to the container object.
class Engine {
void start() { System.out.println("Engine starting..."); }
}
class Car {
Engine engine = new Engine(); // Car "Has-A" Engine
}
Delegation
Delegation is when an object relies on another object to perform tasks, promoting modularity
and code reuse.
class Printer {
void print(String message) { System.out.println(message); }
}
class Manager {
Printer printer = new Printer();
void delegatePrint(String msg) { printer.print(msg); }
}
Account.java
package com.bank.account;
@Override
public void withdraw(double amount) {
if (amount <= getBalance() + overdraftLimit) super.withdraw(amount);
else System.out.println("Exceeded overdraft limit.");
}
}
BankService.java
package com.bank.service;
import com.bank.account.Account;
import com.bank.account.SavingsAccount;
import com.bank.account.CurrentAccount;
import com.bank.account.SavingsAccount;
import com.bank.account.CurrentAccount;
import com.bank.service.BankService;
bankService.showBalance(savings);
bankService.showBalance(current);
bankService.deposit(savings, 200);
bankService.withdraw(savings, 150);
bankService.deposit(current, 100);
bankService.withdraw(current, 600);
bankService.calculateInterest(savings);
}
}
8.Write the step by step procedure for creating a package with a suitable example
Create a Package
To create a package, start by defining it at the top of your Java file using the package keyword.
The package name is usually written in lowercase to avoid conflicts with class names.
// File: Account.java
package com.example.bank;
After defining the package, save the Java file in the directory structure that matches the
package name. For example, for the package com.example.bank, save your file as:
com/example/bank/Account.java
Navigate to the root directory (the one above com) in the terminal and compile the file using
javac.
javac com/example/bank/Account.java
This will compile the Account.java file and create Account.class inside the corresponding
com/example/bank/ directory.
Use the Package in Another Class
To use the class defined in your package, you need to import it in another Java file. You can do
this by using the import statement.
// File: Test.java
import com.example.bank.Account;
You should also save Test.java in the correct directory matching its package (if any). For
simplicity, we can save it in the root directory above com/.
javac Test.java
java Test
Output:
Account Number: 101
Account Holder Name: Alice
Balance: 1000.0
PART - C
1 .Given an array of integers, rearrange the array in such a way that the first
int n = arr.length;
int[] result = new int[n];
int start = 0, end = n - 1;
import java.util.*;