Java OCA OCP Practice Question 1049

Question

What is the output of the following program?

public class Main {
   static int i = 1;
   static {//from w w  w.  j  a  v a 2  s .c o m
      ++i;
   }

   public static void main(String[] args) {
      increment(i, 5);
      display(i);
   }

   static void increment(int n, int m) {
      n += m;
   }

   static void display(int n) {
      System.out.print(n);
   }

   static {
      ++i;
   }
} 
  • A. 1
  • B. 3
  • C. 6
  • D. 7


B.

Note

Both static initializers are executed before main() is executed.

The increment() method has no effect on the value of i.




PreviousNext

Related