Java OCA OCP Practice Question 183

Question

Given:

class Main { //w  w w . ja v  a2 s  .  c  o m
  public static void main(String[] args) { 
    long x = 42L; 
    long y = 44L; 
    System.out.print(" " + 7 + 2 + " "); 
    System.out.print(bar() + x + 5 + " "); 
    System.out.println(x + y + bar()); 
  } 
  static String bar() { return "bar"; } 
} 

What is the result?

  • A. 9 bar47 86bar
  • B. 9 bar47 4244bar
  • C. 9 bar425 86bar
  • D. 9 bar425 4244bar
  • E. 72 bar47 86bar
  • F. 72 bar47 4244bar
  • G. 72 bar425 86bar
  • H. 72 bar425 4244bar
  • I. Compilation fails


G is correct.

Note

Concatenation runs from left to right.

If either operand is a String, the operands are concatenated.

If both operands are numbers, they are added together.




PreviousNext

Related