Which of these data type value is returned by equals () method of string class?
Java MCQs on String comparision in Java Programming Language. Show
1. Which of these method of class String is used to compare two String objects for their equality? Answer: a 2. Which of these methods is used to compare a specific region inside a string with another specific region in another string? Answer: d 3. Which of these methods of class String is used to check whether a given object starts with a particular string literal? Answer: a 4. What is the value returned by function compareTo() if the invoking string is less than the string compared? Answer: b 5. Which of these data type value is returned by equals() method of String class? Answer: c 6. What will be the output of the following Java code?
a) true Answer: b { 07. What will be the output of the following Java code?
a) true true Answer: d public static void main(String args[])0 It is used in authentication (by equals() method), sorting (by compareTo() method), reference matching (by == operator) etc. There are three ways to compare String in Java:
1) By Using equals() MethodThe String class equals() method compares the original content of the string. It compares values of string for equality. String class provides the following two methods:
Teststringcomparison1.java Test it NowOutput: In the above code, two strings are compared using equals() method of String class. And the result is printed as boolean values, true or false. Teststringcomparison2.java Test it NowOutput: In the above program, the methods of String class are used. The equals() method returns true if String objects are matching and both strings are of same case. equalsIgnoreCase() returns true regardless of cases of strings. Click here for more about equals() method2) By Using == operatorThe == operator compares references not values. Teststringcomparison3.java Test it NowOutput: 3) String compare by compareTo() methodThe above code, demonstrates the use of == operator used for comparing two String objects. 3) By Using compareTo() methodThe String class compareTo() method compares values lexicographically and returns an integer value that describes if first string is less than, equal to or greater than second string. Which of these datatype value is returned by equals () method of string class?The equals() method compares two strings, and returns true if the strings are equal, and false if not.
What is the return type of string compareTo () method?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.
Which value will be returned if invoking string is equal to Str?Technical Details. Returns: An int value: 0 if the string is equal to the other string.
What is the value returned by function compareTo ()?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.
|