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)

Arun kumar said:   1 decade ago
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:   1 decade ago
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:   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

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.

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?

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()+"").

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

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

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

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.


Post your comments here:

Your comments will be displayed after verification.