C++ Programming Questions and Answers – C Style Algorithms

This section on C++ interview questions and answers focuses on “C Style Algorithms”. 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 “C Style Algorithms” along with answers, explanations and/or solutions:

1. Pick out the in correct type of function in <algorithm> header file.
a) Partitions
b) Sort
c) Merge
d) Join
View Answer

Answer: d
Explanation: First three type of options are available in <algorithm> header file.

2. What type of algorithm is not available in creating our own STL style algorithms?
a) copy_if()
b) remove_copy_if()
c) sort
d) remove_copy()
View Answer

Answer: a
Explanation: copy_if() algorithm is not available in creating our own STL style algorithms.

3. What is meant by hash tables in C++?
a) Array data structure
b) Keyed array data structure
c) Data structure
d) Linear probing
View Answer

Answer: b
Explanation: In hash table, every value will have a key, So that it can be accessed easily.
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());
  11.         v.pop_back();
  12.         cout << v.front() << '\n';
  13.         return 0;
  14.     }

a) 10
b) 20
c) 30
d) 5
View Answer

Answer: b
Explanation: In this program, We are forming a heap with the vector and then we are popping one element and finding the maximum element in the heap.
Output:

$ g++ style.cpp
$ a.out
20

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 first[] = {5, 10, 15, 20, 25};
  8.         int second[] = {50, 40, 30, 20, 10};
  9.         vector<int> v(10);
  10.         vector<int> :: iterator it;
  11.         sort (first, first + 5);
  12.         sort (second, second + 5);
  13.         it = set_union (first, first + 5, second, second + 5, v.begin());  
  14.         v.resize(it-v.begin());
  15.         for (it = v.begin(); it != v.end(); ++it)
  16.             cout << ' ' << *it;
  17.         cout << '\n';
  18.         return 0;
  19.     }

a) 5 10 15
b) 20 25 30
c) 40 50
d) 20 25
View Answer

Answer: d
Explanation: In this kind of style algorithm, We are finding the elements in the both the vector by using set_union function.
Output:

advertisement
$ g++ style1.cpp
$ a.out
5 10 15 20 25 30 40 50

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.     bool myfunction (int i, int j)
  6.     { 
  7.         return (i < j);
  8.     }
  9.     struct myclass {
  10.     bool operator() (int i, int j)
  11.     {
  12.         return (i < j);
  13.     } 
  14.     } myobject;
  15.     int main () 
  16.     {
  17.         int myints[] = {10, 9, 8};
  18.         vector<int> myvector (myints, myints + 3);
  19.         sort (myvector.begin(), myvector.begin() + 2);
  20.         sort (myvector.begin() + 1, myvector.end(), myfunction);
  21.         sort (myvector.begin(), myvector.end(), myobject);
  22.         for (vector<int> :: iterator it = myvector.begin(); it != myvector.end(); ++it)
  23.             cout << ' ' << *it;
  24.         return 0;
  25.     }

a) 8 9 10
b) 10 8 9
c) 9 8 10
d) 10 8 8
View Answer

Answer: a
Explanation: In this style algorithm, We have sorted the elements in the vector by using the sort method.
Output:

$ g++ style2.cpp
$ a.out
8 9 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) 
  6.     {
  7.         return (i < j);
  8.     }
  9.     int main () 
  10.     {
  11.         int myints[] = {9, 8, 7, 6};
  12.         vector<int> myvector (myints, myints + 4);
  13.         partial_sort (myvector.begin(), myvector.begin() + 2, myvector.end());
  14.         partial_sort (myvector.begin(), myvector.begin() + 2, myvector.end(),
  15.         myfunction);
  16.         for (vector<int> :: iterator it = myvector.begin(); it != myvector.end(); ++it)
  17.             cout << ' ' << *it;
  18.         return 0;
  19.     }

a) 6 7 8 9
b) 9 8 6 7
c) 6 7 9 8
d) 9 8 5 7
View Answer

Answer: c
Explanation: In this program, We are partial sorting the vector by using the partial sort method.
Output:

$ g++ style3.cpp
$ a.out
6 7 9 8

8. 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 first[] = {5, 10, 15};
  8.         int second[] = {50, 40, 30};
  9.         vector<int> v(4);
  10.         vector<int> :: iterator it;
  11.         sort (first, first + 3);
  12.         sort (second, second + 3);
  13.         it = set_symmetric_difference (first, first + 2, second, second + 2, 
  14.         v.begin());
  15.         v.resize(it - v.begin());
  16.         for (it = v.begin(); it != v.end(); ++it)
  17.         cout << ' ' << *it;
  18.         return 0;
  19.     }

a) 5 10
b) 30 40
c) 50 40
d) 5 10 30 40
View Answer

Answer: d
Explanation: In this style algorithm, We are finding the symmetric difference between the vectors and printing it.
Output:

$ g++ style4.cpp
$ a.out
5 10 30 40

9. What is the use of includes function in c++?
a) Compares two ranges of data
b) Compares two sorted ranges of data
c) Includes a new element in the range
d) Includes a new element in the end
View Answer

Answer: b
Explanation: Returns true if the first sorted range contains all the elements in the second sorted range.

10. How many parameters are required for sort_heap function?
a) 1
b) 2
c) 2 or 3
d) 3
View Answer

Answer: c
Explanation: There are three parameters required for sort_heap. There are first element in heap and last element in heap and an optional compare.

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.