ProgrammingHardInfosys DSE · Wipro Elite
What is printed?
What is printed?
Integer a = 127, b = 127, c = 128, d = 128; System.out.print((a == b) + " " + (c == d) + " " + c.equals(d));
- A. true true true
- B. true false true
- C. false false true
- D. true false false
Correct answer: B. true false true
Autoboxing caches Integers from -128 to 127, so == is true only for 127; equals compares values.
More OOP with C++ and Java questions
- What happens when this runs? java Integer n = null; boolean flag = false; Integer r = flag ? 1 : n; System.out.print(r);
- What is printed? java byte b = 10; b += 120; System.out.print(b + " " + (b >>> 1));
- What is printed? java try { try { throw new Exception("1"); } finally { throw new RuntimeException("2"); } } catch (Exception e) {…
- What is printed? java class R implements AutoCloseable { String n; R(String n) { this.n = n; System.out.print("o" + n); } public void…
- What is printed? java class A {} class B extends A {} static void f(A a) { System.out.print("A"); } static void f(B b) {…
- What is printed? java class P { void f(Object o) { System.out.print("PO"); } } class C extends P { void f(String s) {…