ProgrammingModerateTCS NQT · Infosys
What is printed?
What is printed?
String s1 = "hi";
String s2 = new String("hi");
System.out.print((s1 == s2) + " " + s1.equals(s2));- A. false true
- B. true true
- C. false false
- D. true false
Correct answer: A. false true
new String creates a separate object, so == (reference comparison) is false; equals compares contents and is true.
More OOP with C++ and Java questions
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct C { static int n; C() { n++; } ~C() { n--; } }; int C::n = 0…
- Which statement about abstract classes in Java is correct?
- In C++, a Base pointer holds a Derived object created with new, and the Base destructor is NOT virtual. What happens on delete basePtr?
- What is printed? java static void f(int x) { System.out.print("int"); } static void f(long x) { System.out.print("long"); } static void…
- What happens when this C++ program is compiled? cpp struct B { int x = 5; }; class D : B { }; int main() { D d; cout << d.x; }
- What is printed? java static int f() { try { return 1; } finally { System.out.print("F"); } } public static void main(String[] a) {…