ProgrammingVery hardGATE CS · TCS Digital · Advanced
What is printed?
What is printed?
Long l = 127L;
Integer i = 127;
System.out.print(l.equals(i) + " " + l.equals(127L) + " "
+ (l == 127) + " " + l.equals(127));- A. false false true false
- B. true true true true
- C. false true true false
- D. false true true true
Correct answer: C. false true true false
Long.equals needs a Long argument, so the Integer i and the boxed int literal 127 both give false; 127L boxes to Long (true); l == 127 unboxes l and compares numerically (true).
More OOP with C++ and Java questions
- What is printed? java static void f(long x) { System.out.print("L"); } static void f(Integer x) { System.out.print("I"); } static void…
- What is printed? java System.out.print(true ? 1 : 2.0); System.out.print(true ? 'a' : 0); int i = 0; System.out.print(true ? 'a' : i);
- What is printed? java static int f() { try { throw new RuntimeException(); } finally { return 1; } } static int g() { int x = 1; try {…
- What is printed? java class K { int v; K(int v) { this.v = v; } public int hashCode() { return v; } public boolean equals(Object o) {…
- What is printed? java Number[] n = new Integer[2]; n[0] = 1; try { n[1] = 2.5; } catch (ArrayStoreException e) { System.out.print("ASE ")…
- What is printed? java String a = "ab"; String b = "a"; String c = b + "b"; final String d = "a"; String e = d + "b"; System.out.print((a…