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

4/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>
#include <Valarray>
using namespace std;
int main()
{
	Valarray<int> varr = { 1, 2, 3, 4, 5 };
	for (int &x: varr) cout << x << " ";
	cout<<endl;
	varr = varr.shift(2);
	for (int &x: varr) cout << x << " ";
	return 0;
}

Select an option to see the answer and solution.

What function will be called when we have an uncaught exception?

Select an option to see the answer and solution.

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;
    fl1.remove_if([](int x){ return x > 3;});
    for (int&c : fl1)  
        cout << c << " ";
    cout<<endl;
    return 0; 
}

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <valarray>
using namespace std;
int main()
{
	valarray<int> varr = { 10, 2, 20, 1, 30 };
	cout<<"Sum of array: "<<varr.sum();
	return 0;
}

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 Base
{
    public:
    Base ( )
    {
        cout << "1" << endl;
    }
    ~Base ( )
    {
        cout << "2" << endl;
    }
};
class Derived : public Base
{
    public:
    Derived  ( )
    {
        cout << "3" << endl;
    }
    ~Derived ( )
    {
        cout << "4" << endl;
    }    
}; 
int main( )
{
    Derived x;
}

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
	bitset<8> b1(95);
	bitset<8> b2 = b1 << 3;
	cout<<b2;
}

Select an option to see the answer and solution.

What is the main feature of locale in C++?

Select an option to see the answer and solution.

Elements in STL heap are removed in . . . . . . . .

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main () 
{
    vector<int> myvector;
    for (int i = 1; i < 5; ++i)
        myvector.push_back(i);
    rotate(myvector.begin(), myvector.begin() + 3, myvector.end( ));
    for (vector<int> :: iterator it = myvector.begin(); 
        it != myvector.end(); ++it)
    cout << ' ' << *it;
    return 0;
}

Select an option to see the answer and solution.

What are command line arguments?

Select an option to see the answer and solution.

How many parameters are there in getline function?

Select an option to see the answer and solution.

Which of the following relationships is uni-directional?

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 = 10;
    if (a < 15)
    {
        time:
        cout << a;
        goto time;
    }
    break;
    return 0;
}

Select an option to see the answer and solution.

Which is used to create a pure virtual function?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
    int num = 3;
    string str_bad = "wrong number used";
    try
    {
        if ( num == 1 )
        {       
            throw 5;
        }
        if ( num == 2 )
        {
            throw 1.1f;
        }
        if ( num != 1 || num != 2 )
        {    
            throw str_bad;
        }
    }
    catch (int a)
    {
        cout << "Exception is: " << a << endl;
    }
    catch (float b)
    {
        cout << "Exception is: " << b << endl;
    }
    catch (...)
    {
        cout  << str_bad << endl;
    }
    return 0;
}

Select an option to see the answer and solution.

Which of the following function is used to insert an element at the end of a vector?

Select an option to see the answer and solution.

What are the tuples in C++?

Select an option to see the answer and solution.

How many ways of reusing are there in the class hierarchy?

Select an option to see the answer and solution.

Which header file is used for the numeric limits in C++?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <tuple>
using namespace std;
int main()
{
	tuple <int, char, string> tp1;
	tuple <int, char, string> tp2;
	tp1 = make_tuple(6, 'a', "Hello");
	tp2 = make_tuple(9, 'z', "World");
	cout<<"("<<get<0>(tp1)<<", "<<get<1>(tp1)<<", "<<get<2>(tp1)<<")"<<endl;
	cout<<"("<<get<0>(tp2)<<", "<<get<1>(tp2)<<", "<<get<2>(tp2)<<")"<<endl;
	tp1.swap(tp2);
	cout<<"("<<get<0>(tp1)<<", "<<get<1>(tp1)<<", "<<get<2>(tp1)<<")"<<endl;
	cout<<"("<<get<0>(tp2)<<", "<<get<1>(tp2)<<", "<<get<2>(tp2)<<")"<<endl;
	return 0;
}

Select an option to see the answer and solution.