Menu
Topics Index
...
`

Strings and if else


If you need explanation Read this topic

If you need Answer Take test on this topic

Strings and if else
What will be the output of the following program?
public class Compare {
    public static void main(String[] args) {
        String str1 = "Merit Campus";
        String str2 = "" + "Merit Campus";
        String str3 = new String("Merit Campus");
        System.out.println(str1 + ", " + str2 + ", " + str3);
        if (str1 == str2)
            System.out.println("Equal 1");
        else
            System.out.println("Not Equal 1");
        if (str1 == str3)
            System.out.println("Equal 2");
        else
            System.out.println("Not Equal 2");
        if (str1.equals(str3))
            System.out.println("Equal 3");
        else
            System.out.println("Not Equal 3");
    }
}

A. Merit Campus, Merit Campus, Merit Campus
Equal 1
Not Equal 2
Equal 3
B. Merit Campus, Merit Campus, Merit Campus
Equal 1
Equal 2
Equal 3
C. Merit Campus, Merit Campus, Merit Campus
Equal 1
Equal 2
Not Equal 3
D. Merit Campus, Merit Campus, Merit Campus
Equal 1
Not Equal 2
Not Equal 3
E. Some other output
F. Compilation Error or Runtime Error
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.

c

Posted by Maheshwari Natarajan    2014-07-04 08:36:35


F
Explanations: equal operator (=) is not valid in java..strings can only be campared using equal function.

Posted by Tarun Kumar    2014-07-04 08:38:02


F

Posted by Rishav Seksaria    2014-07-04 08:44:18


answ A ; str1=str2 this shows that the to the two variable indicate the same place which hold string " merit campus '. but when we compare them with str3 using '=' the cannot be equal for they don't indicate the same place in memo. but indicate  same string content in d/t memo area.then we can compare they using str1.equals(str3) or str1.equalsIgnoreCase(str3)!

Posted by Elias Dblo    2014-07-04 09:23:07


E , the first SOP prints as usually ,next the first if condition is false so it returns "Not Equal 1" , second if condition is true so it returns "Equal 2", and the third if conditon is true so it returns "Equal 3".

Posted by Damu Surya    2014-07-04 09:24:42


A

Posted by Abbii Tilku    2014-07-05 11:36:20


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

Posted by Merit Campus    2014-07-07 05:23:55


© meritcampus 2019

All Rights Reserved.

Open In App