ProgrammingModerateMicrosoft/Google SDE MCQ · GATE CS
What is printed?
What is printed?
int main() {
vector<int> a(3, 5);
vector<int> b{3, 5};
vector<int> c;
c.reserve(10);
cout << a.size() << b.size() << c.size();
}- A. 3210
- B. 320
- C. 220
- D. 3010
Correct answer: B. 320
(3, 5) means three elements equal to 5. Braces prefer the initializer_list constructor, giving the two elements 3 and 5. reserve only changes capacity, so c.size() stays 0.
More OOP with C++ and Java questions
- Which line fails to compile? cpp struct S { int v = 1; static int count; static int get() { return v; } // line 1 static int all() {…
- What is printed? cpp struct B { virtual void f() { cout << "B"; } void g() { f(); } }; struct D : B { void f() override { cout << "D"; }…
- Which statement about this class is correct? cpp struct Buf { int p; Buf() : p(new int[8]) { } ~Buf() { delete[] p; } }; void use() { Buf…
- What is printed? cpp struct A { int x; A(int v) : x(v) { } }; struct B : A { int y; B(int v) : A(v 2), y(x + 1) { } }; int main() { B…
- What is printed? cpp struct B { B() { cout << "b"; } ~B() { cout << "~b"; } }; struct D : B { D() { cout << "d"; } ~D() { cout << "~d"; }…
- What is printed? cpp struct X { int v; X(int a) : v(a) { } bool operator==(const X& o) const { return v == o.v; } }; int main() { X a(1)…