Java OCA OCP Practice Question 1237

Question

Consider the following application:

 1. public class Main { 
 2.   public static void main(String args[]) { 
 3.     Holder h = new Holder(); 
 4.     h.myInt = 100; //from   ww w.  j a v a2  s .  com
 5.     h.bump(h); 
 6.     System.out.println(h.myInt); 
 7.   } 
 8. } 
 9. 
10. class Holder { 
11.   public int myInt; 
12.   public void bump(Holder h) { 
13.     h.myInt++; 
14.   } 
15. } 

What value is printed out at line 6?

  • A. 0
  • B. 1
  • C. 100
  • D. 101


D.

Note

A holder is constructed on line 3.

A reference to that holder is passed into method bump() on line 5.

Within the method call, the holder's myInt variable is bumped from 100 to 101.




PreviousNext

Related