Which of the following is true about serialization in Java?
AAll classes are serializable by default
BA class must implement Serializable interface to be serialized
CSerialization is only for primitive types
DStatic fields are always serialized
Correct Answer:
B. A class must implement Serializable interface to be serialized
EXPLANATION
A class must explicitly implement the Serializable interface to be serialized. Static fields are not serialized, and not all classes are serializable by default.
Which of the following statements will correctly read a file line by line?
AFileInputStream fis = new FileInputStream("file.txt"); String line = fis.readLine();
BBufferedReader br = new BufferedReader(new FileReader("file.txt")); String line = br.readLine();
CFileReader fr = new FileReader("file.txt"); String line = fr.readLine();
DDataInputStream 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.
Which class allows reading and writing objects to a stream?
ADataInputStream and DataOutputStream
BObjectInputStream and ObjectOutputStream
CSerializableInputStream
DByteArrayStream
Correct Answer:
B. ObjectInputStream and ObjectOutputStream
EXPLANATION
ObjectInputStream and ObjectOutputStream are used for serialization and deserialization of Java objects. Objects must implement Serializable interface.
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.
Which method reads a complete line from a BufferedReader?
Aread()
BreadLine()
CreadString()
DnextLine()
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.