Which of these data type value is returned by equals () method of string class?

Java MCQs on String comparision in Java Programming Language.

1. Which of these method of class String is used to compare two String objects for their equality?
a) equals()
b) Equals()
c) isequal()
d) Isequal()

Answer: a
Clarification: None.

2. Which of these methods is used to compare a specific region inside a string with another specific region in another string?
a) regionMatch()
b) match()
c) RegionMatches()
d) regionMatches()

Answer: d
Clarification: None.

3. Which of these methods of class String is used to check whether a given object starts with a particular string literal?
a) startsWith()
b) endsWith()
c) Starts()
d) ends()

Answer: a
Clarification: Method startsWith() of string class is used to check whether the String in question starts with a specified string. It is a specialized form of method regionMatches().

4. 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

Answer: b
Clarification: 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.

5. 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

Answer: c
Clarification: equals() method of string class returns boolean value true if both the string are equal and false if they are unequal.

6. What will be the output of the following Java code?

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         { 
  5.            String c = "Hello i love java";
  6.            boolean var;
  7.            var = c.startsWith("hello");
  8.            System.out.println(var);
  9.         }
  10.     }

a) true
b) false
c) 0
d) 1

Answer: b
Clarification: startsWith() method is case sensitive “hello” and “Hello” are treated differently, hence false is stored in var.
Output:

    {
0

7. What will be the output of the following Java code?

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         { 
  5.     {
    5
  6.     {
    6
  7.     {
    7
  8.         }
  9.     }

a) true true
b) false false
c) true false
d) false true

Answer: d
Clarification: The == operator compares two object references to see whether they refer to the same instance, where as equals() compares the content of the two objects.
Output:

        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() Method
  2. By Using == Operator
  3. By compareTo() Method

1) By Using equals() Method

The 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:

  • public boolean equals(Object another) compares this string to the specified object.
  • public boolean equalsIgnoreCase(String another) compares this string to another string, ignoring case.

Teststringcomparison1.java

Test it Now

Output:

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 Now

Output:

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() method

2) By Using == operator

The == operator compares references not values.

Teststringcomparison3.java

Test it Now

Output:


3) String compare by compareTo() method

The above code, demonstrates the use of == operator used for comparing two String objects.


3) By Using compareTo() method

The 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.