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();
}
  1. A. 41
  2. B. 14
  3. C. 44
  4. 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

Exams that ask this topic