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.


How can you make a buffer ready for writing new data?

  1. By calling flip()

  2. By calling clear()

  3. By setting limit

  4. By resetting position

The correct answer is: By calling clear()

A buffer is a data structure that allows data to be transferred from one place to another. When creating a buffer, the initial state is set for reading and writing, but in order to start writing new data, the buffer must first be cleared. Flipping the buffer using the flip() method does not clear the buffer, but rather adjusts the pointers for reading and writing. Setting the limit only restricts the number of elements that can be read from or written to the buffer, it does not make the buffer ready for writing new data. Similarly, resetting the position only changes the location of the pointer for reading and writing, it does not clear the buffer. Therefore, the correct answer is option B, by calling the clear() method.