JAVA ST-2 Solutions
JAVA ST-2 Solutions
Q2:What are the advantages of stream in java, programming? Di erentiate terminal and
intermediate stream methods
Ans: Advantages of Streams in Java
1. Clean & Concise Code: Reduces boilerplate, makes code more readable.
2. Functional Style: Supports operations like map, lter, reduce.
3. E cient Processing: Can be lazy and optimized.
4. Easy Parallelism: Use .parallelStream() for multi-core processing.
5. Chained Operations: Allows powerful data transformation pipelines.
fi
ffi
fi
fi
fi
fi
ff
Example:
List<String> names = Arrays.asList("Alice", "Bob", "Anna");
System.out.println(count); //
Output: 2
Or
Q2: Write short Note on:
a. Sealed Classes b. Java Module System
Example:
public sealed class Shape permits Circle, Square {}
Bene ts:
• Limits subclassing
• Makes code more predictable and safe
Purpose:
• Better encapsulation
• Avoid classpath issues
• Support for large applications
Bene ts:
• Explicit dependencies
• Faster startup and performance
• Better code organization
// Encode
String encodedText = Base64.getEncoder().encodeToString(originalText.getBytes());
System.out.println("Encoded: " + encodedText);
// Decode
byte[] decodedBytes = Base64.getDecoder().decode(encodedText);
String decodedText = new String(decodedBytes);
System.out.println("Decoded: " + decodedText);
}
}
Output:
Encoded: SGVsbG8sIFdvcmxkIQ==
Decoded: Hello, World!
Or
Q3: Write a java program demonstrate the use of try with resources with example code.
Ans: Java Program: Try-with-Resources Example
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
// Try-with-resources block
try (BufferedReader reader = new BufferedReader(new FileReader( lePath))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("Error reading le: " + e.getMessage());
}
}
}
Key Points:
• No need to explicitly close Bu eredReader — it’s done automatically.
• Any class that implements AutoCloseable or Closeable can be used.
Section B
Q4: Write a java program t o Read data from a text le like sample.txt which is available in d:/sample
code folder using FileReader. [TCS]
Ans: Java Program to Read File Using FileReader
import java.io.FileReader;
import java.io.IOException;
fi
fi
ff
fi
fi
fi
public class ReadFileExample {
public static void main(String[] args) {
String lePath = "D://samplecode//sample.txt";
Notes:
• Use double slashes (//) in le path or a single backslash with escaping (\\).
• The try-with-resources automatically closes the FileReader.
• This is suitable for TCS Java le handling questions.
Or
Q2: Write a java program t o create 4 threads using Runnable interface, set the names and run all the
threads. [Infosys]
Ans: Java Program: 4 Threads Using Runnable
class MyTask implements Runnable {
@Override
public void run() {
System.out.println("Thread running: " + Thread.currentThread().getName());
}
}
// Create 4 threads
Thread t1 = new Thread(task, "Thread-1");
Thread t2 = new Thread(task, "Thread-2");
Thread t3 = new Thread(task, "Thread-3");
Thread t4 = new Thread(task, "Thread-4");
Output (Sample):
Thread running: Thread-1
Thread running: Thread-3
Thread running: Thread-2
Thread running: Thread-4
(Note: Output order may vary as threads run concurrently.)
fi
fi
fi
fi
fi
Q5: Di erentiate between ByteStream and CharacterStream with suitable example.
Summary:
• Use ByteStreams for binary data.
• Use CharacterStreams for text data.
ff
fi
fi
fi
Or
Q5:Discuss the role of multithreading programming in large scale application. Explain life cycle of a
thread.
Ans: Role of Multithreading in Large-Scale Applications
Multithreading allows multiple tasks to run concurrently in the same program. It’s critical for performance
and responsiveness in large-scale applications.
Key Roles:
1. Improved Performance: Tasks like I/O, processing, or computation can run in parallel.
2. Responsiveness: UI or web servers stay active while background tasks run.
3. Resource Sharing: Threads share memory space e ciently.
4. Scalability: Makes full use of multi-core CPUs.
5. Faster Execution: Reduces idle time and increases throughput.
Use Cases:
• Web servers handling multiple requests
• Game engines (UI + logic + physics running in parallel)
• Financial applications (real-time trading + logging + monitoring)
1. New
• Thread is created using Thread t = new Thread();
• Not started yet
2. Runnable
• After calling start()
• Ready to run, but may wait for CPU
3. Running
• Actively executing the run() method
4. Blocked/Waiting/Sleeping
• Temporarily inactive
• Blocked: waiting for a resource
• Sleeping: using Thread.sleep(ms)
• Waiting: using wait() or join()
5. Terminated (Dead)
• Run method is completed or exception occurred