0% found this document useful (0 votes)
10 views9 pages

5 Mark

The document provides an overview of Java I/O classes, including InputStream, OutputStream, Reader, Writer, and their uses for file handling and serialization. It explains serialization and deserialization with examples of a Student class and its persistence in files. Additionally, it covers annotations, the Map interface, legacy classes like Vector and Hashtable, and the concept and uses of lambda expressions in Java.

Uploaded by

saralasai522
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views9 pages

5 Mark

The document provides an overview of Java I/O classes, including InputStream, OutputStream, Reader, Writer, and their uses for file handling and serialization. It explains serialization and deserialization with examples of a Student class and its persistence in files. Additionally, it covers annotations, the Map interface, legacy classes like Vector and Hashtable, and the concept and uses of lambda expressions in Java.

Uploaded by

saralasai522
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Describe the I/O classes in java.

5
marks
In Java, I/O (Input/Output) classes are part of the java.io package and are used for
handling input and output operations, such as reading from and writing to files,
consoles, and streams. The key classes in Java I/O are:

1. InputStream & OutputStream (Byte Stream Classes)

• InputStream: Reads bytes from a source (e.g., FileInputStream).

• OutputStream: Writes bytes to a destination (e.g., FileOutputStream).

2. Reader & Writer (Character Stream Classes)

• Reader: Reads characters from a source (e.g., FileReader).

• Writer: Writes characters to a destination (e.g., FileWriter).

3. File Classes

• File: Represents file and directory pathnames.

• RandomAccessFile: Supports both reading and writing at specific file positions.

4. Buffered Classes (For efficient I/O operations)

• BufferedReader: Reads text efficiently from a character input stream.

• BufferedWriter: Writes text efficiently to a character output stream.

5. Object and Data Stream Classes

• ObjectInputStream & ObjectOutputStream: Used for object serialization and


deserialization.

• DataInputStream & DataOutputStream: Read and write primitive data types.

Illustrate Serialization and


Deserialization with an example . 5
mark answer
Serialization and Deserialization in Java

Serialization is the process of converting a Java object into a byte stream for
storage or transmission.

Deserialization is the reverse process, where the byte stream is converted back
into an object.

1. Student.java (Serializable Class)

import java.io.Serializable;

public class Student implements Serializable {

private static final long serialVersionUID = 1L; // Version control

int rno;

String name;

float fees;

// Constructor

public Student(int rno, String name, float fees) {

this.rno = rno;

this.name = name;

this.fees = fees;

// toString method to display object data

public String toString() {


return rno + " " + name + " " + fees;

2. Persist.java (Serialization)

import java.io.*;

class Persist {

public static void main(String args[]) {

try {

// Creating object

Student s1 = new Student(1, "Ram", 5000.50f);

// Writing object to a file

FileOutputStream fout = new FileOutputStream("d:\\f.txt");

ObjectOutputStream out = new ObjectOutputStream(fout);

out.writeObject(s1);

out.flush();

out.close();

System.out.println("Object Serialized Successfully!");

} catch (Exception e) {

System.out.println(e);

}
3. Depersist.java (Deserialization)

import java.io.*;

class Depersist {

public static void main(String args[]) {

try {

// Reading object from a file

FileInputStream fin = new FileInputStream("d:\\f.txt");

ObjectInputStream in = new ObjectInputStream(fin);

Student s = (Student) in.readObject(); // Deserialization

System.out.println("Deserialized Object: " + s);

in.close();

} catch (Exception e) {

System.out.println(e);

Describe about Annotations and its types with example.5


mark
Annotations and Its Types

Annotations in programming are metadata that provide additional information


about code without affecting its execution. They are commonly used in Java,
Python, and other languages to enhance code readability, enforce rules, and
support frameworks.

Types of Annotations

1. Marker Annotation – An annotation with no parameters, used as a flag.

Example:

@Override

public void myMethod() { ... }

2. Single-Value Annotation – An annotation with one parameter.

Example:

@Deprecated

public void oldMethod() { ... }

3. Multi-Value Annotation – An annotation with multiple parameters.

Example:

@SuppressWarnings({"unchecked", "deprecation"})

public void myMethod() { ... }

4. Custom Annotation – User-defined annotations for specific use cases.

Example:

@interface MyAnnotation {

String value();

Expalin Map Interface with an example


Map Interface in Java (5 Marks)

The Map interface in Java, part of java.util, is used to store key-value pairs. It
does not allow duplicate keys, but values can be duplicated. Common
implementations include HashMap, LinkedHashMap, and TreeMap.

Example Using HashMap

import java.util.*;

public class MapExample {

public static void main(String[] args) {

Map<Integer, String> map = new HashMap<>();

map.put(1, "Apple");

map.put(2, "Banana");

map.put(3, "Cherry");

System.out.println("Value for key 2: " + map.get(2)); // Output: Banana

Explain any two legacy classes of Java's Collection


Framework
Two Legacy Classes of Java’s Collection Framework (5 Marks)

Before Java 2 (JDK 1.2), Java had legacy collection classes, which were later
integrated into the Java Collection Framework (JCF). Two important legacy
classes are Vector and Hashtable.

1. Vector Class

• A dynamic array that grows automatically.

• Thread-safe (synchronized) but slower than ArrayList.


• Allows duplicate elements and maintains insertion order.

Example:

import java.util.Vector;

public class VectorExample {

public static void main(String[] args) {

Vector<String> vector = new Vector<>();

vector.add("Apple");

vector.add("Banana");

System.out.println("Vector: " + vector);

output : Vector: [Apple, Banana]

2. Hashtable Class

• Stores key-value pairs, similar to HashMap, but is synchronized (thread-safe).

• Does not allow null keys or null values.

Example:

import java.util.Hashtable;

public class HashtableExample {

public static void main(String[] args) {

Hashtable<Integer, String> hashtable = new Hashtable<>();


hashtable.put(1, "Java");

hashtable.put(2, "Python");

System.out.println("Hashtable: " + hashtable);

output: Hashtable: {1=Java, 2=Python}

Explain about Lambda expression and its uses 5 mark


Lambda Expression in Java (5 Marks, Simple Explanation)

What is a Lambda Expression?

A lambda expression is a short way to write functions in Java. It is used to


implement functional interfaces easily. Introduced in Java 8, it helps in
reducing code size and improving readability.

Syntax of Lambda Expression

(parameters) -> { function body }

Example With Lambda (After Java 8)

interface MyInterface {

void show();

public class Example {

public static void main(String[] args) {

MyInterface obj = () -> System.out.println("Hello!");

obj.show();
}

Output:

Hello!

Uses of Lambda Expressions

1. Reduces Code Size – Less boilerplate code.

2. Improves Readability – Makes code cleaner.

3. Works in Collections – Used in sorting and iteration.

4. Used in Multithreading – Simplifies thread creation.

5. Supports Functional Programming – Works with functional interfaces.

You might also like