ProgrammingHardProduct-company placement · C++
What is printed?
What is printed?
int main() {
priority_queue<int> a;
priority_queue<int, vector<int>, greater<int>> b;
for (int x : {3, 1, 4}) { a.push(x); b.push(x); }
cout << a.top() << b.top();
}- A. 41
- B. 14
- C. 44
- D. 11
Correct answer: A. 41
priority_queue is a max-heap by default (top is the largest). Passing greater<int> as the comparator turns it into a min-heap, so its top is the smallest.
More OOP with C++ and Java questions
- What happens when this is compiled? cpp struct Pt { int x, y; }; int main() { map<Pt, int> m; m[{1, 2}] = 5; }
- What is printed? cpp int main() { vector<int> a(3, 5), b{3, 5}; cout << a.size() << a[0] << b.size() << b[0]; }
- 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; }…