Menu
Topics Index
...
`

String comparison


If you need explanation Read this topic

If you need Answer Take test on this topic

String comparison
What will the output of the following program?
class StringTest {

    public static void main(String[] args) {
        String a = "A";
        String b = "B";
        String ab = a + b;
        String a1 = new String("A");
        String a2 = new String(a);
        System.out.print(ab.equals(a + b) ? ":" : ";");
        System.out.print(ab == a + b ? "(" : "-");
        System.out.print(a.equals(a1) ? ")" : "D");
        System.out.print(a == a1 ? ":" : ";");
        System.out.print(a2 == a1 ? "-" : "!");
        System.out.print(a == b ? "P" : "D");
    }
}

A. :();!D
B. :();-D
C. :-);-D
D. :-);!D
E. None of the above
Topic: Java equals method vs == Operator

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans:A equals() and "=="  both checks the content in string.

Posted by Indrasena Reddy    2014-02-18 03:37:21


Ans id D: == n equals not same so it wl giv false so :-)d;!D

Posted by Uday Kumar    2014-02-18 03:42:04


Ans  D.. because in string "==" for reference comparison.and "equals method" for data comparison. first statement : printed both content is same..2nd statement refences are diff so - will be printed..3rd statement the data will be same so ) printed.4th statement  both are diff refferences(memory locations) so ; printed...llerlly like that  our output will be           :-);!D

Posted by Sapparapu Pradeep Kumar    2014-02-18 09:48:20


Congratulations Uday Kumar. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-02-19 03:57:33


© meritcampus 2019

All Rights Reserved.

Open In App