# C++ Programming Questions and Answers – Non Modifying Sequence Algorithms

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

1. How does a sequence of objects are accessed in c++?
a) Iterators
b) Pointers
c) Both Iterators & Pointers
d) Objects

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.

2. How many parameters are present in mismatch method in non-sequence modifying algorithm?
a) 1
b) 2
c) 3
d) 3 or 4

Explanation: There are two definitions of mismatch with either three or four parameters. They are first1, last1, first2 and optional predicate.

3. What will happen in ‘all_of’ method if the range is empty?
a) Return true
b) Return false
c) Return nothing
d) Return error

Explanation: Returns true if pred returns true for all the elements in the range [first, last) or if the range is empty, and false otherwise.

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

1. `    #include <iostream>`
2. `    #include <algorithm>`
3. `    #include <vector>`
4. `    #include <utility>`
5. `    using namespace std;`
6. `    bool mypredicate (int i, int j)`
7. `    {`
8. `        return (i == j);`
9. `    }`
10. `    int main () `
11. `    {`
12. `        vector<int> myvector;`
13. `        for (int i = 1; i < 6; i++) myvector.push_back (i * 10);`
14. `        int myints[] = {10, 20, 30, 40, 1024};`
15. `        pair<vector<int> :: iterator, int*> mypair;`
16. `        mypair = mismatch (myvector.begin(), myvector.end(), myints);`
17. `        cout  << *mypair.first<<'\n';`
18. `        cout  << *mypair.second << '\n';`
19. `        ++mypair.first; ++mypair.second;`
20. `        return 0;`
21. `    }`

a)

```   40
1024```

b)

```   50
1024```

c)

```   20
1024```

d) 1024

Explanation: In this program, We are finding the elements which are mismatching in both the variables.
Output:

```\$ g++ non.cpp
\$ a.out
50
1024```

5. 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 IsOdd (int i)`
6. `    {`
7. `        return ((i % 2) == 1);`
8. `    }`
9. `    int main () `
10. `    {`
11. `        vector<int> myvector;`
12. `        myvector.push_back(10);`
13. `        myvector.push_back(25);`
14. `        myvector.push_back(40);`
15. `        myvector.push_back(55);`
16. `        vector<int> :: iterator it = find_if (myvector.begin(), `
17. `        myvector.end(), IsOdd);`
18. `        cout  << *it << '\n';`
19. `        return 0;`
20. `    }`

a) 10
b) 25
c) 40
d) 55

Explanation: In this program, We used find_if method and returned the first odd value in the vector.
Output:

```\$ g++ non1.cpp
\$ a.out
25```

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. `        vector<int> myvector;`
8. `        for (int i = 1; i < 5; ++i)`
9. `            myvector.push_back(i);`
10. `        rotate(myvector.begin(), myvector.begin() + 3, myvector.end( ));`
11. `        for (vector<int> :: iterator it = myvector.begin(); `
12. `            it != myvector.end(); ++it)`
13. `        cout << ' ' << *it;`
14. `        return 0;`
15. `    }`

a) 1 2 3 4
b) 4 3 2 1
c) 3 4 2 1
d) 4 1 2 3

Explanation: In this program, We are rotating the vector values by 3, So it is printing this option.
Output:

```\$ g++ non2.cpp
\$ a.out
4 1 2 3```

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[] = {10, 20, 20, 20, 30, 30, 20, 20, 10};`
12. `        vector<int> myvector (myints, myints + 9);`
13. `        vector<int> :: iterator it;`
14. `        it = unique (myvector.begin(), myvector.end());                                `
15. `        myvector.resize( distance(myvector.begin(), it) );`
16. `        unique (myvector.begin(), myvector.end(), myfunction);`
17. `        for (it = myvector.begin(); it != myvector.end(); ++it)`
18. `            cout << ' ' << *it;`
19. `        return 0;`
20. `    }`

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

Explanation: In this program, We are printing only the unique values by comparing every value.
Output:

```\$ g++ non3.cpp
\$ a.out
10 20 30 20 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, 30, 20, 10, 10, 20};`
8. `        int mycount = count (myints, myints + 8, 10);`
9. `        cout << mycount;`
10. `        vector<int> myvector (myints, myints + 8);`
11. `        mycount = count (myvector.begin(), myvector.end(), 20);`
12. `        cout << mycount;`
13. `        return 0;`
14. `    }`

a) 33
b) 44
c) 22
d) 55

Explanation: In this program, We are counting the number of 10’s and 20’s in the program.
Output:

```\$ g++ non4.cpp
\$ a.out
33```

9. To what kind of elements does non-modifying sequence algorithm can be applied?
a) Range
b) Vector
c) List
d) Methods

Explanation: Non-modifying sequence algorithm can be applied to list and vector for example the “find” function can be applied to list and vector.

10. Pick out the incorrect method in non-modifying sequence algorithm?
a) find-if
b) none-of
c) any-of
d) like 