Vidyalelo
C++ Programming · all questions

C++ miscellaneous
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

1,008

Questions

20/51

Page

Pick an option on a question to see the right answer and solution.

What will be the output of the following C++ code?
#include <iostream>
using namespace std;
double division(int a, int b)
{
    if (b == 0)
    {
        throw "Division by zero condition!";
    }
    return (a / b);
}
int main ()
{
    int x = 50;
    int y = 0;
    double z = 0;
    try 
    {
        z = division(x, y);
        cout << z << endl;
    }
    catch (const char* msg) 
    {
        cerr << msg << endl;
    }
    return 0;
}

Select an option to see the answer and solution.

If inner catch block is unable to handle the exception thrown then . . . . . . . .

Select an option to see the answer and solution.

Where are allocators used?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
using namespace std;
template<class T = float, int i = 5> class A
{
    public:
    A();
    int value;
};
template<> class A<> 
{ 
    public: A(); 
};
template<> class A<double, 10>
{ 
    public: A(); 
};
template<class T, int i> A<T, i>::A() : value(i)
{
    cout << value;
}
A<>::A() 
{
    cout << "default";
}
A<double, 10>::A() 
{
    cout << "10" << endl;
}
int main() 
{
    A<int, 6> x;
    A<> y;
    A<double, 10> z;
}

Select an option to see the answer and solution.

What is the use of checked iterators?

Select an option to see the answer and solution.

Which word is used to stop the unpacking of a value in a tuple?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <iterator>
#include <list>
using namespace std;
int main () 
{
    list<int> mylist;
    for (int i = 0; i < 5; i++) 
        mylist.push_back (i * 20);
    list<int> :: iterator first = mylist.begin();
    list<int> :: iterator last = mylist.end();
    cout << distance(first, last) << endl;
    return 0;
}

Select an option to see the answer and solution.

Which is a constant defined in <cstdlib> header file?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main ()
{
    int a = 100;
    double b = 3.14;
    cout << a;
    cout << endl;
    cout << b << endl << a * b;
    endl (cout);
    return 0;
}

Select an option to see the answer and solution.

Which exception is thrown by dynamic_cast?

Select an option to see the answer and solution.

How many items are presented in the associate container?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <memory>
#include <algorithm>
using namespace std;
int main ()
{
    int numbers[] = {1, 5, 4, 5};
    pair <int*, ptrdiff_t> result = get_temporary_buffer<int>(4);
    if (result.second > 0)
    {
        uninitialized_copy (numbers, numbers + result.second, result.first);
        sort (result.first, result.first + result.second);
        for (int i = 0; i < result.second; i++)
            cout << result.first[i] << " ";
        return_temporary_buffer (result.first);
    }
    return 0;
}

Select an option to see the answer and solution.

Which constructor will initialize the base class data member?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
using namespace std;
struct X;
struct Y 
{
    void f(X*);
};
struct X 
{
    private:
    int i;
    public:
    void initialize(); 
    friend void g(X* , int);
    friend void Y :: f(X*);
    friend struct Z;
    friend void h();
};
void X :: initialize() 
{
    i = 0;
}
void g(X* x, int i) 
{
    x -> i = i;
}
void Y :: f(X * x) 
{
    x -> i = 47;
    cout << x->i;
}
struct Z 
{
    private:
    int j;
    public:
    void initialize();
    void g(X* x);
};
void Z::initialize() 
{
    j = 99;
}
void Z::g(X* x) 
{
    x -> i += j;
}
void h() 
{
    X x;
    x.i = 100;
    cout << x.i;
}
int main() 
{
    X x;
    Z z;
    z.g(&x);
    cout << "Data accessed";
}

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
    unsigned int i;
    vector<int> first;
    vector<int> second (4, 100);
    vector<int> third (second.begin(), second.end());
    vector<int> fourth (third);
    int myints[] = {16, 2, 77, 29};
    vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
    for (vector<int> :: iterator it = fifth.begin(); it != fifth.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

Select an option to see the answer and solution.

What does size() function returns?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include<iostream> 
#include<iterator> 
#include<vector> 
using namespace std; 
int main() 
{ 
    vector<int> ar = { 1, 2, 3, 4, 5 }; 
    vector<int>::iterator ptr = ar.begin(); 
    advance(ptr, 2);
    cout << *ptr << endl; 
    return 0; 
}

Select an option to see the answer and solution.

Which of the following is correct?

Select an option to see the answer and solution.

When exceptions are used?

Select an option to see the answer and solution.

Which of te following is a built-in example of functors in C++?

Select an option to see the answer and solution.