Questions and Answers


  1. What do all STL containers define?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    All the STL containers define the iterator types for that container, e.g., iterator and const_iterator, e.g., vector::iterator and the begin/end methods for that container, e.g., begin() and end().


  1. What is the output of this program?
    #include <iostream>
    #include <map>
    using namespace std;
    int main ()
    {
    multimap<char, int> MultimapData;
    MultimapData.insert(make_pair('Q', 105));
    MultimapData.insert(make_pair('Q', 250));
    MultimapData.insert(make_pair('Q', 120));
    MultimapData.insert(make_pair('Q', 102));
    MultimapData.insert(make_pair('Q', 220));
    pair<char, int> highest = *MultimapData.rbegin();
    multimap<char, int> :: iterator Iter = MultimapData.begin();
    do
    {
    cout << (*Iter).first << " => " << (*Iter).second << '\n';
    } while ( MultimapData.value_comp()(*Iter++, highest) );
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    In this program, the method rbegin is used to return the first element in the map.



  1. What is the output of this program?
    #include <iostream>
    #include <queue>
    using namespace std;
    int main ()
    {
    priority_queue<int> QueueData;
    QueueData.push(12);
    QueueData.push(25);
    QueueData.push(13);
    cout << QueueData.top() << endl;
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    In this program, We used the queue template and the top method is used to retain the last but before element.


  1. What is the output of this program?
    #include <iostream>
    #include <map>
    using namespace std;
    int main ()
    {
    multimap<char, int> MultimapData;
    MultimapData.insert(make_pair('P', 150));
    MultimapData.insert(make_pair('Q', 250));
    MultimapData.insert(make_pair('Q', 300));
    MultimapData.insert(make_pair('R', 450));
    cout << MultimapData.size() << '\n';
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: E

    In this program, We are counting the number of elements in the map.



  1. What is the lifetime of the element in container?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    A Container “owns” its elements: the lifetime of an element stored in a container cannot exceed that of the Container itself.