Mastering Java: The Ultimate Quiz for 'Thinking in Java'

Disable ads (and more) with a membership for a one time $2.99 payment

Mastering Java: The Ultimate Quiz for 'Thinking in Java'. Dive deep into Java with multiple-choice questions. Challenge yourself with quiz questions designed to test and improve your understanding of the 'Thinking in Java' book. Get ready for your exam!

Practice this question and more.


In which scenario would you use ByteArrayInputStream?

  1. When reading from a database

  2. When working with files

  3. When reading from a memory area

  4. When downloading data from the internet

The correct answer is: When reading from a memory area

When working with files (B), you would typically use FileInputStream instead, as it is optimized for reading bytes from a file. Similarily, when reading from a database (A), you would use a JDBC query to retrieve data, not ByteArrayInputStream. When downloading data from the internet (D), you would likely use an InputStream from URL or a similar mechanism designed for remote retrieval, not ByteArrayInputStream. Therefore, the only scenario where you would truly use ByteArrayInputStream is when you need to read from a memory area (C) such as a byte array. This can be useful when dealing with packets of data in network programming or when working with large arrays of binary data in memory.