Java - IO - Interview Questions and Answers on Streams

Q1.  Tell something about BufferedWriter ? What are flush() and close() used for ?

Ans. A Buffer is a temporary storage area for data. The BufferedWriter class is an output stream.It is an abstract class that creates a buffered character-output stream.

Flush() is used to clear all the data characters stored in the buffer and clear the buffer.

Close() is used to closes the character output stream.

Q2.  What is Scanner class used for ? when was it introduced in Java ?

Ans. Scanner class introduced in Java 1.5 for reading Data Stream from the imput device. Previously we used to write code to read a input using DataInputStream. After reading the stream , we can convert into respective data type using in.next() as String ,in.nextInt() as integer, in.nextDouble() as Double etc 

Q3.  What is a stream and what are the types of Streams and classes of the Streams?

Ans. A Stream is an abstraction that either produces or consumes information. There are two types of Streams :

Byte Streams: Provide a convenient means for handling input and output of bytes.

Character  Streams:  Provide a convenient means for handling input & output of characters.

Byte Streams classes: Are defined by using two abstract classes, namely InputStream and OutputStream.

Character Streams classes: Are defined by using two abstract classes, namely Reader and Writer.

Q4.  What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy?

Ans. The Reader/Writer class hierarchy is character-oriented, and the InputStream/OutputStream class hierarchy is byte-oriented


Q5.  Which is the abstract parent class of FileWriter ?

Ans. OutputStreamWriter

Q6.  Which class is used to read streams of raw bytes from a file?

Ans. FileInputStream

Q7.  Which is the Parent class of FileInputStream ?

Ans. InputStream

Q8.  Which exceptions should be handled with the following code ?

FileOutputStream fileOutputStream = new FileOutputStream(new File("newFile.txt"));

Ans. FileNotFoundException

Q9.  Will this code compile fine ?

ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("newFile.txt")));

Ans. Yes.

Q10. Which is the Parent Class of  ByteArrayInputStream and AudioInputStream ?

Ans.InputStream

Q11. Which interfaces are implemented by  InputStream?

Ans.[Closeable,  AutoCloseable]

Q12. What is the package name for  ObjectInputStream class?

Ans.java.io

Q13. Which is the Parent Class of  StringBufferInputStream class?

Ans.InputStream

Q14. Which is the Parent Class of  SequenceInputStream class?

Ans.InputStream

Q15. Which is the Parent Class of  FilterInputStream class?

Ans.InputStream

Q16. What is the package name for  FilterInputStream class?

Ans.java.io

Q17. Which interfaces are implemented by  FileInputStream?

Ans.[Closeable,  AutoCloseable]

Q18. Which is the Parent Class of InputStream class?

Ans.Object

Q19. Which is the Parent Class of FileInputStream class?

Ans.InputStream