Java Programming - Operators and Assignments - Discussion

Discussion Forum : Operators and Assignments - Finding the output (Q.No. 11)
11.
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";
    }
}
9 7 7 foo 7 7foo
72 34 34 foo34 34foo
9 7 7 foo34 34foo
72 7 34 foo34 7foo
Answer: Option
Explanation:

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.

Discussion:
12 comments Page 1 of 2.

Naveen said:   3 years ago
@Naveen_bobby_ak.

Step1: " "+7+2+" " it's String Type not int o/t: 72
Step2: (a+b)=3+4=7 it's int type so addition o/t: 7.
Step3: " "+a+b+" " It's String Type (left to right)
" "+3+4+" " o/t: 34,
Step4: foo()+a+b+" " foo() method is String Type
foo+3+4+" " o/t: foo34.
Step5: a+b+foo() a+b is a integer type so addition will be performed (left to right)
o/t: 7foo.

Finally o/t: 72 7 34 foo34 7foo.
(2)

Ganesh said:   7 years ago
Thanks all for explaining.

Kush said:   8 years ago
In java, we have only operator precedence but not operand precedence.before applying any operator all operand will be evaluated from left to right.

Here, System.out.print(foo() + a + b + " "); foo34.

because of foo() will be evaluated first which returns string "foo" so if string first comes then the remaing value will be converted to the string like foo34 and
here, System.out.println(a + b + foo());7foo.

Here primitive value added first, because operator precedence and last string will be converted like 7foo

The string always convertes while primitives always added.

Bittu said:   8 years ago
I am still not clear with,

System.out.print(foo() + a + b + " "); foo34
System.out.println(a + b + foo());7foo

Please, anyone help me.

Pushpendra said:   9 years ago
What will be the answer? If write it so;

s.o.p(foo()+3+4);

Keshav Kumar said:   9 years ago
If either operand being evaluated is a String, the + operator will concatenate the two operands. Why?

Jeng said:   10 years ago
If I am not mistaken it can never be right that (""+a+b+"") is 34.

This should be 7 with whitespaces, 34 should be like this (""+a.toString()+b.toString()+"").

Anish Kumar said:   1 decade ago
foo() is again a method and it is not initiate. Why we should take as a statement or sentence. foo() is declared?

Santhosh said:   1 decade ago
+7+2 = 72 in step one.

We have used + operator ,rest are a+b is sum.

Next, use two conditions then you get the answer.

Arjun said:   1 decade ago
Step1: s.o.p(""+7+2+""); this means adding with appending values 7+2=72 in this condition only.(Ans.72)
Step2: s.o.p(a+b) this means adding given values a=3,b=4 calculate a+b=7. (Ans.7)
Step3: s.o.p(""+a+b+""); this means adding with appending values 3+4=34 compare with step1.(Ans.34)
Step4: s.o.p(foo()+a+b+""); this means 1st applied to the foo() method. foo+3+4=foo34.(Ans.foo34).
Step5: s.o.p(a+b+foo()); this means adding to all values(only step5 values) ..3+4+foo=7foo(Ans.7foo)

Finally Answer : 72 7 34 foo34 7foo


Post your comments here:

Your comments will be displayed after verification.