ProgrammingHardCognizant GenC · HCL
What is printed?
What is printed?
Integer a = 127, b = 127, c = 128, d = 128; System.out.print((a == b) + " " + (c == d));
- A. true true
- B. false false
- C. false true
- D. true false
Correct answer: D. true false
Autoboxing caches Integer objects from -128 to 127, so 127 shares one object while 128 creates two separate ones.
More OOP with C++ and Java questions
- What does this method return? java static int f() { int x = 1; try { return x; } finally { x = 2; } }
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct B { B() { f(); } virtual void f() { cout << "B"; } }; struct…
- A Java method has this structure. What happens when it is compiled? java try { reader.read(); // throws IOException } catch (Exception e)…
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct B { virtual void f() = 0; }; void B::f() { cout << "B"; }…
- What is printed? java String s = "a"; s.concat("b"); s += "c"; System.out.print(s);
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct B { void f(int) { cout << "Bi"; } }; struct D : B { void…