C++ Programming MCQ – Heap

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Heap”. 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
View Answer

Answer: c
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

Answer: b
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

Answer: d
Explanation: The elements in the heap are compared using operator< (for the first version), or comp (for the second version).
advertisement
advertisement

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

  1.     #include <iostream>
  2.     #include <algorithm>
  3.     #include <vector>
  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
View Answer

Answer: c
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?

advertisement
  1.     #include <iostream>
  2.     #include <algorithm>
  3.     #include <vector>
  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
View Answer

Answer: b
Explanation: In this program, We are pushing a new value into heap and printing it.
Output:

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

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

  1.     #include <iostream>
  2.     #include <algorithm>
  3.     #include <vector>
  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
View Answer

Answer: a
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?

  1.     #include <iostream>
  2.     #include <algorithm>
  3.     #include <vector>
  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
View Answer

Answer: b
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

Answer: d
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

Answer: d
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

Answer: b
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]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.