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

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

Prepare for your Java exam with our ultimate quiz based on 'Thinking in Java'. Engage with expertly crafted questions that enhance your learning process. Perfect for Java enthusiasts looking to solidify their knowledge!

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

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.