C++ Stack

This quiz tests your knowledge of C++ Stack container and its associated operations. It contains 10 MCQs.

Last Updated :
Discuss
Comments

Question 1

How do you create a deque of strings?

  • deque<string> dq;

  • deque<char[]> dq;

  • deque char* dq;

  • string deque dq

Question 2

Which of the following operations on a deque has O(n) time complexity?

  • insert()

  • push_front() 

  • pop_back()

  • erase()

Question 3

Which of the following operations is not supported by deques in C++?

  • Insertion at both ends

  • Deletion at both ends

  • Accessing elements at any index

  • Sorting the elements

Question 4

How to find the size of the deque?

  • Using size()

  • Using length()

  • Using getSize()

  • Must be counted using loop

Question 5

What is the time complexity of push_front() operation in a C++ deque?

  • O(1)

  • O(n log n)

  • O(n)

  • O(log n)

Question 6

Match the deque operation with time complexity:

I. Insert at Beginning i) O(n)

II. Delete from Middle ii) O(1)

III. Find iii) O(n)

IV. Random Index Access iv) O(1)

  • I-ii, II-i, III-iv, IV-iii

  • I-ii, II-iii, III-i, IV-iv

  • I-i, II-iii, III-n, IV-ii

  • I-i, II-i, III-i, IV-i

Question 7

How does C++ STL deque achieve O(1) insertions, deletions, and random access?

  • Circular array with dynamic resizing

  • Linked list for insertions and deletions

  • Array of pointers to fixed-size blocks

  • Single dynamic array with resizing

Question 8

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

C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
    deque<int> dq = {1, 2, 3};
    dq.push_back(4);
    dq.push_front(0);
    dq.pop_back();
    dq.pop_front();
    for (int i : dq)
        cout << i << " ";
    return 0;
}


  • 0 1 2

  • 2

  • 1 2 3

  • 1 2

Question 9

What will be the output of the following program?

C++
#include <bits/stdc++.h>
using namespace std;
void specialInsert(deque<int> &dq, int val)
{
    if (dq.size() == 0)
        dq.push_back(val);
    else if (val > dq.back())
        dq.push_back(val);
    else if (val < dq.front())
        dq.push_front(val);
    else
    {
        dq.insert(dq.end() - 1, val);
    }
}
int main()
{
    deque<int> dq;
    specialInsert(dq, 1);
    specialInsert(dq, 9);
    specialInsert(dq, 7);
    specialInsert(dq, 2);
    cout << dq.front() << endl;
    cout << dq.back();
    return 0;
}


  • 9 2

  • 1 2

  • 1 9

  • 7 9

Question 10

Add the missing code to get the given ouptut

C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
    deque<int> dq = {1, 2, 3};

    // Missing code here

    for (int i : dq)
        cout << i << " ";
    return 0;
}


  • dq[1] = 23

  • dq.insert(dq.begin() + 1, 23);

  • dq.insert(dq.begin()++, 23);

  • dq.insert(1, 23);

Tags:

There are 10 questions to complete.

Take a part in the ongoing discussion