Output of Java Programs | Set 53 (String Comparison)

Prerequisite : String Comparison in Java

1. What should be the output of this program?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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));
    }
}

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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));
    }
}

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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]);
    }
}

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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));
    }
}

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
    }
}

chevron_right


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.



My Personal Notes arrow_drop_up

Intern at GeeksforGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Saurav Bhasin