Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

270 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 111–120 of 270
Topics in Java Programming
Q.111 Easy I/O Streams
Which interface must be implemented by a class to make its objects serializable?
A Cloneable
B Serializable
C Comparable
D Iterable
Correct Answer:  B. Serializable
EXPLANATION

The Serializable interface is a marker interface that indicates a class can be serialized. It's found in java.io package and requires no methods to be implemented.

Take Test
Q.112 Easy I/O Streams
What is the output of the following code?
FileReader fr = new FileReader("test.txt");
int data = fr.read();
System.out.println(data);
A Character from the file
B ASCII value of the character
C -1 if EOF is reached
D File not found exception
Correct Answer:  B. ASCII value of the character
EXPLANATION

FileReader.read() returns the ASCII/Unicode value of the character as an integer, not the character itself. It returns -1 when EOF is reached.

Take Test
Q.113 Easy I/O Streams
Which class in Java is used to read primitive data types from a binary stream?
A DataInputStream
B BufferedInputStream
C FileInputStream
D ObjectInputStream
Correct Answer:  A. DataInputStream
EXPLANATION

DataInputStream is specifically designed to read primitive data types (int, float, double, etc.) from a binary stream using methods like readInt(), readFloat(), etc.

Take Test
Q.114 Easy I/O Streams
What happens when you attempt to read from a closed InputStream?
A Returns -1
B Throws IOException
C Returns empty byte array
D Silently ignores the read request
Correct Answer:  B. Throws IOException
EXPLANATION

Attempting to read from a closed stream throws an IOException with message 'Stream closed', preventing accidental data processing from invalid sources.

Take Test
Q.115 Easy I/O Streams
Which class would you use to read and write at arbitrary positions in a file?
A BufferedReader
B FileInputStream
C RandomAccessFile
D PushbackInputStream
Correct Answer:  C. RandomAccessFile
EXPLANATION

RandomAccessFile allows reading and writing at arbitrary positions using seek() method, useful for direct access to file contents.

Take Test
Q.116 Easy I/O Streams
Which of the following is a character-based stream in Java?
A InputStream
B FileInputStream
C FileReader
D DataInputStream
Correct Answer:  C. FileReader
EXPLANATION

FileReader is a character-based stream that reads characters from a file. InputStream and its subclasses are byte-based streams.

Take Test
Q.117 Easy I/O Streams
What is the purpose of the flush() method in PrintWriter?
A Close the stream permanently
B Clear the buffer and write pending data to the underlying stream
C Reset the file pointer to the beginning
D Skip unwanted characters from input
Correct Answer:  B. Clear the buffer and write pending data to the underlying stream
EXPLANATION

The flush() method clears the internal buffer and forces any buffered output to be written to the underlying stream without closing it.

Take Test
Q.118 Easy I/O Streams
Which class in Java I/O streams is used to read primitive data types from a binary file?
A DataInputStream
B FileReader
C BufferedInputStream
D InputStreamReader
Correct Answer:  A. DataInputStream
EXPLANATION

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

Take Test
Q.119 Easy I/O Streams
When using ObjectInputStream, what exception is thrown if the serialVersionUID doesn't match?
A SerializationException
B InvalidClassException
C ClassNotFoundException
D IOException
Correct Answer:  B. InvalidClassException
EXPLANATION

InvalidClassException is thrown when deserialized class's serialVersionUID doesn't match the serialized object.

Take Test
Q.120 Easy I/O Streams
A RandomAccessFile is opened with mode 'r'. Which operation will throw an exception?
A Reading bytes from the file
B Seeking to a specific position
C Writing bytes to the file
D Closing the file
Correct Answer:  C. Writing bytes to the file
EXPLANATION

Mode 'r' allows read-only access. Write operations will throw IOException.

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