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

26/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 msg) 
    {
        cerr << msg << endl;
    }
    return 0;
}

Select an option to see the answer and solution.

Which of the following is correct syntax of making heap from a vector v?

Select an option to see the answer and solution.

What is mandatory for designing a new container?

Select an option to see the answer and solution.

Pick out the incorrect method in non-modifying sequence algorithm?

Select an option to see the answer and solution.

What do container adapter provide to interface?

Select an option to see the answer and solution.

Given the below class, what is the correct syntax of declaring a functor that adds 10 to each of the passed argument?
class Add
{
	int x;
   public:
	Add(int x){
		this->x = x;
	}
	int operator()(int a){
		return x+a;
	}
};

Select an option to see the answer and solution.

setprecision requires which of the following header file?

Select an option to see the answer and solution.

What is the correct syntax of constructing any using assignment operator?

Select an option to see the answer and solution.

What is random_device?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <stdexcept>
#include <limits>
#include <iostream>
using namespace std;
void func(int c)
{
    if (c < numeric_limits<char> :: max())
        throw invalid_argument("MyFunc argument too large.");
    else
    {
        cout<<"Executed";
    }
}
int main()
{
    try
    {
        func(256);
    }
    catch(invalid_argument& e)
    {
        cerr << e.what() << endl;
        return -1;
    }
    return 0;
}

Select an option to see the answer and solution.

Which of the following function is used to get the actual number of elements stored in vector?

Select an option to see the answer and solution.

What are functors in C++?

Select an option to see the answer and solution.

How many streams are automatically created when executing a program?

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 ()
{
    vector<int> myvector (3);
    for (unsigned i = 0; i < myvector.size(); i++)
    myvector.at(i) = i;
    for (unsigned i = 0; i < myvector.size(); i++)
    cout << ' ' << myvector.at(i);
    return 0;
}

Select an option to see the answer and solution.

What is the use of is_array() function in C++?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <new>
using namespace std;
int main ()
{
    int i, n;
    int * p;
    i = 2;
    p= new (nothrow) int[i];
    if (p == 0)
        cout << "Error: memory could not be allocated";
    else
    {
        for (n=0; n<i; n++)
        {
            p[n] = 5;
        }
        for (n = 0; n < i; n++)
            cout << p[n];
        delete[] p;
     }
     return 0;
}

Select an option to see the answer and solution.

What is the correct syntax of defining function template/template functions?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
using namespace std;
long factorial (long a)
{
    if (a > 1)
        return (a * factorial (a + 1));
    else
        return (1);
}
int main ()
{
    long num = 3;
    cout << num << "! = " << factorial ( num );
    return 0;
}

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <vector> 
#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;
int square(int i) { return i * i; }
int main()
{
    vector<int> V, V2;
    V.push_back(0);
    V.push_back(1);
    V.push_back(2);
    transform(V.begin(), V.end(), back_inserter(V2), square);
    copy(V2.begin(), V2.end(), ostream_iterator<int>(cout, " "));
    cout << endl;
}

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class BaseClass 
{
    int x;
    public:
    void setx(int n) 
    {
        x = n;
    }
    void showx() 
    {
        cout << x ;
    }
};
class DerivedClass : private BaseClass
{
    int y;
    public:
    void setxy(int n, int m)
    {
        setx(n);      
        y = m;
    }
    void showxy() 
    {
        showx();       
        cout << y << '\n';
    }
};
int main()
{
    DerivedClass ob;
    ob.setxy(10, 20);
    ob.showxy();
    return 0;
}

Select an option to see the answer and solution.