ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
public class Main {
public static void main(String[] args) {
Integer a = 127;
int b = 127;
System.out.print(a == b);
}
}- A. It depends on the IntegerCache range
- B. Compilation error: cannot compare Integer and int with ==
- C. false
- D. true
Correct answer: D. true
When one operand is a primitive int, == triggers auto-unboxing on the Integer, so this becomes a plain value comparison (127 == 127), always true — the IntegerCache identity rules only matter when BOTH sides are reference types (Integer == Integer).
More OOP with C++ and Java questions
- What is printed? java public class Main { public static void main(String[] args) { String s1 = "java"; String s2 = "java".intern(); String…
- What is printed? java public class Main { public static void main(String[] args) { String s = "hello"; String t = s; t = t.concat("…
- What is printed? java public class Main { public static void main(String[] args) { Object o1 = new Object(); Object o2 = new Object()…
- What is printed? java public class Main { public static void main(String[] args) { List<String> list = new ArrayList<>(Arrays.asList("a"…
- What is printed? java class Key { final String name; Key(String n) { name = n; } @Override public boolean equals(Object o) { if (this ==…
- What is printed? java public class Main { public static void main(String[] args) { String a = "Hello"; String b = "hello"…