# C++ Programming Questions and Answers – STL – Heap

«
»

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “STL – Heap”.

1. Which type of heap is implemented in STL heap?
a) max heap
b) min heap
c) middle heap
d) close heap

Explanation: C++ STL-heap implements max heap i.e. the front of heap contains the maximum of all the elements in a range.

2. Which function is used to construct heap from given sequence of numbers?
a) create_heap()
b) make_heap()
c) construct_heap()
d) start_heap()

Explanation: C++ STL-heap container provides make_heap() function to convert a given range of number into heap.

3. What is the use of front() function in heap?
a) Returns the element closest to the median of a sequence
b) Returns the last element of the heap
c) Returns the first element of the heap
d) Returns the element closest to mean of a sequence

Explanation: C++ STL-heap container provides the front() function that returns the first element of the heap i.e. the maximum number of the sequence.

4. Which function is used to insert an element into heap?
a) push_back()
b) push_heap()
c) pop_back()
d) pop_heap()

Explanation: C++ STL-heap container provides push_heap() function that inserts a new element to the constructed heap.

5. Elements in STL heap are removed in ________________________
a) decreasing order
b) increasing order
c) alternate i.e. once max element then min element
d) input order

Explanation: C++ STL-heap simulates the max heap i.e. the maximum element is at the top/front of the heap hence on poping we pop the first element which is always the maximum number in the sequence.

6. Which header file is required to use heap in your program?
a) <heap>
b) <algorithm>
c) <vector>
d) <map>

Explanation: <algorithm> header file is required to use the functionality of the heap container provided by C++.

7. Which of the following is correct syntax of making heap from a vector v?
a) make_heap(v.elements);
b) make_heap(v);
c) make_heap(v.end(), v.begin());
d) make_heap(v.begin(), v.end());

Explanation: To construct heap usng the vector elements one need to use the following syntax make_heap(v.begin(), v.end()); which is taking the iterator to first and last element of the vector using which elements of vector can be accessed and heap can be constructed.

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

```#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char const *argv[])
{
vector <int> v = {1,5,23,90,15,35};
make_heap(v.begin(),v.end());
cout<<v.front();
}```

a) 23
b) 1
c) 35
d) 90

Explanation: The heap C++ construct is max heap so when we are trying to print the front of heap the maximum element of the sequence will be printed as that will be at the top of heap.

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

```#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char const *argv[])
{
vector <int> v = {1,5,23,90,15,35};
make_heap(v.begin(),v.end());
v.push_back(110);
push_heap(v.begin(), v.end());
cout<<v.front();
}```

a) 90
b) 1
c) 110
d) 23

Explanation: In this program we are trying to construct heap using the given vector after which we are inserting 110 into the heap which is now the maximum element in the heap so the answer will be 110.

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

```#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char const *argv[])
{
vector <int> v = {1,5,23,90,15,35};
make_heap(v.begin(),v.end());
v.push_back(110);
push_heap(v.begin(), v.end());
pop_heap(v.begin(), v.end());
v.pop_back();
cout<<v.front();
}```

a) 90
b) 1
c) 110
d) 23

Explanation: In this program we are trying to construct heap using the given vector after which we are inserting 110 into the heap and then we are poping one element from the heap and as 110 is at the top of the heap so it will be popped out and we will have 90n at the top of heap so the answer will be 90.

11. What is the use of sort_heap() function in heap?
a) To sort the elements in the heap into descending order
b) To sort the elements in the heap into ascending order
c) To sort the first half of the heap
d) To sort the second half of the heap

Explanation: C++ STL-heap container provides sort_heap() function to sort the heap into ascending order which will no longer remain a heap.

12. Which function is used to check whether a given sequence is heap or not?
a) sort_heap()
b) is_heap()
c) is_heap_until()
d) check_heap()

Explanation: C++ STL-heap container provides is_heap() function to check whether a given sequence of elements represents a heap or not. Descending order of elements represents a valid heap.

13. What is the use of is_heap_until() function?
a) Returns the iterator of the last element of the sequence always
b) Returns the iterator to the position from where the sequence is a heap
c) Returns the iterator of the position till that the sequence is a heap
d) Returns the iterator of the first element of the sequence

Explanation: C++ STL-heap container provides is_heap_until() function which returns the iterator to the position till the container is a heap. For example, we have 7 5 3 1 10 12 so till 1 the sequence forms a heap so this function will return the iterator to the position of element 1.

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

```#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char const *argv[])
{
vector <int> v = {1,5,23,90,15,35};
cout<<is_heap(v.begin(), v.end());
make_heap(v.begin(), v.end());
cout<<is_heap(v.begin(), v.end());
}```

a) 00
b) 01
c) 10
d) 11

Explanation: Initially the sequence V is not a heap therefore the function returns 0 after make_heap() function the vector is converted into heap therefore the function returns 1 this time.

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

```#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char const *argv[])
{
vector <int> v = {90, 47, 34, 23, 4, 35, 67};
auto it = is_heap_until(v.begin(), v.end());
for(auto i = v.begin(); i != it; i++)
cout<<*i<<" ";
}```

a) 90 67 47 35 34 23 4
b) 90 47 34 23 4 35
c) 90 47 34 23 4 35 67
d) 90 47 34 23 4 