This is the java programming questions and answers section on "Java Strings" with an explanation for various interview, competitive examination and entrance test. Solved examples with detailed answer description, explanation are given and it would be easy to understand.
Java Strings Questions
Online Test Name | Java Strings |
Exam Type | Multiple Choice Questions |
Category | Computer Science Engineering Quiz |
Number of Questions | 26 |
11. Which of these data type value is returned by equals() method of String class?
- A. char
- B. int
- C. boolean
- D. All of the mentioned
- View Answer
- Workspace
- Report
- Discuss
Answer & Explanation
Answer: Option C
Explanation:
equals() method of string class returns boolean value true if both the string are equal and false if they are unequal.
Workspace
Report Error
12. Which of these method of class String is used to extract a substring from a String object?
- A. substring()
- B. Substring()
- C. SubString()
- D. None of the mentioned
- View Answer
- Workspace
- Report
- Discuss
Workspace
Report Error
13.
What will s2 contain after following lines of code?
String s1 = "one;
String s2 = s1.concat("two)
- A. one
- B. two
- C. onetwo
- D. twoone
- View Answer
- Workspace
- Report
- Discuss
Answer & Explanation
Answer: Option C
Explanation:
Two strings can be concatenated by using concat() method.
Workspace
Report Error
15. What is the value returned by function compareTo() if the invoking string is less than the string compared?
- A. zero
- B. value less than zero
- C. value greater than zero
- D. None of the mentioned
- View Answer
- Workspace
- Report
- Discuss
Answer & Explanation
Answer: Option B
Explanation:
compareTo() function returns zero when both the strings are equal, it returns a value less than zero if the invoking string is less than the other string being compared and value greater than zero when invoking string is greater than the string compared to.
Workspace
Report Error
Mock Tests & Online Quizzes
The Java String class compareTo() method compares the given string with the current string lexicographically. It returns a positive number, negative number, or 0.
It compares strings on the basis of the Unicode value of each character in the strings.
If the first string is lexicographically greater than the second string, it returns a positive number (difference of character value). If the first string is less than the second string lexicographically, it returns a negative number, and if the first string is lexicographically equal to the second string, it returns 0.
Syntax
The method accepts a parameter of type String that is to be compared with the current string.
It returns an integer value. It throws the following two exceptions:
ClassCastException: If this object cannot get compared with the specified object.
NullPointerException: If the specified object is null.
Internal implementation
Java String compareTo() Method Example
FileName: CompareToExample.java
Test it NowOutput:
Java String compareTo(): empty string
When we compare two strings in which either first or second string is empty, the method returns the length of the string. So, there may be two scenarios:
- If first string is an empty string, the method returns a negative
- If second string is an empty string, the method returns a positive number that is the length of the first string.
FileName: CompareToExample2.java
Output:
Java String compareTo(): case sensitive
To check whether the compareTo() method considers the case sensitiveness of characters or not, we will make the comparison between two strings that contain the same letters in the same sequence.
Suppose, a string having letters in uppercase, and the second string having the letters in lowercase. On comparing these two string, if the outcome is 0, then the compareTo() method does not consider the case sensitiveness of characters; otherwise, the method considers the case sensitiveness of characters.
FileName: CompareToExample3.java
Output:
Conclusion: It is obvious by looking at the output that the outcome is not equal to zero. Hence, the compareTo() method takes care of the case sensitiveness of characters.
Java String compareTo(): ClassCastException
The ClassCastException is thrown when objects of incompatible types get compared. In the following example, we are comparing an object of the ArrayList (al) with a string literal ("Sehwag").
FileName: CompareToExample4.java
Output:
Exception in thread "main" java.lang.ClassCastException: class Players cannot be cast to class java.lang.Comparable
Java String compareTo(): NullPointerException
The NullPointerException is thrown when a null object invokes the compareTo() method. Observe the following example.
FileName: CompareToExample5.java
Output:
Exception in thread "main" java.lang.NullPointerException at CompareToExample5.main(CompareToExample5.java:9)