Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Assignment No.

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.

2. What is Lambda Expression? Explain with example: Lambda expressions are a


feature in Java that provide a clear and concise way to represent one method interface
using an expression. They are used primarily to define inline implementation of a
functional interface. Example:

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

7. What is Operator? Explain the different types of operators: An operator in Java is


a symbol that performs operations on variables and values. Types include:
o Arithmetic Operators: +, -, *, /, %
o Relational Operators: ==, !=, >, <, >=, <=
o Logical Operators: &&, ||, !
o Bitwise Operators: &, |, ^, ~
o Assignment Operators: =, +=, -=, *=, /=

Assignment No. 2 Unit 2

1. What is constructor? Explain the types of constructor? Explain with an example:


A constructor in Java is a special method that initializes objects. Types include:
o Default Constructor: No parameters.
o Parameterized Constructor: With parameters to initialize fields. Example:

java
Copy code
public class MyClass {
int x;
MyClass() { x = 0; } // Default
MyClass(int val) { x = val; } // Parameterized
}

2. Explain the difference between static variable and static method:


o Static Variable: Belongs to the class, not instances. Shared among all
instances.
o Static Method: Belongs to the class. Can be called without creating an
instance. Cannot access non-static fields.
3. What is method overloading? Explain with an example: Method overloading
allows multiple methods with the same name but different parameters within a class.
Example:

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 { }

2. Write a Difference between abstract class and interface:


o Abstract Class: Can have both abstract and concrete methods. Can have
instance variables.
o Interface: Only abstract methods (until Java 8). Cannot have instance
variables, only constants.
3. Why multiple inheritances is not supported in Java: Multiple inheritance can lead
to the Diamond Problem, where a class inherits the same method from multiple
classes, leading to ambiguity. To avoid this, Java supports multiple inheritance
through interfaces.
4. Write a short note on abstract class: An abstract class provides a common
definition of a base class that multiple derived classes can share. It allows defining
methods that must be implemented by subclasses, ensuring a consistent interface.

Assignment No. 4 Unit 4

1. What is stream? Differentiate between stream source and stream destination: 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).
2. Write a program using FileReader and PrintWriter classes for file handling:

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();
}
}

3. Write a program to catch more than two exceptions:

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());
}
}
}

4. Differentiate between throw and throws exceptions / Keywords:


o throw: Used to explicitly throw an exception.
o throws: Declares that a method can throw exceptions, informing the caller to
handle them.

Assignment No. 5 Unit 5

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

You might also like