ProgrammingVery hardTCS NQT · Infosys pattern
What is printed?
What is printed?
// in main: Integer a = 100; Integer b = 100; Integer c = 200; Integer d = 200; System.out.println((a == b) + " " + (c == d));
- A. true true
- B. true false
- C. false true
- D. false false
Correct answer: B. true false
Autoboxing uses Integer.valueOf, which caches instances for values -128 to 127. 100 falls in that range, so a and b are the same cached object (==true). 200 is outside the cache range, so c and d are distinct boxed objects (==false), even though their .equals() would agree.
More OOP with C++ and Java questions
- What is printed? java // in main: String s1 = "Java"; String s2 = new StringBuilder("Java").toString(); System.out.println((s1 == s2) + "…
- What is printed? java // in main: String s = "hello"; s.concat(" world"); System.out.println(s); String t = s.concat(" world")…
- What is printed? java class Node { int val; Node(int v) { val = v; } @Override public int hashCode() { return 42; } @Override public…
- What is printed? java // in main: String a = "Hello".toLowerCase(); String b = "hello"; System.out.println((a == b) + " " + a.equals(b));
- Which overridden equals() below is actually BROKEN with respect to the symmetry requirement of the equals contract? java class A { int x…
- What is printed? java class Version { int major, minor; Version(int ma, int mi) { major = ma; minor = mi; } @Override public boolean…