Question:

If ‘str’ is the object of string class and its content is “Best-Luck”, then what is the value of str.length() in Java?

Show Hint

The {length()} method of the String class returns the total number of characters in a string, including spaces and special characters.
  • 8
  • 9
  • 10
  • 11
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is B

Solution and Explanation

Step 1: Understanding the String length() method.
In Java, the String class provides a method called length() that returns the number of characters present in a string. Every character in the string is counted, including letters, numbers, spaces, and special symbols.
Step 2: Analyzing the given string.
The string provided is:
"Best-Luck"
Now we count the characters:
B = 1
e = 2
s = 3
t = 4
- = 5
L = 6
u = 7
c = 8
k = 9
Step 3: Determining the result.
The total number of characters in the string is 9. Therefore, the method str.length() will return the value 9.
Step 4: Conclusion.
Thus, the length of the string "Best-Luck" is 9 characters.
Final Answer: 9
Was this answer helpful?
0
0