ProgrammingModerateTCS NQT · Infosys pattern
What is printed?
What is printed?
String a = "hi";
String b = "hi";
String c = new String("hi");
System.out.print((a == b) + " " + (a == c) + " " + a.equals(c));- A. true false false
- B. false false true
- C. true true true
- D. true false true
Correct answer: D. true false true
Equal string literals share one object from the string pool, so a == b is true. new String creates a separate object, so a == c compares different references (false). equals compares characters (true).
More OOP with C++ and Java questions
- What is printed? java System.out.print('a' + 1); System.out.print(" "); System.out.print("" + 'a' + 1);
- What is printed? cpp void f(int) { cout << "int "; } void f(double) { cout << "double "; } int main() { f('a'); f(2.5f); }
- What is printed? cpp struct A { A() { cout << "C"; } A(const A &) { cout << "K"; } ~A() { cout << "D"; } }; void f(A a) { } int main() { A…
- What is printed? cpp class A { public: static int count; A() { count++; } ~A() { count--; } }; int A::count = 0; int main() { A a, b; { A…
- Which line fails to compile? cpp struct Shape { virtual double area() = 0; }; struct Circle : Shape { }; struct Square : Shape { double…
- class D : private B { ... }; where B has public int x, protected int y and private int z. Which is correct?