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 method is used to calculate the number of characters in the String?

  1. count()

  2. calculateLength()

  3. size()

  4. length()

The correct answer is: length()

Explanation The method used to calculate the number of characters in a String is called length(). Option A, count(), is used to count the number of occurrences of a specific character in a String, not the total number of characters. Option B, calculateLength(), is not a predefined method in Java for String objects. Option C, size(), is used to get the number of elements in a collection, not the number of characters in a String. Therefore, option D, length(), is the correct and most appropriate method to use for finding the number of characters in a String.