This set of Advanced Java Multiple Choice Questions & Answers (MCQs) on “AutoCloseable, Closeable and Flushable Interfaces”.
1. Autocloseable was introduced in which Java version? Answer: a 2. What is the alternative of using finally to close resource? Answer: b 3. Which of the below is a child interface of Autocloseable? Answer: a 4. It is a good practise to not throw which exception in close() method of autocloseable? Answer: c 5. What will be the output of the following Java code snippet? a) Runtime Error Answer: d 6. What is the difference between AutoCloseable and Closeable? Answer: b 7. What is the use of Flushable interface? Answer: a 8. Which version of java added Flushable interface? Answer: d 9. Does close() implicitly flush() the stream. Answer: a 10. AutoCloseable and Flushable are part of which package? Answer: a
a) java SE 7
b) java SE 8
c) java SE 6
d) java SE 4
Clarification: Java 7 introduced autocloseable interface.
a) catch block
b) autocloseable interface to be implemented
c) try block
d) throw Exception
Clarification: Autocloseable interface provides close() method to close this resource and any other underlying resources.
a) Closeable
b) Close
c) Auto
d) Cloneable
Clarification: A closeable interface extends autocloseable interface. A Closeable is a source or destination of data that can be closed.
a) IOException
b) CustomException
c) InterruptedException
d) CloseException
Clarification: InterruptedException interacts with a thread’s interrupted status and runtime misbehavior is likely to occur if an InterruptedException is suppressed.
try (InputStream is = ...)
{
// do stuff with is...
}
catch (IOException e)
{
// handle exception
}
b) IOException
c) Compilation Error
d) Runs successfully
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.
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
Clarification: Closeable extends AutoCloseable and both are interfaces. Closeable throws IOException and AutoCloseable throws Exception.
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
Clarification: Flushable interface provides flush() method which Flushes this stream by writing any buffered output to the underlying stream.
a) java SE 7
b) java SE 8
c) java SE 6
d) java SE 5
Clarification: Flushable and Closeable interface are added in java SE 5.
a) True
b) False
Clarification: close() closes the stream but it flushes it first.
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
Clarification: Autocloseable is a part of java.lang; Flushable is a part of java.io.