Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

270 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 121–130 of 270
Topics in Java Programming
Q.121 Easy I/O Streams
What will be the output of reading from a ByteArrayInputStream initialized with bytes [65, 66, 67] after calling read() three times?
A 65, 66, 67
B ABC
C A, B, C
D -1
Correct Answer:  A. 65, 66, 67
EXPLANATION

read() returns integer values of bytes. [65, 66, 67] are ASCII values for 'A', 'B', 'C'. Multiple read() calls return these values sequentially.

Take Test
Q.122 Easy I/O Streams
Which of the following correctly demonstrates chaining multiple filters in Java I/O?
A new BufferedReader(new FileReader(file))
B new FileReader(new BufferedReader(file))
C new Reader(new BufferedReader(new FileReader(file)))
D new BufferedInputStream(new FileInputStream(new File(file)))
Correct Answer:  A. new BufferedReader(new FileReader(file))
EXPLANATION

Filter streams are chained from inside-out. FileReader is wrapped with BufferedReader for better performance.

Take Test
Q.123 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.

Take Test
Q.124 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.

Take Test
Q.125 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.

Take Test
Q.126 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.

Take Test
Q.127 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.

Take Test
Q.128 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.

Take Test
What is the primary difference between throw and throws in Java exception handling?
A throw is used in catch block, throws is used in method declaration
B throw explicitly raises an exception, throws declares that a method may throw exceptions
C throw handles exceptions, throws creates exceptions
D Both are identical; throws is just an older syntax
Correct Answer:  B. throw explicitly raises an exception, throws declares that a method may throw exceptions
EXPLANATION

'throw' is a statement that explicitly throws an exception object. 'throws' is a clause in method signature indicating the method may throw specific checked exceptions. Example: throw new IOException(); vs public void method() throws IOException {}

Take Test
What is the output of the following code?
try {
int x = 10/0;
} catch(ArithmeticException e) {
System.out.println("Caught");
} finally {
System.out.println("Finally");
}
System.out.println("After");
A Caught\nFinally\nAfter
B Caught\nFinally
C Finally\nAfter
D ArithmeticException is thrown, program terminates
Correct Answer:  A. Caught\nFinally\nAfter
EXPLANATION

When ArithmeticException occurs, it's caught by the catch block printing 'Caught'. The finally block always executes, printing 'Finally'. Then normal program flow continues, printing 'After'.

Take 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