Menu
Topics Index
...
`

Strings and StringBuffer


If you need explanation Read this topic

If you need Answer Take test on this topic

Strings and StringBuffer
What will be the output of the following program?
public class StringTest {
    public static void main(String arg[]) {
        String input = "123MeRiTCaMpUs456";
        int a = 6;
        int b = 3;
        int c = a & b;
        System.out.println(c);
        StringBuffer sb = new StringBuffer(input);
        if (input.equalsIgnoreCase("MeR") || input.equalsIgnoreCase(sb)) {
            System.out.print(input.toUpperCase());
        } else {
            System.out.print(input.toLowerCase());
        }
    }
}

A. 2
123meritcampus456
B. 3
123MeRitcampus456
C. 2
123MeRITCAMPUS56
D. Some other output
E. Compilation Error or Runtime Error
Topic: Java StringBuffer

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.

E.compilation error bcz...lang.string cannot be applied to lang.stringbuffer ie; input.ignorecase

Posted by Shabaaz Shaikz    2014-05-06 04:04:16


ans : d
some other output
c=6&3; is 2
and input to upper case is
123MERITCAMPUS456
op:
123MERITCAMPUS456

Posted by Ananda Kumar    2014-05-06 04:04:19


Ans is E as actual argument StringBuffer cannot be converted to String by method invocation conversion

Posted by Bharath Yelchuri    2014-05-06 07:44:07


Answer is E. equalsIgnoreCase() compares the invoking string object with the string object passed in as parameter for equality i.e., it compares two "string" objects. Here the argument is of type StringBuffer (LINE 9). Hence compiler throws an error. If the second expression in the "if" condition was input.equalsIgnoreCase(sb.toString()), then the answer would be:
2
123MERITCAMPUS456
(i.e., we have converted StringBuffer to String, which will fix the error)

Posted by Shaileshwar Sharma    2014-05-07 09:08:53


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

Posted by Merit Campus    2014-05-08 04:06:13


tnkx..

Posted by Shabaaz Shaikz    2014-05-08 04:26:46


© meritcampus 2019

All Rights Reserved.

Open In App