Question:

Which of the following constructor of string class is used to create a string object with no character in Java?

Show Hint

The constructor {String()} creates an empty string whose length is {0}.
  • String()
  • String(char ary[])
  • String(string strObj)
  • String(string literal)
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is A

Solution and Explanation

Step 1: Understanding constructors in the String class.
In Java, the String class provides multiple constructors that allow programmers to create string objects in different ways. Constructors are special methods used to initialize objects when they are created.
Step 2: Purpose of String() constructor.
The constructor String() creates an empty string object. This means that the string object will be initialized but will contain no characters.
Example:
String s = new String();
Here, the variable s holds an empty string. The length of this string will be 0.
Step 3: Evaluation of options.
(A) String(): Correct. It creates an empty string with no characters.
(B) String(char ary[]): Incorrect. This constructor creates a string using the characters stored in the given character array.
(C) String(string strObj): Incorrect. This constructor creates a string using another string object.
(D) String(string literal): Incorrect. This creates a string using a literal value.
Step 4: Conclusion.
Thus, the constructor used to create a string object with no characters is String().
Final Answer: String()
Was this answer helpful?
0
0