Java Programming - Operators and Assignments - Discussion

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";
    }
}

[A]. 9 7 7 foo 7 7foo
[B]. 72 34 34 foo34 34foo
[C]. 9 7 7 foo34 34foo
[D]. 72 7 34 foo34 7foo

Answer: Option D

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.


Arun Kumar said: (Jun 17, 2012)  
I didn't get why its giving different answers. Any one please explain me.

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

Ans : foo34 7 foo.

Anand H.R said: (Jun 26, 2012)  
Sytem.out.print(foo()+a+b+"");- in this first it will evaluated from left to right so function will return string , if any thing add with string it will be string only so the answer is foo34.


System.out.print(a+b+foo());- in this it will be evalueated from left to right and fist it will add a and b both are int type and intermediat result is 7 so then this 7 append with string foo.


hope now u got...

Arjun said: (Oct 19, 2012)  
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

Santhosh said: (Sep 5, 2013)  
+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.

Anish Kumar said: (Dec 4, 2014)  
foo() is again a method and it is not initiate. Why we should take as a statement or sentence. foo() is declared?

Jeng said: (Apr 8, 2016)  
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()+"").

Keshav Kumar said: (Nov 14, 2016)  
If either operand being evaluated is a String, the + operator will concatenate the two operands. Why?

Pushpendra said: (Apr 2, 2017)  
What will be the answer? If write it so;

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

Bittu said: (May 1, 2017)  
I am still not clear with,

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

Please, anyone help me.

Kush said: (Jul 20, 2017)  
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.

Ganesh said: (Jul 10, 2018)  
Thanks all for explaining.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.