Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 441–450 of 958
Topics in Java Programming
Q.441 Hard I/O Streams
What happens if you call close() multiple times on a stream?
A Throws exception on second call
B First call closes; subsequent calls are typically ignored
C Causes memory leak
D Reopens the stream
Correct Answer:  B. First call closes; subsequent calls are typically ignored
EXPLANATION

Most streams in Java are idempotent regarding close() - calling it multiple times is safe and subsequent calls typically do nothing.

Test
Q.442 Hard I/O Streams
In Java NIO, which class replaces traditional Stream-based I/O for better performance?
A FastInputStream
B ByteBuffer and Channels
C HighPerformanceStream
D AsyncInputStream
Correct Answer:  B. ByteBuffer and Channels
EXPLANATION

Java NIO (New I/O) uses ByteBuffer and Channels for non-blocking, scalable I/O operations with better performance than traditional streams.

Test
Q.443 Medium I/O Streams
What is the purpose of the skip() method in InputStream?
A Skip the entire stream
B Skip a specified number of bytes in the stream
C Skip to the end of the stream
D Skip error handling
Correct Answer:  B. Skip a specified number of bytes in the stream
EXPLANATION

The skip(long n) method skips n bytes in the input stream and returns the number of bytes actually skipped.

Test
Q.444 Medium I/O Streams
Which of the following statements will correctly read a file line by line?
A FileInputStream fis = new FileInputStream("file.txt"); String line = fis.readLine();
B BufferedReader br = new BufferedReader(new FileReader("file.txt")); String line = br.readLine();
C FileReader fr = new FileReader("file.txt"); String line = fr.readLine();
D DataInputStream dis = new DataInputStream(new FileInputStream("file.txt")); String line = dis.readLine();
Correct Answer:  B. BufferedReader br = new BufferedReader(new FileReader("file.txt")); String line = br.readLine();
EXPLANATION

BufferedReader's readLine() method is the standard way to read lines. FileInputStream doesn't have readLine(), FileReader doesn't have readLine() either.

Test
Q.445 Medium I/O Streams
What is the mark() method used for in BufferedReader?
A Mark end of stream
B Mark current position in stream for later reset
C Mark data as corrupted
D Mark stream as closed
Correct Answer:  B. Mark current position in stream for later reset
EXPLANATION

The mark() method marks the current position in the stream so it can be returned to later using reset(). Requires markSupported() to return true.

Test
Q.446 Medium I/O Streams
Which class allows reading and writing objects to a stream?
A DataInputStream and DataOutputStream
B ObjectInputStream and ObjectOutputStream
C SerializableInputStream
D ByteArrayStream
Correct Answer:  B. ObjectInputStream and ObjectOutputStream
EXPLANATION

ObjectInputStream and ObjectOutputStream are used for serialization and deserialization of Java objects. Objects must implement Serializable interface.

Test
Q.447 Medium I/O Streams
What is the difference between FileInputStream and BufferedInputStream?
A No difference; they are aliases
B FileInputStream reads directly from file; BufferedInputStream adds buffering for efficiency
C BufferedInputStream reads characters; FileInputStream reads bytes
D FileInputStream is deprecated in Java 11+
Correct Answer:  B. FileInputStream reads directly from file; BufferedInputStream adds buffering for efficiency
EXPLANATION

FileInputStream reads directly from a file byte by byte, while BufferedInputStream wraps another stream and adds buffering to improve performance by reducing I/O operations.

Test
Q.448 Easy I/O Streams
Which method reads a complete line from a BufferedReader?
A read()
B readLine()
C readString()
D nextLine()
Correct Answer:  B. readLine()
EXPLANATION

The readLine() method reads a line of text from the BufferedReader, returning a String without the newline character, or null if end of stream is reached.

Test
Q.449 Easy I/O Streams
What exception is thrown when a file is not found while creating FileInputStream?
A NullPointerException
B FileNotFoundException
C IOException
D StreamException
Correct Answer:  B. FileNotFoundException
EXPLANATION

FileNotFoundException (a subclass of IOException) is thrown when the specified file does not exist or cannot be opened.

Test
Q.450 Medium I/O Streams
Which of the following creates a bridge between character and byte streams?
A BufferedReader
B InputStreamReader
C DataInputStream
D ObjectInputStream
Correct Answer:  B. InputStreamReader
EXPLANATION

InputStreamReader converts a byte stream (InputStream) into a character stream (Reader), acting as a bridge between the two.

Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips