Java OCA OCP Practice Question 822

Question

Given the following declarations, identify which statements will return true:

Integer i1 = 1;  
Integer i2 = new Integer (1); 
int i3 = 1; 
Byte b1 = 1; 
Long g1 = 1L; 

Select 2 options

  • A. i1 == i2
  • B. i1 == i3
  • C. i1 == b1
  • D. i1.equals (i2)
  • E. i1.equals (g1)
  • F. i1.equals (b1)


Correct Options are  : B D

Note

For Option A.

This will return false because both are pointing to different objects.

For Option B.

This will return true because one operand is a primitive int, so the other will be unboxed and then the value will be compared.

For Option C. i1 == b1

This will not compile because type of i 1 and b 1 references are classes that are not in the same class hierarchy.

For Option D. i1.equals (i2)

This will return true because both are Integer objects and both have the value 1.

For Option E. i1.equals (g1)

This will return false because they are pointing to objects of different types.

Signature of equals method is : boolean equals(Object o);

For Option F. i1.equals (b1)

This will return false because they are pointing to objects of different types.




PreviousNext

Related