Java OCA OCP Practice Question 2939

Question

Given:

59.  Integer i1 = 2001;  // set 1  
60.  Integer i2 = 2001;  
61.  System.out.println((i1 == i2) + " " + i1.equals(i2));  // output 1  
62.  Integer i3 = 21;   // set 2  
63.  Integer i4 = new Integer(21);  
64.  System.out.println((i3 == i4) + " " + i3.equals(i4));  // output 2  
65.  Integer i5 = 21;   // set 3  
66.  Integer i6 = 21;  
67.  System.out.println((i5 == i6) + " " + i5.equals(i6));  // output 3 

What is the result? (Choose all that apply.)

  • A. Compilation fails.
  • B. An exception is thrown at runtime.
  • C. All three sets of output will be the same.
  • D. The last two sets of output will be the same.
  • E. The first two sets of output will be the same.
  • F. The first and last sets of output will be the same.


E is correct.

Note

For small values, wrappers created through boxing will be pooled, so i5 and i6 actually point to the same pooled instance of Integer.

In each of the first two sets, two meaningfully equivalent instances are created.




PreviousNext

Related