Operators and Assignments

Q1: What will be the output of the program?
class SC2 
{
    public static void main(String [] args) 
    {
        SC2 s = new SC2();
        s.start();
    }

    void start() 
    {
        int a = 3;
        int b = 4;
        System.out.print(' ' + 7 + 2 + ' ');
        System.out.print(a + b);
        System.out.print(' ' + a + b + ' ');
        System.out.print(foo() + a + b + ' ');
        System.out.println(a + b + foo());
    }

    String foo() 
    {
        return 'foo';
    }
}

A 72 34 34 foo34 34foo

B 9 7 7 foo 7 7foo

C 72 7 34 foo34 7foo

D 9 7 7 foo34 34foo

ANS:A - 9 7 7 foo 7 7foo

Because all of these expressions use the + operator, there is no precedence to worry about and all of the expressions will be evaluated from left to right. If either operand being evaluated is a String, the + operator will concatenate the two operands; if both operands are numeric, the + operator will add the two operands.



img not found
img

For help Students Orientation
Mcqs Questions

One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.