Java OCA OCP Practice Question 435

Question

What is the output of the following application?


public class MyClass { 
        public static void main(String[] in) { 
           int tiger = 2; 
           short lion = 3; 
           long winner = lion+2*(tiger + lion); 
           System.out.print(winner); 
        } /*from  ww  w .  ja v a 2s .c o m*/
     ? 
} 
  • A. 11
  • B. 13
  • C. 25
  • D. None of the above


B.

Note

For this problem, it helps to recognize that parentheses take precedence over the operations outside the parentheses.

Once we replace the variables with values, the expression becomes: 3+2*(2+3).

We then calculate the value inside the parentheses to get 3+2*5.

Since the multiplication operator has higher precedence than addition, we evaluate it first, resulting in 3+10 = 13, making Option B the correct answer.




PreviousNext

Related