Advanced 250+ TOP MCQs on AutoCloseable, Closeable and Flushable Interfaces and Answers

This set of Advanced Java Multiple Choice Questions & Answers (MCQs) on “AutoCloseable, Closeable and Flushable Interfaces”.

1. Autocloseable was introduced in which Java version?
a) java SE 7
b) java SE 8
c) java SE 6
d) java SE 4

Answer: a
Clarification: Java 7 introduced autocloseable interface.

2. What is the alternative of using finally to close resource?
a) catch block
b) autocloseable interface to be implemented
c) try block
d) throw Exception

Answer: b
Clarification: Autocloseable interface provides close() method to close this resource and any other underlying resources.

3. Which of the below is a child interface of Autocloseable?
a) Closeable
b) Close
c) Auto
d) Cloneable

Answer: a
Clarification: A closeable interface extends autocloseable interface. A Closeable is a source or destination of data that can be closed.

4. It is a good practise to not throw which exception in close() method of autocloseable?
a) IOException
b) CustomException
c) InterruptedException
d) CloseException

Answer: c
Clarification: InterruptedException interacts with a thread’s interrupted status and runtime misbehavior is likely to occur if an InterruptedException is suppressed.

5. What will be the output of the following Java code snippet?

  1. try (InputStream is = ...) 
  2. {
  3.     // do stuff with is...
  4. } 
  5. catch (IOException e) 
  6. {
  7.     // handle exception
  8. }

a) Runtime Error
b) IOException
c) Compilation Error
d) Runs successfully

Answer: d
Clarification: Using java 7 and above, AutoCloseable objects can be opened in the try-block (within the ()) and will be automatically closed instead of using the finally block.

6. What is the difference between AutoCloseable and Closeable?
a) Closeable is an interface and AutoCloseable is a concrete class
b) Closeable throws IOException; AutoCloseable throws Exception
c) Closeable is a concept; AutoCloseable is an implementation
d) Closeable throws Exception; AutoCloseable throws IOException

Answer: b
Clarification: Closeable extends AutoCloseable and both are interfaces. Closeable throws IOException and AutoCloseable throws Exception.

7. What is the use of Flushable interface?
a) Flushes this stream by writing any buffered output to the underlying stream
b) Flushes this stream and starts reading again
c) Flushes this connection and closes it
d) Flushes this stream and throws FlushException

Answer: a
Clarification: Flushable interface provides flush() method which Flushes this stream by writing any buffered output to the underlying stream.

8. Which version of java added Flushable interface?
a) java SE 7
b) java SE 8
c) java SE 6
d) java SE 5

Answer: d
Clarification: Flushable and Closeable interface are added in java SE 5.

9. Does close() implicitly flush() the stream.
a) True
b) False

Answer: a
Clarification: close() closes the stream but it flushes it first.

10. AutoCloseable and Flushable are part of which package?
a) Autocloseable java.lang; Flushable java.io
b) Autocloseable java.io; Flushable java.lang
c) Autocloseable and Flushable java.io
d) Autocloseable and Flushable java.lang

Answer: a
Clarification: Autocloseable is a part of java.lang; Flushable is a part of java.io.

contest

Leave a Reply

Your email address will not be published. Required fields are marked *