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
View Answer
Explanation: 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
View Answer
Explanation: 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 &<
View Answer
Explanation: 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?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main ()
{
int myints[] = {10, 20, 30, 5, 15};
vector<int> v(myints, myints + 5);
make_heap (v.begin(), v.end());
pop_heap (v.begin(), v.end()); v.pop_back();
v.push_back(99); push_heap (v.begin(), v.end());
sort_heap (v.begin(), v.end());
for (unsigned i = 0; i < v.size(); i++)
cout << ' ' << v[i];
return 0;
}
a) 5 10
b) 5 10 15 20
c) 5 10 15 20 99
d) 10 15 20 65
View Answer
Explanation: 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?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main ()
{
int myints[] = {1, 2, 3, 4 ,5};
vector<int> v(myints, myints + 5);
v.push_back(33);
push_heap (v.begin(),v.end());
cout << v.front() << '\n';
sort_heap (v.begin(),v.end());
return 0;
}
a) 1
b) 33
c) 3
d) 44
View Answer
Explanation: 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?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main ()
{
int myints[] = {2, 4, 6, 8, 10};
vector<int> v(myints, myints + 5);
make_heap (v.begin(),v.end());
cout << v.front() << '\n';
return 0;
}
a) 10
b) 20
c) 4
d) 8
View Answer
Explanation: 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?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool myfunction (int i,int j) { return (i<j); }
int main ()
{
int myints[] = {9, 8, 7, 6, 5};
vector<int> myvector (myints, myints + 5);
partial_sort (myvector.begin(), myvector.begin() + 3, myvector.end());
partial_sort (myvector.begin(), myvector.begin() + 2, myvector.end(),
myfunction);
for (vector<int> :: iterator it = myvector.begin(); it != myvector.end(); ++it)
cout << ' ' << *it;
return 0;
}
a) 5 6 7
b) 5 6 7 9 8
c) 9 8 7 6 5
d) 8 5 6 7 4
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: There are two types of heaps. They are min and max heap.
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]
- Check C++ Books
- Practice Programming MCQs
- Practice Computer Science MCQs
- Apply for C++ Internship
- Check Programming Books