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.


What exception is thrown if you call 'array()' on a buffer not backed by an array?

  1. UnsupportedOperationException

  2. BufferUnderflowException

  3. BufferOverflowException

  4. ReadOnlyBufferException

The correct answer is: UnsupportedOperationException

The exception overloaded is UnsupportedOperationException. Generally excess capacity in the buffer, when calling array function which convert to an array, it will throw UnsupportedOperationException, as there is no array in the ByteBuffer. The other options, BufferUnderflowException, BufferOverflowException, and ReadOnlyBufferException, are all thrown when there is an issue with the buffer's position, limit, or read/write permissions, respectively. These are not applicable in this scenario as the issue is with the buffer not having an underlying array. Therefore, these options can be ruled out as the correct answer.