C++ Programming Questions and Answers – Modifying Sequence Algorithms

This section on C++ language interview questions and answers focuses on “Modifying Sequence 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++ language interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

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

1. What is the use of middle parameter in the rotate method?
a) Marks the begining of a sequence
b) Marks the ending of a sequence
c) Marks the elements in a sequence
d) Marks the digits in a sequence
View Answer

Answer: c
Explanation: Forward iterator pointing to the element within the range and that can be moved to the first position in the range.

2. What kind of object is modifying sequence algorithm?
a) Function template
b) Class template
c) Method
d) Iterator
View Answer

Answer: a
Explanation: It is a group of functions and implemented under algorithm header file.

3. How the sequence of objects can be accessed?
a) Iterators
b) Pointers
c) Both Iterators & Pointers
d) Library
View Answer

Answer: c
Explanation: A range is any sequence of objects that can be accessed through iterators or pointers, such as an array or an instance of some of the STL containers.
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.         vector<int> myvector (5);
  8.         fill (myvector.begin(), myvector.begin() + 4, 5);
  9.         fill (myvector.begin() + 3,myvector.end() - 2, 8);
  10.         for (vector<int> :: iterator it = myvector.begin();
  11.             it != myvector.end(); ++it)
  12.         cout << ' ' << *it;
  13.         return 0;
  14.     }

a) 5 5 5 5 0
b) 8 8 8 8 0
c) 5 8 5 8 0
d) 5 5 5 5 5
View Answer

Answer: a
Explanation: In this program, We filled up all the vector values by using fill method.
Output:

Note: Join free Sanfoundry classes at Telegram or Youtube
$ g++ msa.cpp
$ a.out
5 5 5 5 0

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[]={ 10, 20, 30, 40, 50 };
  8.         vector<int> myvector (4, 99);
  9.         iter_swap(myints, myvector.begin());
  10.         iter_swap(myints + 3,myvector.begin() + 2);
  11.         for (vector<int> :: iterator it = myvector.begin(); 
  12.             it != myvector.end(); ++it)
  13.         cout << ' ' << *it;
  14.         return 0;
  15.     }

a) 10
b) 10 40
c) 10 99 40 99
d) 99 40 10
View Answer

Answer: c
Explanation: In this program, We are swapping the certain values in two vectors by using iter_swap.
Output:

advertisement
$ g++ msa1.cpp
$ a.out
10 99 40 99

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

  1.     #include <iostream>
  2.     #include <algorithm>
  3.     #include <vector>
  4.     #include <functional>
  5.     using namespace std;
  6.     int op_increase (int i)
  7.     {
  8.         return ++i;
  9.     }
  10.     int main ()
  11.     {
  12.         vector<int> a;
  13.         vector<int> b;
  14.         for (int i = 1; i < 4; i++)
  15.         a.push_back (i * 10);
  16.         b.resize(a.size());
  17.         transform (a.begin(), a.end(), b.begin(), op_increase);
  18.         transform (a.begin(), a.end(), b.begin(), a.begin(), plus<int>());
  19.         for (vector<int> :: iterator it = a.begin(); it != a.end(); ++it)
  20.             cout << ' ' << *it;
  21.         return 0;
  22.     }

a) 21
b) 41
c) 61
d) 21 41 61
View Answer

Answer: d
Explanation: In this program, We allocated the values to the vector and then by using transform function, We increased the values.
Output:

$ g++ msa2.cpp
$ a.out
21 41 61

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

  1.     #include <iostream>
  2.     #include <algorithm>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 };
  7.         int* pbegin = myints;
  8.         int* pend = myints + sizeof(myints) / sizeof(int);
  9.         pend = remove (pbegin, pend, 20);
  10.         for (int* p = pbegin; p != pend; ++p)
  11.             cout << ' ' << *p;
  12.         return 0;
  13.     }

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

Answer: b
Explanation: In this program, We removed the values in the vector by using the remove method.
Output:

$ g++ msa3.cpp
$ a.out
10 30 30 10 10

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 myints[] = { 10, 20, 30 ,40 };
  8.         int * p;
  9.         p = find (myints, myints + 4, 30);
  10.         --p;
  11.         cout << *p << '\n';
  12.         return 0;
  13.     }

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

Answer: b
Explanation: In this program, We used the find method to find the value before 20.
Output:

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

9. How many kind of operation can be applied to transform method in c++?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: b
Explanation: There are two kinds of operations. They are unary and binary operation.

10. What operator is used to remove the dupplicates in the range?
a) )
b) ^
c) %
d) ==
View Answer

Answer: d
Explanation: The function uses operator== to compare the pairs of elements.

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.

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.