Explore topic-wise MCQs in C++ Programming.

This section includes 77 Mcqs, each offering curated multiple-choice questions to sharpen your C++ Programming knowledge and support exam preparation. Choose a topic below to get started.

1.

What is the output of this program?
#include <stdexcept>
#include <limits>
#include <iostream>
using namespace std;
void Function(int ch)
{
if (ch < numeric_limits :: max())
throw invalid_argument("Function argument too large.");
else
{
cout< }
}
int main()
{
try
{
Function(150);
}
catch(invalid_argument& excep)
{
cout << excep.what() << endl;
return -1;
}
return 0;
}

A. Function argument too large.
B. Compilation Error
C. Programs Executed...
D. Runtime Error
E. None of these
Answer» D. Runtime Error
2.

What is the output of this program?
#include <iostream>
using namespace std;
void Example(int n)
{
try
{
if (n > 0)
throw n;
else
throw 'L';
}
catch(char)
{
cout << "Catch a integer and that integer is :" << n;
}
}
int main()
{
cout << "Testing multiple catches :";
Example(5);
Example(0);
}

A. Catch a integer and that integer is : 5
B. Compilation Error
C. Testing multiple catches :
D. Runtime Error
E. None of these
Answer» D. Runtime Error
3.

What is the output of this program?
#include <iostream>
#include <exception>
using namespace std;
int main()
{
try
{
double* number= new double[200];
cout << "Memory allocated...";
}
catch (exception& excep)
{
cout << "Exception occurred:" << excep.what() << endl;
}
return 0;
}

A. Memory allocated...
B. Compilation Error
C. Exception occurred...
D. Runtime Error
E. None of these
Answer» B. Compilation Error
4.

What is the output of this program?
#include <iostream>
using namespace std;
double Div(int num1, int num2)
{
if (num2 == 0)
{
throw "Division by zero condition!";
}
return (num1 / num2);
}
int main ()
{
int p = 25;
int q = 0;
double Res = 0;
try
{
Res = Div(p, q);
cout << Res << endl;
}
catch (const Message)
{
cerr << Message << endl;
}
return 0;
}

A. Compilation Error
B. 25
C. Runtime Error
D. Division by zero condition!
E. None of these
Answer» B. 25
5.

What is the output of this program?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int n = 5;
string str = "Exception Caught: Wrong number used";
try
{
if ( n == 1 )
{
throw 10;
}
if ( n == 2 )
{
throw 3.5f;
}
if ( n != 1 || n != 2 )
{
throw str;
}
}
catch (int p)
{
cout << "Exception value is: " << p << endl;
}
catch (float q)
{
cout << "Exception value is: " << q << endl;
}
catch (string str)
{
cout << str << endl;
}
return 0;
}

A. Exception value is: 10
B. Compilation Error
C. Exception value is: 3.5
D. Exception Caught: Wrong number used
E. None of these
Answer» E. None of these
6.

What is the output of this program?
#include <stdexcept>
#include <limits>
#include <iostream>
using namespace std;
void Function(char ch)
{
if (ch < numeric_limits::max())
return invalid_argument;
}
int main()
{
try
{
Function(50);
}
catch(invalid_argument& excep)
{
cout << excep.what() << endl;
return -1;
}
return 0;
}

A. Invalid argument
B. 50
C. Compilation Error
D. Runtime Error
E. None of these
Answer» D. Runtime Error
7.

What is the output of this program?
#include <iostream>
using namespace std;
void DivFunction(const double num1, const double num2);
int main()
{
double option1=20, option2=5;
try
{
DivFunction(option1, option2);
}
catch (const char* S)
{
cout << "Bad Operator caught: " << S;
}
return 0;
}
void DivFunction(const double num1, const double num2)
{
double Res;
if (num2 == 0)
throw "Division by zero not allowed";
Res = num1 / num2;
cout << Res;
}

A. Bad Operator caught
B. Division by zero not allowed
C. 10
D. 20
E. 4
Answer» F.
8.

What is the output of this program?
#include <iostream>
#include <typeinfo>
using namespace std;
class shapeClass
{
public:
virtual void Virtualfunc() const {}
};
class NewTriangle: public shapeClass
{
public:
virtual void Virtualfunc() const
{
};
};
int main()
{
shapeClass shape_object;
shapeClass &Ref_shape = shape_object;
try
{
NewTriangle &Ref_NewTriangle = dynamic_cast<NewTriangle&>(Ref_shape);
}
catch (bad_cast)
{
cout << "Exception caught by bad_cast";
}
return 0;
}

A. Compilation Error
B. Exception caught by bad_cast
C. Runtime Error
D. Garbage Value
E. None of these
Answer» C. Runtime Error
9.

What is the output of this program?
#include <iostream>
using namespace std;
int main()
{
char* p;
unsigned long int num = (size_t(0) / 3);
cout << num << endl;
try
{
p = new char[size_t(0) / 3];
delete[ ] p;
}
catch(bad_alloc &TheBadAllocation)
{
cout << TheBadAllocation.what() << endl;
};
return 0;
}

A. 5
B. 0
C. depends on compiler
D. bad_alloc
E. None of these
Answer» C. depends on compiler
10.

What is the output of this program?
#include <iostream>
#include <exception>
using namespace std;
int main ()
{
try
{
float* myarray= new float[125];
cout << "Memory Allocated";
}
catch (exception& excep)
{
cout << "Standard exception: " << excep.what() << endl;
}
return 0;
}

A. Memory Allocated
B. Compilation Error
C. Standard exception:
D. Runtime Error
E. None of these
Answer» B. Compilation Error
11.

What is the output of this program?
#include <iostream>
#include <exception>
using namespace std;
class NewExcep: public exception
{
virtual const char* what() const throw()
{
return "New exception occurred...";
}
} NewExcep;
int main ()
{
try
{
throw NewExcep;
}
catch (exception& excep)
{
cout << excep.what() << endl;
}
return 0;
}

A. Exception
B. Compilation Error
C. New exception occurred...
D. Runtime Error
E. None of these
Answer» D. Runtime Error
12.

What is the output of this program?
#include <iostream>
#include <exception>
using namespace std;
class NewException: public exception
{
virtual const char* what() const throw()
{
return "New Exception occurred...";
}
} NewExcep;
int main ()
{
try
{
throw NewExcep;
}
catch (exception& e)
{
cout << e.what() << endl;
}
return 0;
}

A. Compilation Error
B. Runtime Error
C. Garbage value
D. New Exception...
E. None of these
Answer» E. None of these
13.

What is the output of this program?
#include <iostream>
using namespace std;
int main ()
{
try
{
throw 15;
}
catch (int e)
{
cout << "Exception occurred..." << e << endl;
}
return 0;
}

A. 15
B. Exception occurred...
C. Compilation Error
D. Exception occurred...15
E. None of these
Answer» E. None of these
14.

What is the output of this program?
#include <iostream>
using namespace std;
double Div(int num1, int num2)
{
if (num2 == 0)
{
throw "Division by zero condition!";
}
return (num1 / num2);
}
int main ()
{
int p = 40;
int q = 0;
double R = 0;
try
{
R = Div(p, q);
cout << R << endl;
}
catch (const char* Message)
{
cerr << Message << endl;
}
return 0;
}

A. 40
B. Division by zero condition!
C. Compilation Error
D. Runtime Error
E. None of these
Answer» C. Compilation Error
15.

What is the output of this program?
#include <iostream>
using namespace std;
int main()
{
int n1 = 20, n2 = 30, n3 = 60;
float Res;
try
{
if ((n1 - n2) != 0)
{
Res = n3 / (n1 - n2);
cout << Res;
}
else
{
throw(n1 - n2);
}
}
catch (int k)
{
cout< }
}

A. 20
B. 30
C. 60
D. -6
E. None of these
Answer» E. None of these
16.

What is the output of this program?
#include <iostream>
#include <math.h>
using namespace std;
double Function(double n)
{
if (n < 0.0)
throw "Cannot take sqrt of negative number";
return sqrt(n);
}
int main()
{
double n = 16;
cout << Function(n) << endl;
}

A. 0.0
B. 16
C. 4
D. Compilation Error
E. None of these
Answer» D. Compilation Error
17.

What is the output of this program?
#include <iostream>
using namespace std;
int main()
{
char* p;
unsigned long int test = sizeof(size_t(0) / 3);
cout << test << endl;
try
{
p = new char[size_t(0) / 3];
delete[ ] p;
}
catch (bad_alloc &BadAllocation)
{
cout << BadAllocation.what() << endl;
};
return 0;
}

A. 8
B. 4
C. 2
D. 1
E. Depends on compiler
Answer» F.
18.

What is the output of this program?
#include <typeinfo>
#include <iostream>
using namespace std;
class AngleShape
{
public:
virtual void myvirtualfunc() const {}
};
class Triangle: public AngleShape
{
public:
virtual void myvirtualfunc() const
{
};
};
int main()
{
AngleShape Angleshape_instance;
AngleShape &ref_Angleshape = Angleshape_instance;
try
{
Triangle &ref_Triangle = dynamic_cast <Triangle& > (ref_Angleshape);
}
catch (bad_cast)
{
cout << "Can't do the Dynamic_cast" << endl;
cout << "Caught: bad_cast exception. AngleShape is not Triangle. n";
}
return 0;
}

A. Can t able to create the dynamic instance for the triangle, So it is arising an exception
B. Compilation Error
C. Runtime Error
D. Can't do the Dynamic_cast
E. None of these
Answer» B. Compilation Error
19.

What is the output of this program?
#include <iostream>
#include <string>
#include <typeinfo>
using namespace std;
int main( )
{
try
{
string str1("Interview");
string str2("Mania");
str1.append(str2, 6, 3);
cout << str1 << " " << endl;
}
catch (exception &e)
{
cout << "Caught: " << e.what() << endl;
cout << "Type: " << typeid(e).name() << endl;
};
return 0;
}

A. Compilation Error
B. Runtime Error
C. out_of_range
D. Garbage value
E. None of these
Answer» D. Garbage value
20.

What is the output of this program?
#include <iostream>
#include <exception>
#include <typeinfo>
using namespace std;
class Sample
{
virtual int Function()
{
}
};
int main ()
{
try
{
Sample *ptr = NULL;
typeid (*ptr);
}
catch (std::exception& TypeVar)
{
cout << "Exception Occurred: " << TypeVar.what() << endl;
}
return 0;
}

A. Compilation Error
B. Null
C. Exception Occurred: std::bad_alloc
D. Exception Occurred: std::bad_typeid
E. None of these
Answer» E. None of these
21.

What is the output of this program?
#include <iostream>
using namespace std;
void Example() throw()
{
cout << "In empty()" << endl;
}
void WithTypeFunction() throw(int)
{
cout << "Will throw an int" << endl;
throw(1);
}
int main()
{
try
{
Example();
WithTypeFunction();
}
catch (int)
{
cout << "Caught an int" << endl;
}
}

A. Caught an int
B. In empty()
C. Will throw an int
D. All of above
E. None of these
Answer» E. None of these
22.

What is the output of this program?
#include <iostream>
#include <exception>
#include <cstdlib>
using namespace std;
void TerminateFunction()
{
cout << "terminate handler called...";
abort();
}
int main (void)
{
set_terminate (TerminateFunction);
throw 0;
return 0;
}

A. Compilation Error
B. Runtime Error
C. terminate handler called...
D. Aborted
E. None of these
Answer» E. None of these
23.

What is the output of this program?
#include <iostream>
#include <exception>
using namespace std;
void terminatorFunction()
{
cout << "terminate" << endl;
}
void (*old_terminate)() = set_terminate(terminatorFunction);
class BotchClass
{
public:
class Fruits {};
void f()
{
cout << "First" << endl;
throw Fruits();
}
~BotchClass() noexcept(false)
{
throw 'U';
}
};
int main()
{
try
{
BotchClass obj;
obj.f();
}
catch(...)
{
cout << "inside catch(...) block" << endl;
}
}

A. Aborted
B. terminate
C. First
D. inside catch(...) block
E. None of these
Answer» B. terminate
24.

What is the output of this program?
#include <iostream>
using namespace std;
class BaseClass
{
protected:
int num;
public:
BaseClass()
{
num = 25;
}
BaseClass(int k)
{
num = k;
}
virtual ~BaseClass()
{
if (num < 0) throw num;
}
virtual int getA()
{
if (num < 0)
{
throw num;
}
}
};
int main()
{
try
{
BaseClass obj(-23);
cout << endl << obj.getA();
}
catch (int)
{
cout << endl << "Illegal initialization";
}
}

A. Runtime Error
B. Compilation Error
C. Illegal initialization
D. Garbage value
E. None of these
Answer» C. Illegal initialization
25.

What is the output of this program?
#include <iostream>
using namespace std;
#include
#include
void Function()
{
cout << "Function() was called by terminate()." << endl;
exit(0);
}
int main()
{
try
{
set_terminate(Function);
throw "Out of memory!";
}
catch(int)
{
cout << "Integer exception raised..." << endl;
}
return 0;
}

A. Compilation Error
B. Runtime Error
C. Integer exception raised...
D. Function() was called by terminate().
E. None of these
Answer» E. None of these
26.

What is the output of this program?
#include <iostream>
using namespace std;
class TestClassA
{
};
class TestClassB : public TestClassA { };
void Function();
int main()
{
try
{
Function();
}
catch (const TestClassA&)
{
cout << "Caught an exception" << endl;
}
return 0;
}
void Function()
{
throw TestClassB();
}

A. Caught an exception
B. Compilation Error
C. Runtime Error
D. Garbage value
E. None of these
Answer» B. Compilation Error
27.

What is the output of this program?
#include <iostream>
using namespace std;
int main()
{
int num = -2;
char *p;
p = new char[50];
try
{
if (num < 0)
{
throw num;
}
if (p == NULL)
{
throw "p is NULL ";
}
}
catch (...)
{
cout << "Exception occurred: exiting"<< endl;
}
return 0;
}

A. Compilation Error
B. Runtime Error
C. p is NULL
D. Exception occurred: exiting
E. None of these
Answer» E. None of these
28.

What is the output of this program?
#include <iostream>
#include <typeinfo>
using namespace std;
class Poly
{
virtual void Member(){}

};
int main ()
{
try
{
Poly * ptr = 0;
typeid(*ptr);
}
catch (exception& ex)
{
cout << "An Exception caught: " << ex.what() << endl;
}
return 0;
}

A. An Exception caught: std::bad_alloc
B. An Exception caught: std::bad_cast
C. An Exception caught: std::bad_typeid
D. All of above
E. None of these
Answer» D. All of above
29.

What is the output of this program?
#include <iostream>
using namespace std;
int main()
{
int num = -2;
try
{
if (num < 0)
{
throw num;
}
else
{
cout<< num;
}
}
catch (int num )
{
cout << "Exception occurred: Thrown value is " << num << endl;
}
return 0;
}

A. Compilation Error
B. Runtime Error
C. -2
D. Exception occurred: Thrown value is -2
E. None of these
Answer» E. None of these
30.

What is the output of this program?
#include <iostream>
#include <string>
using namespace std;
int main()
{
double Option1 = 12, Option2 = 6, Result;
char Option;
try
{
if (Option != '+' && Option != '-' && Option != '*' && Option != '/')
throw Option;
switch(Option)
{
case '+':
Result = Option1 + Option2;
break;
case '-':
Result = Option1 - Option2;
break;
case '*':
Result = Option1 * Option2;
break;
case '/':
Result = Option1 / Option2;
break;
}
cout << " n" << Option1 << " " << Option << " "<< Option2 << " = " << Result;
}
catch (const char ch)
{
cout << ch << "Is not a valid Option...";
}
return 0;
}

A. Compilation Error
B. Runtime Error
C. Garbage Value
D. Is not a valid Option...
E. None of these
Answer» E. None of these
31.

What is the output of this program?
#include <iostream>
using namespace std;
double DivFunction(int num1, int num2)
{
if ( num2 == 0 )
{
throw "Division by zero condition Occurred...";
}
return (num1 / num2);
}
int main ()
{
int m = 10;
int n = 0;
double Res = 0;
try
{
Res = DivFunction(m, n);
cout << Res << endl;
}
catch (const char* msg)
{
cout << msg << endl;
}
return 0;
}

A. Compilation Error
B. Runtime Error
C. Division by zero condition Occurred...
D. Garbage value
E. None of these
Answer» D. Garbage value
32.

What is the output of this program?
#include <iostream>
using namespace std;
int main()
{
int a = 8;
try
{
if (a < 0)
throw "Positive Number Required";
cout << a << " n n";
}
catch(const char* Msg)
{
cout << "Error: " << Msg;
}
return 0;
}

A. Positive Number Required
B. 8
C. Compilation Error
D. Runtime Error
E. None of these
Answer» C. Compilation Error
33.

What is the output of this program?
#include <iostream>
#include <exception>
using namespace std;
class ExceptionExample: public exception
{
virtual const char* what() const throw()
{
return "Exception Occurred...";
}
} NewExcep;
int main ()
{
try
{
throw NewExcep;
}
catch (exception& excep)
{
cout << excep.what() << endl;
}
return 0;
}

A. Compilation Error
B. Runtime Error
C. Exception Occurred...
D. Garbage value
E. None of these
Answer» D. Garbage value
34.

What is the output of this program?
#include <iostream>
using namespace std;
int main()
{
try
{
throw 13;
}
catch (int ex)
{
cout << "Exception number: " << ex << endl;
return 0;
}
cout << "No any Exception..." << endl;
return 0;
}

A. Exception number: 13
B. 13
C. Compilation Error
D. Runtime Error
E. None of these
Answer» B. 13
35.

What is the output of this program?
#include <iostream>
using namespace std;
int main()
{
double num1 = 9, num2 = 3, Result;
char Operator = '/';
try
{
if (num2 == 0)
throw "Division by zero not allowed";
Result = num1 / num2;
cout << num1 << " / " << num2 << " = " << Result;
}
catch(const char* S)
{
cout << " n Bad Operator: " << S;
}
return 0;
}

A. 9
B. 3
C. 9 / 3 = 3
D. Bad Operator
E. None of these
Answer» D. Bad Operator
36.

What is the output of this program?
#include <iostream>
#include <exception>
using namespace std;
int main ()
{
try
{
int* myarray = new int[150];
cout << "Memory Allocated...";
}
catch (exception& e)
{
cout << "Standard Exception occurred..." << e.what() << endl;
}
return 0;
}

A. Memory Allocated...
B. Compilation Error
C. Runtime Error
D. Garbage Value
E. None of these
Answer» B. Compilation Error
37.

What is the output of this program?
 #include 
#include
using namespace std;
int main()
{
try
{
int * arr1 = new int[100000000];
int * arr2 = new int[100000000];
int * arr3 = new int[100000000];
int * arr4 = new int[100000000];
cout << "Memory Allocated successfully";
}
catch(bad_alloc&)
{
cout << "Error allocating the requested memory." << endl;
}
return 0;
}

A. Depends on the memory of the computer
B. Allocated successfully
C. Error allocating the requested memory
D. All of above
E. None of these
Answer» B. Allocated successfully
38.

What is the output of this program?
#include 
using namespace std;
void function();
int main()
{
try
{
function();
}
catch(double)
{
cout << "caught a double type exception..." << endl;
}
return 0;
}
void function()
{
throw 5;
}

A. abnormal program termination
B. caught a double type
C. Compilation Error
D. Runtime Error
E. None of these
Answer» B. caught a double type
39.

What is the output of this program?
#include 
using namespace std;
int main()
{
char* buffer;
try
{
buffer = new char[1024];
if (buffer == 0)
throw "Memory allocation failure!";
else
cout << sizeof(buffer) <
}
catch(char *strg)
{
cout< }
return 0;
}

A. Depends on the size of the data type
B. 4 Bytes allocated successfully
C. 8 Byte successfully allocated!
D. Memory allocation failure
E. None of these
Answer» B. 4 Bytes allocated successfully
40.

What is the output of this program?
#include 
using namespace std;
double function(int p, int q)
{
if (q == 0)
{
throw "Division by zero condition!";
}
return (p / q);
}
int main ()
{
int m = 100;
int n = 20;
double Res = 0;
try
{
Res = function(m, n);
cout << Res;
}
catch(const char *message)
{
cout << message;
}
return 0;
}

A. 4
B. 20
C. 100
D. Compilation Error
E. None of these
Answer» B. 20
41.

What is the output of this program?
#include 
using namespace std;
void SequenceFun(int StopNumber)
{
int N;
N = 1;
while (true)
{
if (N >= StopNumber)
throw N;
cout << N;
N++;
}
}
int main(void)
{
try
{
SequenceFun(5);
}
catch(int ExNumber)
{
cout < cout << "Exception occur with value: " << ExNumber;
}
return 0;
}

A. Runtime Error
B. Compilation Error
C. prints first 4 numbers
D. prints first 4 numbers and throws exception at 5
E. None of these
Answer» E. None of these
42.

What is the output of this program?
#include 
using namespace std;
int main()
{
int num = 20;
try
{
if (num != 20)
{
throw "Positive Number Required";
}
cout << num;
}
catch(const char *Msg)
{
cout << "Error: " << Msg;
}
return 0;
}

A. Compilation Error
B. Runtime Error
C. Garbage value
D. error:Positive Number Required
E. None of these
Answer» E. None of these
43.

When exceptions are used?

A. Exceptions are used when postconditions of a function cannot be satisfied
B. Exceptions are used when postconditions of a function can be satisfied
C. To preserve the program
D. All of above
E. None of these
Answer» C. To preserve the program
44.

Pick out the correct Answer.

A. Exception are suitable for critical points in code
B. Exceptions are not suitable for critical points in code
C. Exception are suitable for One points in code
D. All of above
E. None of these
Answer» C. Exception are suitable for One points in code
45.

How many parameters does the throw expression can have?

A. 4
B. 3
C. 2
D. 1
E. None of these
Answer» E. None of these
46.

Which is used to handle the exceptions in c++?

A. exception handler
B. catch handler
C. handler
D. All of above
E. None of these
Answer» B. catch handler
47.

What should present when throwing a object?

A. destructor
B. constructor
C. copy-constructor
D. All of above
E. None of these
Answer» D. All of above
48.

How to handle the exception in constructor?

A. Function
B. We have to throw an exception
C. We have to return the exception
D. We have to throw an exception & return the exception
E. None of these
Answer» C. We have to return the exception
49.

What will happen when an exception is uncaught?

A. execute continuously
B. arise an error
C. program will run
D. All of above
E. None of these
Answer» C. program will run
50.

Which operations don t throw anything?

A. Operations which are irreversible
B. Operations which are dynamic
C. Operations which are reversible
D. Operations which are static
E. None of these
Answer» B. Operations which are dynamic