Podcast
Questions and Answers
Which constructor in the String class creates an empty string?
Which constructor in the String class creates an empty string?
- String str = new String(); (correct)
- String str = new String("");
- String str = new String("Empty");
- String str = new String(null);
What is the purpose of the String constructor String(byte[] bytes)?
What is the purpose of the String constructor String(byte[] bytes)?
- To create a new String by decoding the specified array of bytes using the platform's default charset. (correct)
- To create a new String by encoding the specified array of bytes using the platform's default charset.
- To create a new String with the same characters as the specified byte array.
- To create a new String by converting the specified byte array to hexadecimal representation.
Which constructor is used to create a String object with the same sequence of characters as a specified StringBuffer?
Which constructor is used to create a String object with the same sequence of characters as a specified StringBuffer?
- String str = new String(StringBuffer.toString());
- String str = new String(buffer.toString());
- String str = new String(buffer);
- String str = buffer.toString(); (correct)
Which constructor creates an empty string?
Which constructor creates an empty string?
Which constructor creates a string with the specified content?
Which constructor creates a string with the specified content?
Which constructor creates a string with the same content as the given StringBuffer object?
Which constructor creates a string with the same content as the given StringBuffer object?
Study Notes
String Constructors
- The
String()
constructor creates an empty string. - The
String(byte[] bytes)
constructor is used to create a String object from a byte array, converting the byte array into a string by decoding the bytes using the platform's default charset. - The
String(StringBuffer buffer)
constructor creates a String object with the same sequence of characters as a specified StringBuffer. - Multiple constructors can create an empty string, but the
String()
constructor is one of them. - The
String(String original)
constructor creates a string with the specified content, by copying the characters from the original string. - The
String(StringBuffer buffer)
constructor also creates a string with the same content as the given StringBuffer object.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Java String Constructors Quiz: Test your knowledge on the different constructors available in the String class. Explore examples of constructors for creating empty strings, converting byte arrays, and constructing strings from StringBuffers. Enhance your understanding of Java string manipulation with this quiz.