My Report

C++ Programming Practice Test 7


Correct Answer: 2 points | Wrong: -1 point
Grades: A* (100% score) | A (80%-99%) | B (60%-80%) | C (40%-60%) | D (0%-40%)
advertisement

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

#include <iostream> 
#include <vector> 
#include <forward_list>  

using namespace std; 
  
int main() 
{ 
    forward_list<int> fl1 = {1,2,3,4,5};
    for (int&c : fl1)  
        cout << c << " ";
    cout<<endl;
    forward_list<int>::iterator ptr = fl1.begin(); 
    fl1.erase_after(ptr);
    for (int&c : fl1)  
        cout << c << " ";
    cout<<endl;
    return 0; 
} 

2. How the size of a vector increases once it is full?

3. In which type of storage location are the vector members stored?

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

    #include <iostream>
    #include <map> 
    using namespace std;
    int main ()
    {
        multimap<char, int> mymultimap;
        mymultimap.insert(make_pair('x', 100));
        mymultimap.insert(make_pair('y', 200));
        mymultimap.insert(make_pair('y', 350));
        mymultimap.insert(make_pair('z', 500));
        cout << mymultimap.size() << '\n';
        return 0;
    }

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

    #include <iostream>
    #include <vector>
    using namespace std;
    int main ()
    {
        vector<int> a (3, 0);
        vector<int> b (5, 0);
        b = a;
        a = vector<int>();
        cout << "Size of a " << int(a.size()) << '\n';
        cout << "Size of b " << int(b.size()) << '\n';
        return 0;
    }

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

  
    #include <new>
    #include <iostream>
    using namespace std;
    struct A 
    {
        virtual ~A() {  };
        void operator delete(void* p) 
        {
            cout << "A :: operator delete" << endl;
        }
    };
    struct B : A 
    {
        void operator delete(void* p) 
        {
            cout << "B :: operator delete" << endl;
        }
    };
    int main() 
    {
        A* ap = new B;
        delete ap;
    } 

7. How can the member functions in the container be accessed?

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

    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int main () 
    {
        vector<int> first (5, 10);
        vector<int> second (5, 33);
        vector<int>::iterator it;
        swap_ranges(first.begin() + 1, first.end() - 1, second.begin());
        cout << " first contains:";
        for (it = first.begin(); it != first.end(); ++it)
            cout << " " << *it;
        cout << "\nsecond contains:";
        for (it = second.begin(); it != second.end(); ++it)
            cout << " " << *it;
        return 0;
    }

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

    #include <iostream>
    #include <map>
    using namespace std;
    int main ()
    {
        multimap<char, int> mymultimap;
        mymultimap.insert(make_pair('y', 202));
        mymultimap.insert(make_pair('y', 252));
        pair<char, int> highest = *mymultimap.rbegin();
        multimap<char, int> :: iterator it = mymultimap.begin();
        do 
        {
            cout << (*it).first << " => " << (*it).second << '\n';
        } while ( mymultimap.value_comp()(*it++, highest) );
        return 0;
    }

10. How many iterators are needed for the defining a new container?


 

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.