ProgrammingVery hardProduct-company placement · GATE CS
What is printed?
What is printed?
Long a = 1L; System.out.print(a.equals(1) + " " + a.equals(1L) + " " + (a == 1));
- A. true true true
- B. false true false
- C. false true true
- D. true true false
Correct answer: C. false true true
a.equals(1) boxes 1 to an Integer, and Long.equals returns false for anything that is not a Long. a == 1 compares a boxed value with a primitive, so a is unboxed and the numeric comparison is true.
More OOP with C++ and Java questions
- What is printed? cpp struct B { B() { f(); } virtual ~B() { f(); } virtual void f() { cout << "B"; } }; struct D : B { D() { f(); } ~D() {…
- What is printed? cpp struct M { int id; M(int i) : id(i) { cout << "M" << id; } ~M() { cout << "m" << id; } }; struct B { B() { cout <<…
- What is printed? cpp struct C { static int n; C() { ++n; } C(const C&) { ++n; } ~C() { --n; } }; int C::n = 0; void f(C c) { cout << C::n…
- What is printed (C++17)? cpp int a[] = {1, 2, 3, 4}; int p = a + 1; cout << p++; cout << ++p; cout << p[-1];
- What is printed? cpp struct B { void f(int) { cout << "Bi"; } void f(const char) { cout << "Bs"; } }; struct D1 : B { void f(double) {…
- Which lines fail to compile? cpp struct B { void f() { } }; class D1 : B { }; struct D2 : B { }; class D3 : public B { }; int main() { D1…