Menu
Topics Index
...
`

Strings1971


If you need explanation Read this topic

If you need Answer Take test on this topic

Strings1971
What will be the output of the following program?
public class Mystery {
    public static void main(String[] args) {
        char a = 'B';
        char b = 'A';
        char c = 'B';
        char d = 'A';
        String hear = "bad";
        String song = "good";
        String good = "hear";
        print(hear, song, a, b, c);
        print(good, hear, (char) (b + c), (char) (a + c), 'A');
        print(good, hear, (char) (a + b), a, c);
    }
    public static void print(String d, String e, char a, char b, char c) {
        System.out.println("-" + e + "-" + a + "-" + b + "-" + c + "-" + d);
    }
}

A. -good-B-A-B-bad
-bad-?-?-A-hear
-bad-A-B-B-hear
B. -good-B-A-B-bad
-bad-A-B-A-hear
-bad-A-B-B-hear
C. -good-B-A-B-bad
-bad-B-B-A-hear
-bad-A-B-B-hear
D. Some other output
E. Output can not be determined
F. Compilation Error or Runtime Error
Topic: Java Methods - Parameter Passing And Scope

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.

Answer is D
Output is:
-good-B-A-B-bad
-bad-?-?-A-hear
-bad-?-B-B-hear
the question mark will appear in output because after adding a+b , b+c as a characters they were again type casted into char type which leads to nothing this causes a ? at that position

Posted by Sai Avinash    2014-08-08 12:49:52


D ..the third print line goes as follows -bad-?-B-B-hear as char (a+b) n a here is ?

Posted by Shabaaz Shaikz    2014-08-08 12:50:02


Ans is D.
becoz while adding b+c = A+B=131 it prints ?.
so output will be
-good-B-A-B-bad
-bad-?-?-A-hear
-bad-?-B-B-hear

Posted by Gopinath Manchikanti    2014-08-08 13:06:51


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

Posted by Merit Campus    2014-08-11 04:34:15


© meritcampus 2019

All Rights Reserved.

Open In App