Core JAVA Assignments
Core JAVA Assignments
1 Unit 1
1. Write a short note on: a. Java Virtual Machine (JVM): JVM is an abstract
computing machine that enables a computer to run Java programs. It converts Java
bytecode into machine language, making Java platform-independent. The JVM
performs tasks like memory management, garbage collection, and security checks.
b. Java Runtime Environment (JRE): JRE provides the libraries, Java Virtual
Machine (JVM), and other components to run applications written in Java. It includes
the JVM, core libraries, and other supporting files. It does not include development
tools like compilers or debuggers.
c. Java Development Kit (JDK): JDK is a software development kit used to develop
Java applications. It includes JRE and development tools such as a compiler,
debugger, and documentation generator. It is essential for developing, compiling, and
debugging Java applications.
java
Copy code
(int a, int b) -> a + b;
3. What is Java Annotation? List & Explain the different type of Java Annotations:
Java annotations provide metadata about the program, which can be processed by the
compiler and runtime. Types include:
o @Override: Indicates that a method overrides a method in a superclass.
o @Deprecated: Marks a method as deprecated, meaning it should no longer be
used.
o @SuppressWarnings: Instructs the compiler to suppress specific warnings.
o @SafeVarargs: Suppresses unsafe operations warnings on varargs methods.
4. Write a java program to find out the area of a triangle:
java
Copy code
public class TriangleArea {
public static void main(String[] args) {
double base = 5, height = 10;
double area = (base * height) / 2;
System.out.println("Area of Triangle: " + area);
}
}
5. What is Java object reference type?: In Java, an object reference is a variable that
holds the memory address of an object. It allows you to interact with the object's
fields and methods. Reference types include class types, interface types, and array
types.
6. Explain Autoboxing & Unboxing: Autoboxing is the automatic conversion of
primitive types to their corresponding object wrapper classes, like int to Integer.
Unboxing is the reverse process where the wrapper class is converted back to a
primitive type. Example:
java
Copy code
Integer obj = 10; // Autoboxing
int num = obj; // Unboxing
java
Copy code
public class MyClass {
int x;
MyClass() { x = 0; } // Default
MyClass(int val) { x = val; } // Parameterized
}
java
Copy code
public class MyClass {
void display(int a) { System.out.println(a); }
void display(double a) { System.out.println(a); }
}
4. Write a short note on abstract class: An abstract class in Java cannot be instantiated
and may contain abstract methods, which are methods without implementation. It can
also have concrete methods. It is used to provide a base for subclasses to extend and
implement the abstract methods.
Assignment No. 3 Unit 3
1. What is package? Explain the types of packages with the help of an example: A
package in Java is a namespace for organizing classes and interfaces. Types:
o Built-in Packages: Predefined packages like java.util.
o User-defined Packages: Created by developers to group related classes.
Example:
java
Copy code
package mypackage;
public class MyClass { }
java
Copy code
import java.io.*;
public class FileHandling {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("input.txt");
PrintWriter pw = new PrintWriter("output.txt");
int c;
while ((c = fr.read()) != -1) {
pw.write(c);
}
fr.close();
pw.close();
}
}
java
Copy code
public class MultiCatch {
public static void main(String[] args) {
try {
int a = 5 / 0;
int[] arr = new int[5];
System.out.println(arr[10]);
} catch (ArithmeticException | ArrayIndexOutOfBoundsException
e) {
System.out.println(e.getMessage());
}
}
}
1. What is the role of layout manager? What is the default layout of Frame?
Explain its working: A layout manager in Java arranges components within a
container. The default layout manager for a Frame is BorderLayout, which divides the
container into five regions: North, South, East, West, and Center. Components added
to these regions are laid out accordingly.
2. Explain the hierarchy of AWT components: AWT (Abstract Window Toolkit)
components hierarchy starts with Component as the base class, followed by
Container, which can hold other components, and further subclasses like Panel,
Window, Frame, Dialog, etc.
3. Explain Delegation Event Model in Java: The Delegation Event Model in Java is
used for event handling. It involves three components: Event Source (generates
events), Event Listener (receives events), and Event Object (encapsulates event
information). Listeners register with sources to handle events.
4. What is stream? Differentiate between stream source and stream destination:
(Duplicate from Assignment No. 4): A stream in Java is a sequence of data. Stream
source is the origin of data (e.g., file, network), while stream destination is where data
is sent (e.g., file, console).