250+ TOP MCQs on Heaps and Answers

This section on C++ interview questions and answers on “Heaps”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ interview questions on “Heaps” along with answers, explanations and/or solutions:

1. What is meant by heap?
a) Used for fast retrieval of elements
b) Used for organising the elements
c) Used for fast retrieval & organising the elements
d) Used for deleting the elements
Answer: c
Clarification: A heap is a way to organize the elements of a range that allows for fast retrieval of the element.

2. Which value is pointed out first in heap?
a) Lowest value
b) Highest value
c) First value
d) Third value
Answer: b
Clarification: The element with the highest value is always pointed by first.

3. Which operator is used to compare the elements in heap?
a) >>
b) comp
c) <
d) Both comp &<
Answer: d
Clarification: The elements in the heap are compared using operator< (for the first version), or comp (for the second version).

4. What will be the output of the following C++ code?

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     int main () 
  6.     {
  7.         int myints[] = {10, 20, 30, 5, 15};
  8.         vector<int> v(myints, myints + 5);
  9.         make_heap (v.begin(), v.end());
  10.         pop_heap (v.begin(), v.end()); v.pop_back();
  11.         v.push_back(99); push_heap (v.begin(), v.end());
  12.         sort_heap (v.begin(), v.end());
  13.         for (unsigned i = 0; i < v.size(); i++)
  14.             cout << ' ' << v[i];
  15.         return 0;
  16.     }

a) 5 10
b) 5 10 15 20
c) 5 10 15 20 99
d) 10 15 20 65
Answer: c
Clarification: In this program, We popped out 30 and pushed 99 and then we are sorting that value, So it is printing it.
Output:

$ g++ heap.cpp
$ a.out
5 10 15 20 99

5. What will be the output of the following C++ code?

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     int main () 
  6.     {
  7.         int myints[] = {1, 2, 3, 4 ,5};
  8.         vector<int> v(myints, myints + 5);
  9.         v.push_back(33); 
  10.         push_heap (v.begin(),v.end());
  11.         cout << v.front() << 'n';
  12.         sort_heap (v.begin(),v.end());
  13.         return 0;
  14.     }

a) 1
b) 33
c) 3
d) 44
Answer: b
Clarification: In this program, We are pushing a new value into heap and printing it.
Output:

$ g++ heap1.cpp
$ a.out
33

6. What will be the output of the following C++ code?

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std; 
  5.     int main ()
  6.     {
  7.         int myints[] = {2, 4, 6, 8, 10};
  8.         vector<int> v(myints, myints + 5);
  9.         make_heap (v.begin(),v.end());
  10.         cout  << v.front() << 'n'; 
  11.         return 0;
  12.     }

a) 10
b) 20
c) 4
d) 8
Answer: a
Clarification: In this program, We are printing the maximum value in the heap.
Output:

$ g++ heap2.cpp
$ a.out
10

7. What will be the output of the following C++ code?

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     bool myfunction (int i,int j) { return (i<j); }
  6.     int main () 
  7.     {
  8.         int myints[] = {9, 8, 7, 6, 5};
  9.         vector<int> myvector (myints, myints + 5);
  10.         partial_sort (myvector.begin(), myvector.begin() + 3, myvector.end());
  11.         partial_sort (myvector.begin(), myvector.begin() + 2, myvector.end(), 
  12.         myfunction);
  13.         for (vector<int> :: iterator it = myvector.begin(); it != myvector.end(); ++it)
  14.             cout << ' ' << *it;
  15.         return 0;
  16.     }

a) 5 6 7
b) 5 6 7 9 8
c) 9 8 7 6 5
d) 8 5 6 7 4
Answer: b
Clarification: In this program, We are partitioning the value by using the partial_sort method.
Output:

$ g++ heap3.cpp
$ a.out
5 6 7 9 8

8. How to protect the heap from affecting the memory?
a) Avoid using pointers for associating two data structures
b) Embed pointed child objects into the parent object
c) Allocate objects in chunks
d) All of the mentioned
Answer: d
Clarification: Steps to protect the heap from affecting the memory.
-> Avoid using pointers for associating two data structures
-> Embed pointed child objects into the parent object
-> Allocate objects in chunks.

9. In what form does the STL provides heap?
a) queue
b) list
c) vector
d) priority_queue
Answer: d
Clarification: STL does provide a heap in the form of a std::priority_queue.

10. How many types are there in binary heaps?
a) 1
b) 2
c) 3
d) 4
Answer: b
Clarification: There are two types of heaps. They are min and max heap.

  250+ TOP MCQs on Stored Sequences and Answers

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top