ExamHoot

Given:

Integer i1 = 127;
Integer i2 = 127;
Integer i3 = 128;
Integer i4 = 128;

Which comparison yields `true`?

  1. i1 == i2

  2. i3 == i4

  3. i1 == i3

  4. i2.equals(i4)

  5. i1.equals(i2)

Show answer & explanation

Correct answer

i1 == i2

Explanation

Java caches `Integer` objects for values between -128 and 127 inclusive. Thus `i1 == i2` refers to the same cached object, so `true`. For 128, new objects are created, so `i3 == i4` is `false`. `.equals()` compares values and is `true` for both.

Written by ExamHoot EditorialPublished · Updated

All 10 Java-variables and data types questions

  1. 1.Which of the following declarations correctly initializes a `char` variable with the Unicode value corresponding to the Greek letter omega…
  2. 2.Consider the following code:
  3. 3.Which of the following assignments is guaranteed to compile without loss of precision?
  4. 4.Given:
  5. 5.What is the output of the following code?
  6. 6.Which of the following is a valid declaration for a multi-dimensional array of `double` with 3 rows and 4 columns, where all elements are…
  7. 7.Consider the following:
  8. 8.What is the range of values that can be stored in a `byte` data type in Java?
  9. 9.Which of the following statements about the `final` keyword with primitive variables is true?
  10. 10.Given the following code, what is the value of `result`?