ProgrammingModerateProduct-company placement · C++
What is printed?
What is printed?
int main() {
vector<int> v{1, 2, 3, 2, 4};
v.erase(remove(v.begin(), v.end(), 2), v.end());
cout << v.size() << ":";
for (int x : v) cout << x;
}- A. 3:134
- B. 5:13424
- C. 4:1324
- D. 3:143
Correct answer: A. 3:134
remove shifts the kept elements to the front (order preserved) and returns the new logical end; erase then cuts off the leftover tail. remove alone never changes the size.
More OOP with C++ and Java questions
- What is wrong with this loop? cpp vector<int> v{1, 2, 3}; for (int x : v) if (x == 1) v.pushback(1);
- What is printed? cpp void log(int) { cout << "G"; } struct S { void log() { cout << "M"; } void run() { log(5); } }; int main() { S s…
- What happens (C++14)? cpp struct S { static int n; }; int main() { S::n = 1; cout << S::n; }
- Which lines fail to compile? cpp void f(int) { cout << "I"; } void f(double) = delete; int main() { f(5); // L1 f('a'); // L2 f(2.5); //…
- Which lines fail to compile? cpp struct H { static H make() { return new H; } void destroy() { delete this; } private: ~H() { } }; int…
- What is printed? cpp struct B { virtual ~B() { } }; int main() { B p = nullptr; try { cout << typeid(p).name(); } catch (const badtypeid&)…