Menu
Topics Index
...
`

String and object comparison


If you need explanation Read this topic

If you need Answer Take test on this topic

String and object comparison
What will be the output of the following program?
public class Merit {

    public static void main(String[] args) {
        String str = "cba";
        Object obj = str;
        if (str.equals(obj)) {
            System.out.print("AAA");
        } else {
            System.out.print("BBB");
        }
        if (obj.equals(str)) {
            System.out.print("CCC");
        } else {
            System.out.print("DDD");
        }
    }
}

A. AAACCC
B. AAADDD
C. BBBCCC
D. BBBDDD
E. Compilation Error or Runtime Error
Topic: Object As A Super Class In Java

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.

option a is the answer by using function in if else blocks

Posted by Karteek Paruchuri    2013-04-24 12:36:56


ans is option a: obj and str have same values so both if conditions are satisfied

Posted by Sarika Vollala    2013-04-24 12:37:28


Congratulations Sarika Vollala, you are today's daily dose winner. Please send us your mobile details and we will process the recharge tomorrow after considering the new likes.

Posted by Merit Campus    2013-04-24 15:36:08


thanku MC received RC

Posted by Sarika Vollala    2013-04-26 15:38:31


© meritcampus 2019

All Rights Reserved.

Open In App