Open In App

Output of Java Programs | Set 53 (String Comparison)

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite : String Comparison in Java

1. What should be the output of this program?




class GeeksforGeeks {
    public static void main(String args[])
    {
        String GfG1 = "Welcome to GeeksforGeeks";
        boolean GfG2;
        GfG2 = GfG1.startsWith("hello");
        System.out.println(GfG2);
    }
}


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

Output:

b) false

Explanation: The startsWith() method is case sensitive “hello” and “Hello” are treated differently, hence false is stored in GfG2.

2. What should be the output of this program?




class GeeksforGeeks {
    public static void main(String args[])
    {
        String GfG1 = "I am intern at GeeksforGeeks";
        String GfG2 = new String(GfG1);
        System.out.println((GfG1 == GfG2) + " " + GfG1.equals(GfG2));
    }
}


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

Output:

d) false true

Explanation: 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.

3. What should be the output of this program?




class GeeksforGeeks {
    public static void main(String args[])
    {
        String GfG1 = "I am intern at GeeksforGeeks";
        String GfG2 = new String(GfG1);
        System.out.println((GfG1 == "I am intern at GeeksforGeeks") + " " + GfG1.equals(GfG2));
    }
}


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

Output:

a) true true

Explanation: The == operator compares two object references to see whether they refer to the same instance but when using == with a string literal(not an instantiated String variable) will only compare the content of the strings.

4. What should be the output of this program?




class GeeksforGeeks {
    public static void main(String args[])
    {
        String GfG[] = { "a", "b", "c", "a", "c" };
        for (int i = 0; i < GfG.length; ++i)
            for (int j = i + 1; j < GfG.length; ++j)
                if (GfG[i].compareTo(GfG[j]) == 0)
                    System.out.print(GfG[j]);
    }
}


a) ab
b) bc
c) ca
d) ac

d) ac

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.

5. What should be the output of this program?




class GeeksforGeeks {
    public static void main(String args[])
    {
        String GfG1 = "Hello";
        String GfG2 = new String(GfG1);
        String GfG3 = "HELLO";
        System.out.println(GfG1.equals(GfG2) + " " + GfG2.equals(GfG3));
    }
}


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

Output:

c) true false

Explanation: As we know that equal() method compares the content of the strings. GfG1 and GfG are having the same content. But as we know equal() is case sensitive, therefore GfG2 and GfG3 are different.

6. What should the output of this program?




class GeeksforGeeks {
    public static void main(String args[])
    {
        StringBuffer GfG1 = new StringBuffer("Hello");
        StringBuffer GfG2 = new StringBuffer(" World");
        GfG1.append(GfG2);
        System.out.println(GfG1);
    }
}


a) Hello
b) World
c) Helloworld
d) Hello World

Output:

d) Hello World

Explanation: append() method of class StringBuffer is used to concatenate the string representation to the end of invoking string.



Last Updated : 10 Sep, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads