Here is a listing of C++ Programming Questions & Answers focuses on “Exceptions” along with answers, explanations and/or solutions:
1. To where does the program control transfers when the exception is arisen?
a) catch
b) handlers
c) throw
d) try
View Answer
Explanation: When an exception is arisen mean, the exception is caught by handlers and then it decides the type of exception.
2. Which keyword is used to check exception in the block of code?
a) catch
b) throw
c) try
d) handlers
View Answer
Explanation: The try() statement is used for exceptions in c++.
3. What will happen when the exception is not caught in the program?
a) error
b) program will execute
c) block of that code will not execute
d) program will execute & displays wrong output
View Answer
Explanation: When exceptions are not caught in any program then program throws error.
4. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int age = 0;
try
{
if (age < 0)
{
throw "Positive Number Required";
}
cout << age;
}
catch(const char *Message)
{
cout << "Error: " << Message;
}
return 0;
}
a) 0
b) error:Positive Number Required
c) compile time error
d) runtime error
View Answer
Explanation: As the zero marks the beginning of the positive number, it is printed as output
Output:
$ g++ excep.cpp $ a.out 0
5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void PrintSequence(int StopNum)
{
int Num;
Num = 1;
while (true)
{
if (Num >= StopNum)
throw Num;
cout << Num;
Num++;
}
}
int main(void)
{
try
{
PrintSequence(20);
}
catch(int ExNum)
{
cout << "Caught an exception with value: " << ExNum;
}
return 0;
}
a) compile time error
b) prints first 19 numbers
c) prints first 19 numbers and throws exception at 20
d) prints first 17 numbers
View Answer
Explanation: In this program, we are printing upto 19 numbers and when executing the 20, we are raising a exception.
Output:
$ g++ excep1.cpp $ a.out 12345678910111213141516171819Caught an exception with value: 20
6. 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 = 2;
double z = 0;
try
{
z = division(x, y);
cout << z;
}
catch(const char *msg)
{
cerr << msg;
}
return 0;
}
a) 25
b) 20
c) Division by zero condition!
d) 35
View Answer
Explanation: In this program, we resembling the division by using the exception handling.
Output:
$ g++ excep2.cpp $ a.out 25
7. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
char* buff;
try
{
buff = new char[1024];
if (buff == 0)
throw "Memory allocation failure!";
else
cout << sizeof(buff) << "Byte successfully allocated!"<<endl;
}
catch(char *strg)
{
cout<<"Exception raised: "<<strg<<endl;
}
return 0;
}
a) 4 Bytes allocated successfully
b) 8 Bytes allocated successfully
c) Memory allocation failure
d) Depends on the size of the data type
View Answer
Explanation: As we are allocating the memory to the variables and if there are not sufficient size means, it will throw an exception.
Output:
$ g++ excep3.cpp $ a.out 4 Bytes allocated successfully
8. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void Funct();
int main()
{
try
{
Funct();
}
catch(double)
{
cerr << "caught a double type..." << endl;
}
return 0;
}
void Funct()
{
throw 3;
}
a) caught a double type
b) compile time error
c) abnormal program termination
d) runtime error
View Answer
Explanation: As we are throwing integer to double it will raise as abnormal program after termination throw statement.
Output:
$ g++ excep4.cpp $ a.out terminate called after throwing an instance of 'int' Aborted
9. What will be the output of the following C++ code?
#include <iostream>
#include <exception>
using namespace std;
int main()
{
try
{
int * array1 = new int[100000000];
int * array2 = new int[100000000];
int * array3 = new int[100000000];
int * array4 = new int[100000000];
cout << "Allocated successfully";
}
catch(bad_alloc&)
{
cout << "Error allocating the requested memory." << endl;
}
return 0;
}
a) Allocated successfully
b) Error allocating the requested memory
c) Depends on the memory of the computer
d) Error
View Answer
Explanation: In this program, we allocating the memory to the arrays by using exception handling and we handled the exception by standard exception.
Output:
$ g++ excep5.cpp $ a.out Allocated successfully
10. What will happen when the handler is not found for an exception?
a) calls the standard library function terminate()
b) raise an error
c) executes the remaining block
d) raise an error and executes the remaining block
View Answer
Explanation: None.
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]
- Practice Computer Science MCQs
- Practice Programming MCQs
- Check Computer Science Books
- Check C++ Books
- Check Programming Books