This set of Advanced Java Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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?
try (InputStream is = ...)
{
// do stuff with is...
}
catch (IOException e)
{
// handle exception
}
a) Runtime Error
b) IOException
c) Compilation Error
d) Runs successfully
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: Flushable and Closeable interface are added in java SE 5.
9. Does close() implicitly flush() the stream.
a) True
b) False
View Answer
Explanation: 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
View Answer
Explanation: Autocloseable is a part of java.lang; Flushable is a part of java.io.
Sanfoundry Global Education & Learning Series – Java Programming Language.
To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check Java Books
- Practice Information Technology MCQs
- Apply for Java Internship
- Practice BCA MCQs
- Practice Programming MCQs