Advanced Java Questions & Answers – AutoCloseable, Closeable and Flushable Interfaces

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

Answer: a
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

Answer: b
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

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

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

Answer: c
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?

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  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
View Answer

Answer: d
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.
advertisement

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

Answer: b
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

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

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

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

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

Answer: a
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

Answer: a
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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.