C++ Programming MCQ – Vector

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Vector”. 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 “Vector” along with answers, explanations and/or solutions:

1. What do vectors represent?
a) Static arrays
b) Dynamic arrays
c) Stack
d) Queue

Explanation: Vectors are sequence containers representing arrays that can change in size.

2. In which type of storage location are the vector members stored?
a) Contiguous storage locations
b) Non-contiguous storage locations
c) Contiguous & Non-contiguous storage locations
d) Memory storage locations

Explanation: Vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements

3. How many vector container properties are there in c++?
a) 1
b) 2
c) 3
d) 4

Explanation: There are three container properties in c++. They are sequence, Dynamic array and allocator-aware.

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

1. `    #include <iostream>`
2. `    #include <vector>`
3. `    using namespace std;`
4. `    int main ()`
5. `    {`
6. `        unsigned int i;`
7. `        vector<int> first;`
8. `        vector<int> second (4, 100);`
9. `        vector<int> third (second.begin(), second.end());`
10. `        vector<int> fourth (third);`
11. `        int myints[] = {16, 2, 77, 29};`
12. `        vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );`
13. `        for (vector<int> :: iterator it = fifth.begin(); it != fifth.end(); ++it)`
14. `            cout << ' ' << *it;`
15. `        return 0;`
16. `    }`

a) 16
b) 16 2
c) 16 2 77
d) 16 2 77 29

Explanation: In this program, We got the values and printing it by using the vector and we are contructing vectors.
Output:

Note: Join free Sanfoundry classes at Telegram or Youtube
```\$ g++ vect.cpp
\$ a.out
16 2 77 29```

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

1. `    #include <iostream>`
2. `    #include <vector>`
3. `    using namespace std;`
4. `    int main ()`
5. `    {`
6. `        vector<int> myvector;`
7. `        int sum (0);`
8. `        myvector.push_back (100);`
9. `        myvector.push_back (200);`
10. `        myvector.push_back (300);`
11. `        while (!myvector.empty())`
12. `        {`
13. `            sum += myvector.back();`
14. `            myvector.pop_back();`
15. `        }`
16. `        cout << sum << '\n';`
17. `        return 0;`
18. `    }`

a) 500
b) 600
c) 700
d) Error

Explanation: In this program, We are forming a stack and adding the elements and We are finding the total number of elements that are in stack.
Output:

```\$ g++ vect1.cpp
\$ a.out
600```

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

1. `    #include <iostream>`
2. `    #include <vector>`
3. `    using namespace std;`
4. `    int main ()`
5. `    {`
6. `        vector<int> a (3, 0);`
7. `        vector<int> b (5, 0);`
8. `        b = a;`
9. `        a = vector<int>();`
10. `        cout << "Size of a " << int(a.size()) << '\n';`
11. `        cout << "Size of b " << int(b.size()) << '\n';`
12. `        return 0;`
13. `    }`

a)

```   Size of a 0
Size of b 3```

b)

```   Size of a 3
Size of b 5```

c) Error
d)

```   Size of a 3
Size of b 9```
Explanation: In this program, We are finding the size of the vector elements.
Output:

```\$ g++ vect2.cpp
\$ a.out
Size of a 0
Size of b 3```

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

1. `    #include <iostream>`
2. `    #include <vector>`
3. `    using namespace std;`
4. `    int main ()`
5. `    {`
6. `        vector<int> first;`
7. `        first.assign (7,100);`
8. `        vector<int>::iterator it;`
9. `        it=first.begin()+1;`
10. `        int myints[] = {1776,7,4};`
11. `        cout << int (first.size()) << '\n';`
12. `        return 0;`
13. `    }`

a) 10
b) 9
c) 8
d) 7

Explanation: In this program, We are finding the size of the vector elements and resizing it.
Output:

```\$ g++ vect3.cpp
\$ a.out
7```

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

1. `    #include <iostream>`
2. `    #include <vector>`
3. `    using namespace std;`
4. `    int main ()`
5. `    {`
6. `        vector<int> myvector (5);`
7. `        int* p = myvector.data();`
8. `        *p = 10;`
9. `        ++p;`
10. `        *p = 20;`
11. `        p[2] = 100;`
12. `        for (unsigned i = 0; i < myvector.size(); ++i)`
13. `            cout << ' ' << myvector[i];`
14. `        return 0;`
15. `    }`

a) 10 20 0 100 0
b) 10 20 0 100
c) 10 20 0
d) 10 20

Explanation: In this program, We are allocating the values to the vector and unallocated values are left as zero.
Output:

```\$ g++ vect4.cpp
\$ a.out
10 20 0 100 0```

9. Pick out the correct statement about vector.
a) vector<int> values (5)
b) vector values (5)
c) vector<int> (5)
d) vector<5>

Explanation: The syntax for declaring the vector element is vector<type> variable_name (number_of_elements);

10. Which is optional in the declaration of vector?
a) Type
b) Name
c) Vector
d) Number_of_elements

Explanation: The number of elements is optional. An empty vector means, A vector that contains zero 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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]