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.


What method is used to convert a String to a byte array?

  1. toString

  2. toByteArray

  3. getBytes

  4. asString

The correct answer is: getBytes

A String is an array of characters, and in order to convert it into a byte array, we need to use a method that returns an array of bytes, rather than a String. toString(), asString(), and toByteArray() all return Strings, which makes them incorrect. The correct method is getBytes(), which actually converts the String into an array of bytes.