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

9/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 <vector>
#include <algorithm>
using namespace std;
int main(int argc, char const *argv[])
{
	vector <int> v = {1,5,23,90,15,35};
	make_heap(v.begin(),v.end());
	v.push_back(110);
	push_heap(v.begin(), v.end());
	pop_heap(v.begin(), v.end());
	v.pop_back();
	cout<<v.front();
}

Select an option to see the answer and solution.

How many kinds of entities are directly parameterized in c++?

Select an option to see the answer and solution.

Which header file is required for using Valarray?

Select an option to see the answer and solution.

Which are the parameters for the content of the buffer?

Select an option to see the answer and solution.

What operation can be performed by destructor?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
template<class T>
T func(T a)
{
	cout<<a;
	return a;
}
 
template<class U>
void func(U a)
{
	cout<<a;
}
 
int main(int argc, char const *argv[])
{
	int a = 5;
	int b = func(a);
	return 0;
}

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include<iostream>
#include<any>
#include<string>
using namespace std;
int main()
{
	string s = "Hello World";
	any var(s);
	cout<<any_cast<string>(var)<<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 <vector>
using namespace std;
int main ()
{
    vector<bool> mask;
    mask.push_back(true);
    mask.flip();
    cout << boolalpha;
    for (unsigned i = 0; i < mask.size(); i++)
    cout << mask.at(i);
    return 0;
}

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
int main ()
{
    string mystring;
    bitset<4> mybits; 
    mybits.set();
    mystring = mybits.to_string<char, char_traits<char>, 
    allocator<char> >();
    cout << mystring << endl;
    return 0;
}

Select an option to see the answer and solution.

What kind of exceptions are available in c++?

Select an option to see the answer and solution.

Which of the following is the correct syntax of accessing the second element of a pair p?

Select an option to see the answer and solution.

Which function is invoked when an unhandled exception is thrown?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <array>
 
using namespace std;
 
int main(int argc, char const *argv[])
{
	int arr1[5] = {1,2,3,4,5};
	int arr2[5] = {6,7,8,9,10};
	arr1.swap(arr2);
	for(int i=0;i<5;i++)
		cout<<arr1[i]<<" ";
	cout<<endl;
	for(int i=0;i<5;i++)
		cout<<arr2[i]<<" ";
	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>
using namespace std;
class Parent
{
    public:
    Parent (void) 
    {     
        cout << "Parent()\n";
    }
    Parent (int i) 
    { 
        cout << "Parent("<< i << ")\n"; 
    };
    Parent (void) 
    { 
        cout << "~Parent()\n";
    }; 
};
class Child1 : public Parent { };
class Child2 : public Parent
{
    public:
    Child2 (void) 
    {
        cout << "Child2()\n";
    }
    Child2 (int i) : Parent (i) 
    {
        cout << "Child2(" << i << ")\n"; 
    }
    ~Child2 (void) 
    {
        cout << "~Child2()\n"; 
    }
};
int main (void)
{
    Child1 a;
    Child2 b;
    Child2 c(42);
    return 0;
}

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <algorithm>
using namespace std;
int main ()
{
    int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 };
    int* pbegin = myints;
    int* pend = myints + sizeof(myints) / sizeof(int);
    pend = remove (pbegin, pend, 20);
    for (int* p = pbegin; p != pend; ++p)
        cout << ' ' << *p;
    return 0;
}

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <typeinfo>
#include <iostream>
using namespace std;
class Myshape
{
    public:
    virtual void myvirtualfunc() const {}
};
class mytriangle: public Myshape
{
    public:
    virtual void myvirtualfunc() const
    {   
    };
};
int main()
{
    Myshape Myshape_instance;
    Myshape &ref_Myshape = Myshape_instance;
    try 
    {
        mytriangle &ref_mytriangle = dynamic_cast<mytriangle&>(ref_Myshape);
    }
    catch (bad_cast)
    {
        cout << "Can't do the dynamic_cast lor!!!" << endl;
        cout << "Caught: bad_cast exception. Myshape is not mytriangle.\n";
    }
    return 0;
}

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
    stringstream mys(ios :: in | ios :: out);
    std :: string dat("The double value is : 74.79 .");
    mys.str(dat);
    mys.seekg(-7, ios :: end);
    double val;
    mys >> val;
    val = val*val;
    mys.seekp(-7,ios::end);
    mys << val;
    std :: string new_val = mys.str();
    cout << new_val;
    return 0;
}

Select an option to see the answer and solution.

What will happen in 'all_of' method if the range is empty?

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, b = 20, c = 30;
    float  d;
    try
    {
        if ((a - b) != 0)
        {
            d = c / (a - b);
            cout << d;
        }
        else
        {
            throw(a - b);
        }
    }
    catch (int i)
    {
        cout<<"Answer is infinite "<<i;
    }
}

Select an option to see the answer and solution.

To which of the following access specifiers are applicable?

Select an option to see the answer and solution.