Sail E0 Webinar
Question


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

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.



Was this answer helpful ?
Next Question

Submit Solution

Your email address will not be published. Required fields are marked *

Latest Videos

Latest Test Papers