Multiple choice

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

  1. 9 7 7 foo 7 7foo

  2. 72 34 34 foo34 34foo

  3. 9 7 7 foo34 34foo

  4. 72 7 34 foo34 7foo

Reveal answer Fill a bubble to check yourself
D Correct answer
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.