ProgrammingModerateAmazon/Microsoft SDE MCQ · Java placement
What is printed?
What is printed?
String s = "java";
s.toUpperCase();
s.concat("x");
StringBuilder sb = new StringBuilder("java");
sb.reverse();
sb.append("x");
System.out.print(s + " " + sb);- A. javax avajx
- B. java avajx
- C. java javax
- D. javax javax
Correct answer: B. java avajx
String methods return new strings and the results here are discarded, so s stays java. StringBuilder methods mutate the builder in place: reverse gives avaj and append adds x.
More OOP with C++ and Java questions
- What is printed? java interface I { default String n() { return "I"; } } class P { public String n() { return "P"; } } class C extends P…
- What is printed (C++17)? cpp struct T { T() { } T(const T&) { cout << "C"; } T(T&&) { cout << "M"; } }; int main() { const T a; T b =…
- What is printed? cpp struct T { T() { } T(const T&) { cout << "C"; } }; struct W { T t; }; int main() { W a; W b = std::move(a); }
- What is printed? cpp struct A { virtual void f() = 0; }; void A::f() { cout << "A"; } struct B : A { void f() override { A::f(); cout <<…
- What is printed? cpp struct B { virtual ~B() { } }; struct D1 : B { }; struct D2 : B { }; int main() { B p = new D1; cout <<…
- What is printed? cpp struct B { }; // no virtual functions struct D : B { }; struct VB { virtual ~VB() { } }; struct VD : VB { }; int…