Which of the following statements are true?

  1. Strings are a primitive type in Java and the StringBuffer is used as the matching wrapper type

  2. The size of a string can be retrieved using the length property.

  3. Strings are a primitive type in Java that overloads the + operator for concatenation

  4. The String class is implemented as a char array, elements are addressed using the stringname[] convention


Correct Option: C

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) Strings are a primitive type in Java and the StringBuffer is used as the matching wrapper type. This option is incorrect because strings are not primitive types in Java. They are objects of the String class, which is part of the Java API. The StringBuffer class is used for mutable strings, but it is not a wrapper type for strings.

Option B) The size of a string can be retrieved using the length property. This option is correct. In Java, the length() method is used to get the number of characters in a string.

Option C) Strings are a primitive type in Java that overloads the + operator for concatenation. This option is correct. In Java, strings are not primitive types, but they are treated as first-class citizens and have a special syntax for concatenation using the + operator.

Option D) The String class is implemented as a char array, elements are addressed using the stringname[] convention. This option is incorrect. While the String class does contain a char array internally, the elements of the array cannot be directly addressed using the stringname[] convention. Instead, you need to use methods provided by the String class to access or manipulate the characters.

The correct answer is C. Strings are not primitive types in Java but are treated as such and overload the + operator for concatenation.

Find more quizzes: