ProgrammingHardProduct-company placement · C++
What is printed?
What is printed?
int main() {
vector<int> a(3, 5), b{3, 5};
cout << a.size() << a[0] << b.size() << b[0];
}- A. 3535
- B. 2323
- C. 3523
- D. 3553
Correct answer: C. 3523
Parentheses select the (count, value) constructor: three 5s. Braces prefer the initializer_list constructor: the two elements 3 and 5.
More OOP with C++ and Java questions
- What is printed? cpp void f(int a[10]) { cout << (sizeof(a) == sizeof(int)); } int main() { int arr[10]; cout << sizeof(arr) /…
- What is printed? cpp struct S { int next() { static int c = 0; return ++c; } }; int main() { S a, b; a.next(); b.next(); cout << a.next(); }
- What is printed? cpp int main() { try { throw "oops"; } catch (string& s) { cout << "S"; } catch (const char s) { cout << s; } }
- Which line fails to compile? cpp struct A { A(int) { } }; struct B { explicit B(int) { } }; A f() { return 5; } // L1 B g() { return 5; }…
- Which line fails to compile? cpp int main() { int a[3] = {1, 2, 3}, b[3]; b = a; // L1 array<int, 3> c{1, 2, 3}, d; d = c; // L2 }
- What is printed? cpp template <sizet N> sizet len(int (&)[N]) { return N; } int main() { int a[7]; cout << len(a); // L1 int p = a; cout…