Java OCA OCP Practice Question 175

Question

Given:

3. public class Main { 
4.   int x = 3; /*from w w  w. ja v  a  2s  .c  o m*/
5.   public static void main(String[] args) { 
6.     new Main().go1(); 
7.   } 
8.   void go1() { 
9.     int x; 
10.     go2(++x); 
11.   } 
12.   void go2(int y) { 
13.     int x = ++y; 
14.     System.out.println(x); 
15.   } 
16. } 

What is the result?

  • A. 2
  • B. 3
  • C. 4
  • D. 5
  • E. Compilation fails
  • F. An exception is thrown at runtime


E is correct.

Note

In go1() the local variable x is not initialized.




PreviousNext

Related