Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 451–460 of 958
Topics in Java Programming
Q.451 Easy I/O Streams
What is the return type of FileInputStream's read() method?
A byte
B int
C char
D void
Correct Answer:  B. int
EXPLANATION

The read() method returns an int. It returns the byte value (0-255) or -1 if end of stream is reached. This is why it needs to be cast to byte if needed.

Test
Q.452 Medium I/O Streams
Which class is used to read primitive data types from a stream?
A ObjectInputStream
B DataInputStream
C BufferedInputStream
D InputStreamReader
Correct Answer:  B. DataInputStream
EXPLANATION

DataInputStream provides methods like readInt(), readDouble(), readBoolean() to read primitive data types from a stream.

Test
Q.453 Easy I/O Streams
What is the purpose of the flush() method in output streams?
A Close the stream
B Clear the buffer and write pending data to the underlying stream
C Reset the stream position
D Release memory allocated to the stream
Correct Answer:  B. Clear the buffer and write pending data to the underlying stream
EXPLANATION

The flush() method forces any buffered output to be written to the underlying stream immediately without closing it.

Test
Q.454 Medium I/O Streams
Which of the following is a filtered stream?
A FileOutputStream
B BufferedOutputStream
C ByteArrayOutputStream
D PipedOutputStream
Correct Answer:  B. BufferedOutputStream
EXPLANATION

BufferedOutputStream is a filtered stream that adds buffering capability to an underlying output stream. Filtered streams wrap other streams to add functionality.

Test
Q.455 Medium I/O Streams
What happens when you try to read from a closed stream in Java?
A Returns -1
B Throws IOException
C Returns null
D Blocks indefinitely
Correct Answer:  B. Throws IOException
EXPLANATION

Attempting to read from a closed stream throws an IOException. The stream must be open for read operations.

Test
Q.456 Easy I/O Streams
Which method is used to read a single byte from a FileInputStream?
A read()
B readByte()
C readSingleByte()
D nextByte()
Correct Answer:  A. read()
EXPLANATION

The read() method reads a single byte and returns it as an integer. Returns -1 if end of stream is reached.

Test
Q.457 Medium I/O Streams
What is the default buffer size of BufferedReader in Java?
A 512 bytes
B 1024 bytes
C 2048 bytes
D 4096 bytes
Correct Answer:  C. 2048 bytes
EXPLANATION

BufferedReader has a default buffer size of 8192 characters, but commonly 2048 bytes is used. The exact implementation may vary, but 8192 is the default character buffer.

Test
Q.458 Easy I/O Streams
Which of the following is a character stream class in Java?
A FileReader
B FileInputStream
C DataInputStream
D BufferedInputStream
Correct Answer:  A. FileReader
EXPLANATION

FileReader is a character stream class that reads characters from a file. FileInputStream, DataInputStream, and BufferedInputStream are byte stream classes.

Test
Q.459 Medium Exception Handling
In Java, when an exception is thrown in a try block and caught in a catch block, if the catch block also throws an exception, what happens to the original exception?
A The original exception is lost and only the new exception propagates
B Both exceptions are chained together automatically
C The original exception is stored in the cause field if exception chaining is used
D The JVM throws a CompoundException containing both
Correct Answer:  C. The original exception is stored in the cause field if exception chaining is used
EXPLANATION

When exception chaining is explicitly used with initCause() or constructor parameters, the original exception becomes the cause of the new exception. Without explicit chaining, the original exception is lost. Exception chaining is not automatic in Java.

Test
Which scenario would cause a StackOverflowError in Java exception handling?
A Catching too many different exception types
B A method recursively catching and rethrowing the same exception infinitely
C Having more than 10 nested try-catch blocks
D Using throw keyword more than once in a method
Correct Answer:  B. A method recursively catching and rethrowing the same exception infinitely
EXPLANATION

StackOverflowError occurs when stack memory exhausts due to deep recursion. If a method catches an exception and immediately rethrows it without modification, calling itself, stack frames accumulate until overflow. This is a runtime error, not a checked exception.

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