1.
a
) Explain the significance of each word in public static void main(string args)
public- It is an access specifier that is accessible by JVM from anywhere
static- It is a keyword that enables the method to be called using class name without
the need to create an object of that class.
void- It is the return type which means that the method does not return a value
main- It is the default method name.
b) Write about bitwise operators in java
● Bitwise OR (|)
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)
Bitwise OR Operation of 5 and 7
0101
| 0111
________
0111 = 7 (In decimal)
● B
itwise AND (&)
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)
Bitwise AND Operation of 5 and 7
0101
& 0111
________
0101 = 5 (In decimal)
● B
itwise XOR (^)
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)
Bitwise XOR Operation of 5 and 7
0101
^ 0111
________
0010 = 2 (In decimal)
● B
itwise Complement (~)
a = 5 = 0101 (In Binary)
Bitwise Complement Operation of 5
~ 0101
________
1010 = 10 (In decimal)
) Write about this keyword.
c
This keyword refers to the current object in a method or constructor. The most common
use of the this keyword is to eliminate the confusion between class attributes and
parameters with the same name
) Discuss about Exception Handling Fundamentals.
d
Java exception handling is managed via five keywords:try, catch, throw,
throws,and
finally.They form an interrelated subsystem in which the use of one
implies the use of [Link] are the key fundamentals of exception handling in Java:
xception Types:
E
Java distinguishes between two types of exceptions: checked exceptions and
unchecked exceptions.
try-catch Blocks:
Exceptions are handled using try-catch blocks. The
tryblock contains the code that
might throw an exception, and the
catchblock catches and handles the exception if it
occurs.
throw and throws:
Exceptions can be thrown explicitly using the
throwand
throwskeyword or
implementing
throwableinterface
) What is a deadlock?
e
Deadlock is a situation in concurrent programming where two or more processes are
stuck in a circular dependency, each waiting for the other to release a resource.
f ) What is a file, directories?
In Java, a file represents a named sequence of bytes stored on a filesystem. It can
contain data or code, and it can exist in various forms.
Directories are used to organize files into hierarchical structures.
In Java, directories are represented by the
Fileclass
) Write a short note on Abstract Classes.
g
Abstract classes in Java are classes that cannot be instantiated on their own, serving as
templates or blueprints for other classes to inherit from. They are declared using the
abstract keyword. Abstract classes may contain both abstract and non-abstract
methods.
2. a)Explain the buzzwords and the three OOP principles of Java.
Java Buzzwords/Features:
OOP Principles:
● Encapsulation:
Binding of data and methods together to avoid outside interference/misuse.
● Inheritance:
Acquiring the properties of one object into another.
● Polymorphism:
An object or a method having many forms each capable of performing different
tasks.
)Can a superclass variable refer to a subclass object. Justify your answer.
b
Yes, in Java, a superclass variable can refer to a subclass object.
This is so because subclass object derived from that superclass because a subclass is a
special case of the superclass.
For example:
A obj=new B();
obj.m();
In this example, “A” is the reference of the superclass and “B” is the object of the
subclass, and “m()” of subclass “B” will be executed at runtime.
)Explain about package and import with a java program.
3. a
Package is a collection of classes and interfaces
The import statement is used to access classes and interfaces from other packages.
// File: [Link]
import [Link]; // Importing Greet class from utilities package
public class Main {
public static void main(String[] args) {
[Link]();
}
}
// File: [Link]
package utilities;
public class Greet {
public static void greet() {
[Link]("Hello, World!");
}
}
) Explain about chained exceptions with a java program.
b
Chained Exceptions allows to relate one exception with another exception, i.e one
exception describes cause of another exception.
public class ChainedExceptionsExample {
public static void main(String[] args) {
try {
method1();
} catch (Exception e) {
[Link]("Caught exception: " + [Link]());
S
[Link]("Original cause: ");
[Link]();
}
}
public static void method1() throws Exception {
try {
method2();
} catch (Exception e) {
// Wrapping the caught exception and throwing a new exception
throw new Exception("Exception in method1", e);
}
}
public static void method2() throws Exception {
// Simulating an exception occurring in method2
throw new Exception("Exception in method2");
}
}
}
T
● he method2() throws an exception with a custom message indicating an error.
● The method1() calls method2() within a try-catch block. If an exception occurs in
method2(), method1() catches it, wraps it in a new exception, and rethrows it.
● The main() calls method1() within a try-catch block. If an exception occurs in
method1(), main() catches it, and throws the exception (Chained Exception).
4. a)Write a java program to create three threads
public class ThreeThreadsExample {
public static void main(String[] args) {
// Creating and starting three threads
Thread thread1 = new Thread(new MyRunnable("Thread 1"));
Thread thread2 = new Thread(new MyRunnable("Thread 2"));
Thread thread3 = new Thread(new MyRunnable("Thread 3"));
t [Link]();
[Link]();
[Link]();
}
}
class MyRunnable implements Runnable {
private String name;
public MyRunnable(String name) {
[Link] = name;
}
Override
@
public void run() {
// Printing a message from the thread
[Link](name + " is running.");
}
}
UTPUT:
O
Thread 1 is running.
Thread 2 is running.
Thread 3 is running.
) Explain about synchronization with program.
b
Synchronization in Java is the capability to control the access of multiple threads to any
shared resource, so that the threads can carry out independent task easily.
public class SynchronizationExample {
private static int counter = 0;
public static void main(String[] args) {
// Creating and starting multiple threads
for (int i = 0; i < 5; i++) {
Thread thread = new Thread(() -> {
for (int j = 0; j < 1000; j++) {
incrementCounter();
}
});
[Link]();
}
/ / Waiting for all threads to finish
try {
[Link](2000); // Wait for 2 seconds for all threads to finish
T
} catch (InterruptedException e) {
[Link]();
}
/ / Printing the final value of the counter
[Link]("Final Counter Value: " + counter);
}
private static void incrementCounter() {
counter++;
}
}
5. a
) Explain about TreeSet class with program.
In Java, TreeSet is a class that implements the Set interface, which represents a
collection of unique elements stored in a sorted order.
import [Link];
public class TreeSetExample {
public static void main(String[] args) {
// Creating a TreeSet
TreeSet<Integer> treeSet = new TreeSet<>();
// Adding elements to the TreeSet
[Link](10);
[Link](5);
[Link](20);
[Link](15);
[Link](5); // Duplicate element (ignored)
// Printing the TreeSet (in sorted order)
[Link]("TreeSet elements: " + treeSet);
// Removing an element from the TreeSet
[Link](15);
// Printing the TreeSet after removal
[Link]("TreeSet elements after removal: " + treeSet);
}
}
)what is an iterator and demonstrate it with a program.
b
In Java, an iterator is an interface provided by the [Link] package, which allows you to
traverse through the elements of a collection sequentially. Iterators provide a
standardized way to access elements of various collections independently of their
underlying implementation.
import [Link];
import [Link];
public class IteratorExample {
public static void main(String[] args) {
// Creating an ArrayList
ArrayList<String> arrayList = new ArrayList<>();
// Adding elements to the ArrayList
[Link]("Apple");
[Link]("Banana");
[Link]("Orange");
// Obtaining an iterator for the ArrayList
Iterator<String> iterator = [Link]();
// Iterating through the elements using the iterator
[Link]("Iterating through the elements of the ArrayList:");
while ([Link]()) {
String element = [Link]();
[Link](element);
}
}
}
UTPUT:
O
Iterating through elements:
Apple
Banana
Orange
6. a)Explain about String Tokenizer
tringTokenizer is a legacy class in Java that is used to break a string into tokens based
S
on a delimiter or a set of [Link] is part of the [Link] package.
import [Link];
public class StringTokenizerExample {
public static void main(String[] args) {
String text = "apple,banana,orange,grape";
// Create a StringTokenizer with comma as delimiter
StringTokenizer tokenizer = new StringTokenizer(text, ",");
// Iterate through tokens
while ([Link]()) {
String token = [Link]();
[Link](token);
}
}
}
UTPUT:
O
Apple
Banana
Orange
) Explain about Serialization.
b
Serialization in Java refers to the process of converting an object into a byte stream, which can
be stored or saved in a persistent storage such as a file.
At a later time the data can be read by converting byte stream back into an object is called
Deserialization.
import [Link].*;
public class SerializationExample {
public static void main(String[] args) {
// Serialization
try {
// Creating an object to serialize
MyClass objectToSerialize = new MyClass("John", 30);
// Writing the object to a file
ObjectOutputStream outputStream = new ObjectOutputStream(new
FileOutputStream("[Link]"));
[Link](objectToSerialize);
[Link]();
[Link]("Object serialized successfully.");
} catch (IOException e) {
[Link]();
}
// Deserialization
try {
// Reading the object from the file
ObjectInputStream inputStream = new ObjectInputStream(new
FileInputStream("[Link]"));
MyClass deserializedObject = (MyClass) [Link]();
[Link]();
// Displaying the deserialized object
[Link]("Deserialized object: " + deserializedObject);
} catch (IOException | ClassNotFoundException e) {
[Link]();
}
}
}
class MyClass implements Serializable {
private String name;
private int age;
public MyClass(String name, int age) {
[Link] = name;
[Link] = age;
}
Override
@
public String toString() {
return "MyClass{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
)Write a program to demonstrate mouse handling events.
7. a
import [Link].*;
import [Link].*;
/*<applet code = "MouseEvents" width = "300" height = "300"></applet>*/
public class MouseEvents extends Frame implements MouseListener
{
Label l;
void MouseListenerExample()
{
addMouseListener(this);
Font boldFont =new Font(Font.SANS_SERIF , [Link],24);
l=new Label();
[Link](boldFont);
[Link]([Link]);
[Link](150,100,200,30);
add(l); setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e)
{
[Link]("Mouse Clicked ");
}
public void mouseEntered(MouseEvent e)
{
[Link]("Mouse entered ");
}
public void mouseExited(MouseEvent e)
{
[Link]("Mouse Exited");
}
public void mousePressed(MouseEvent e)
{
[Link]("Mouse Pressed");
}
public void mouseReleased(MouseEvent e)
{
[Link]("Mouse Realesed");
}
public static void main(String[] args)
{
new MouseListenerExample();
}
}
b) Write a swing application demonstrating JLabel and JScrollPane.
import [Link].*;
public class JLabelJScrollPaneExample {
public static void main(String[] args) {
// Create a JFrame
JFrame frame = new JFrame("JLabel and JScrollPane Example");
[Link](300, 200);
/ / Create a long text
String longText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. ";
/ / Create a JLabel with the long text
JLabel label = new JLabel(longText);
/ / Create a JScrollPane and add the JLabel to it
JScrollPane scrollPane = new JScrollPane(label);
/ / Add the JScrollPane to the JFrame
[Link]().add(scrollPane);
/ / Set JFrame visibility
[Link](true);
}
}