Skip to content
Sharp

Engineering Interviews · interview question

How would you explain a common mistake when using InputStream in Java that can lead to resource leaks in an interview?

Multiple choiceMediumchapter-20-curatedpremiumengineering-interviewscode-review-spotting-bugs
java
InputStream in = new FileInputStream("file.txt");
// read data...
// missing in.close()
Choose one answer
Answer and explanation

Correct answer

C. Forgetting to close the InputStream.

Why

Failing to close an InputStream after use can lead to resource leaks, as the system resources are not released. Always close streams in a finally block or use try-with-resources.

Test yourself on Engineering Interviews

A free test with instant, explained results. No account needed.

Start free test
All Engineering Interviews questions →