C++ Vector Container Aptitude Questions and Answers

C++ Vector Container Aptitude: This section contains C++ Vector Container Aptitude Questions and Answers with explanations.
Submitted by Nidhi, on March 09, 2021

1) There are the following statements that are given below, which of them are correct about vector container in C++?

  1. It is a sequence container class.
  2. It is a derived container class.
  3. It is used to implement a dynamic array; it means size will automatically increase when we add new items.
  4. The vector objects store items in a contiguous manner.

Options:

  1. A and C
  2. B and C
  3. A, C, and D
  4. B, C, and D

2) Which of the following is correct syntax to create an object "ob" of vector container class in C++?

  1. vector ob;
  2. vector <data_type> ob;
  3. Vector ob;
  4. Vector <data_type> ob;

3) Which of the following header file is required to use vector container class in C++?

  1. <vector>
  2. <vcontainer>
  3. <scontainer>
  4. <seqcont>

4) Which of the following function is used to add an element to the end in the vector?

  1. add()
  2. push()
  3. push_back()
  4. push_end()

5) Which of the following function is used to add an element at the specified position in the vector?

  1. add()
  2. insert()
  3. additem()
  4. insertitem()

6) Which of the following is correct syntax to create an iterator object "itr" to access vector container elements in C++?

  1. vector<data_type>::iterator itr;
  2. vector<data_type>.iterator itr;
  3. vector::iterator<data_type> itr;
  4. iterator<data_type> itr;

7) What is the correct output of given code snippets in C++?

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<string> vec;
    vector<string>::iterator iter;

    vec.push_back("India ");
    vec.push_back("UP ");
    vec.push_back("Noida ");

    iter = vec.begin();
    while (iter != vec.end()) {
        ++iter;
        cout << *iter;
    }

    return 0;
}

Options:

  1. India UP Noida
  2. UP Noida
  3. India UP
  4. Syntax error

8) What is the correct output of given code snippets in C++?

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<string> vec;
    vector<string>::iterator iter;

    vec.push_back("India ");
    vec.push_back("UP ");
    vec.push_back("Noida ");

    iter = vec.begin();
    cout << *iter;
    
    while (iter != vec.end()) {
        ++iter;
        cout << *iter;
    }

    return 0;
}

Options:

  1. India UP Noida
  2. UP Noida
  3. India UP
  4. Syntax error

9) Which of the following function is used to check a vector object is empty or not?

  1. isempty()
  2. empty()
  3. noelement()
  4. noitem()

10) Which of the following function is used to remove all elements of vector in C++?

  1. remove()
  2. removeall()
  3. clear()
  4. erase()

11) Which of the following function is used to get total number of elements in vector?

  1. len()
  2. length()
  3. count()
  4. size()

12) What is the correct output of given code snippets?

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> v1;

    v1.push_back(1);
    v1.push_back(2);
    v1.push_back(3);
    v1.push_back(4);

    for (int i = 0; i < v1.size(); i++)
        cout << v1[i] << " ";

    return 0;
}

Options:

  1. 1 2 3 4
  2. Garbage value
  3. Syntax error
  4. Runtime error

13) What is the correct output of given code snippets?

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> v1;

    v1.push_back(1);
    v1.push_back(2);
    v1.push_back(3);
    v1.push_back(4);

    for (int i = 0; i < v1.size(); i++)
        cout << *(v1 + i) << " ";

    return 0;
}

Options:

  1. 1 2 3 4
  2. Garbage value
  3. Syntax error
  4. Runtime error

14) What is the correct output of the given code snippets?

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> v1;
    vector<int> v2;

    v1.push_back(1);
    v1.push_back(2);
    v1.push_back(3);
    v1.push_back(4);

    v2.push_back(5);
    v2.push_back(6);
    v2.push_back(7);
    v2.push_back(8);

    v1.swap(v2);

    for (int i = 0; i < v2.size(); i++)
        cout << v2[i] << " ";

    return 0;
}

Options:

  1. 1 2 3 4
  2. 5 6 7 8
  3. Garbage value
  4. Syntax error

15) What is the correct output of the given code snippets?

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> v1;
    vector<int> v2;

    v1.push_back(1);
    v1.push_back(2);
    v1.push_back(3);
    v1.push_back(4);

    v2.push_back(1);
    v2.push_back(2);
    v2.push_back(3);
    v2.push_back(4);

    if (v1 == v2)
        cout << "both are equal";
    else
        cout << "both are not equal";

    return 0;
}

Options:

  1. both are equal
  2. both are not equal
  3. Syntax error
  4. Runtime error

16) Which of the following operators are overloaded in the vector container?

  1. <
  2. >
  3. +
  4. -

Options:

  1. A and B
  2. C and D
  3. A, B, C, and D
  4. None of the above

17) Which of the following function is used to get how many elements a vector can hold?

  1. maxlen()
  2. max_len()
  3. maxsize()
  4. max_size()

18) Which of the following are not correct functions of vector class in C++?

  1. begin()
  2. rend()
  3. rand()
  4. cend()

Options:

  1. A and B
  2. A and D
  3. C
  4. D

19) Which of the following function is used to insert an item just before a given position in the vector?

  1. insertbefore()
  2. emplace()
  3. emplace_back()
  4. insert_back()

20) Which of the following function is used to write data from vector to an array?

  1. data()
  2. writedata()
  3. convertvector()
  4. coverttoarray()

21) What is the correct output of the given code snippets?

#include <iostream>
#include <cstring>
#include <vector>
using namespace std;

int main()
{
    vector<string> v;

    v.push_back("India ");
    v.push_back("UP ");
    v.push_back("Noida ");

    string str[3] = v.data();

    for (int i = 0; i < v.size(); i++)
        cout << *str++ << " ";

    return 0;
}

Options:

  1. India UP Noida
  2. UP Noida
  3. Syntax error
  4. Runtime error

22) What is the correct output of given code snippets in C++?

#include <iostream>
#include <cstring>
#include <vector>
using namespace std;

int main()
{
    vector<string> v;

    v.push_back("India ");
    v.push_back("UP ");
    v.push_back("Noida ");

    string str[3] = v.data();

    for (int i = 0; i < v.size(); i++)
        cout << str[i] << " ";

    return 0;
}

Options:

  1. India UP Noida
  2. UP Noida
  3. Syntax error
  4. Runtime error

23) What is the correct output of given code snippets in C++?

#include <iostream>
#include <cstring>
#include <vector>
using namespace std;

int main()
{
    vector<string> v;

    v.push_back("India ");
    v.push_back("UP ");
    v.push_back("Noida ");

    string* str = v.data();

    for (int i = 0; i < v.size(); i++)
        cout << *str++ << " ";

    return 0;
}

Options:

  1. India UP Noida
  2. UP Noida
  3. Syntax error
  4. Runtime error

24) What is the correct output of given code snippets in C++?

#include <iostream>
#include <cstring>
#include <vector>
using namespace std;

int main()
{
    vector<string> v;

    v.push_back("India ");
    v.push_back("UP ");
    v.push_back("Noida ");

    string* str = v.data();

    for (int i = 0; i < v.size(); i++)
        cout << str[i] << " ";

    return 0;
}

Options:

  1. India UP Noida
  2. UP Noida
  3. Syntax error
  4. Runtime error

25) What is the correct output of given code snippets in C++?

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<string> v;

    v.push_back(10);
    v.push_back(20);
    v.push_back(30);

    int* num = v.data();

    for (int i = 0; i < v.size(); i++)
        cout << num[i] << " ";

    return 0;
}

Options:

  1. 10 20 30
  2. Garbage value
  3. Syntax error
  4. Runtime error






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.