C++ Programming Questions and Answers – Exceptions

This section on C++ Programming Multiple Choice Questions focuses on “Exceptions”. One shall practice these questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ questions comes with the detailed explanation of the answers which helps in better understanding of C++ concepts.

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

Answer: b
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

Answer: c
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

Answer: a
Explanation: When exceptions are not caught in any program then program throws error.
advertisement
advertisement

4. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int age = 0;
  6.         try 
  7.         {
  8.             if (age < 0) 
  9.             {
  10.                 throw "Positive Number Required";
  11.             }
  12.             cout << age;
  13.         }
  14.         catch(const char *Message)
  15.         {
  16.             cout << "Error: " << Message;
  17.         }
  18.         return 0;
  19.     }

a) 0
b) error:Positive Number Required
c) compile time error
d) runtime error
View Answer

Answer: a
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?

advertisement
  1.     #include <iostream>
  2.     using namespace std;
  3.     void PrintSequence(int StopNum)
  4.     {
  5.         int Num;
  6.         Num = 1;
  7.         while (true) 
  8.         {
  9.             if (Num >= StopNum)
  10.                 throw Num;
  11.             cout << Num;
  12.             Num++;
  13.         }
  14.     }
  15.     int main(void)
  16.     {
  17.         try 
  18.         {
  19.             PrintSequence(20);
  20.         }
  21.         catch(int ExNum)
  22.         {
  23.             cout << "Caught an exception with value: " << ExNum;
  24.         }
  25.         return 0;
  26.     }

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

Answer: c
Explanation: In this program, we are printing upto 19 numbers and when executing the 20, we are raising a exception.
Output:

advertisement
$ g++ excep1.cpp
$ a.out
12345678910111213141516171819Caught an exception with value: 20

6. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     using namespace std;
  3.     double division(int a, int b)
  4.     {
  5.         if (b == 0) 
  6.         {
  7.             throw "Division by zero condition!";
  8.         }
  9.         return (a / b);
  10.     }
  11.     int main ()
  12.     {
  13.         int x = 50;
  14.         int y = 2;
  15.         double z = 0;
  16.         try 
  17.         {
  18.             z = division(x, y);
  19.             cout << z;
  20.         }
  21.         catch(const char *msg) 
  22.         {
  23.             cerr << msg;
  24.         }
  25.         return 0;
  26.     }

a) 25
b) 20
c) Division by zero condition!
d) 35
View Answer

Answer: a
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?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         char* buff;
  6.         try 
  7.         {
  8.             buff = new char[1024];
  9.             if (buff == 0)
  10.                throw "Memory allocation failure!";
  11.             else
  12.                cout << sizeof(buff) << "Byte successfully allocated!"<<endl;
  13.         }
  14.         catch(char *strg)
  15.         {
  16.             cout<<"Exception raised: "<<strg<<endl;
  17.         }
  18.         return 0;
  19.     }

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

Answer: d
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?

  1.     #include <iostream>
  2.     using namespace std;
  3.     void Funct();
  4.     int main()
  5.     {
  6.         try 
  7.         {
  8.             Funct();
  9.         }
  10.         catch(double) 
  11.         {
  12.             cerr << "caught a double type..." << endl;
  13.         }
  14.         return 0;
  15.     }
  16.     void Funct()
  17.     {
  18.         throw 3;
  19.     }

a) caught a double type
b) compile time error
c) abnormal program termination
d) runtime error
View Answer

Answer: c
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?

  1.     #include <iostream>
  2.     #include <exception>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         try 
  7.         {
  8.             int * array1 = new int[100000000];
  9.             int * array2 = new int[100000000];
  10.             int * array3 = new int[100000000];
  11.             int * array4 = new int[100000000];
  12.             cout << "Allocated successfully";
  13.         }
  14.         catch(bad_alloc&) 
  15.         {
  16.             cout << "Error allocating the requested memory." << endl;
  17.         }
  18.         return 0;
  19.     }

a) Allocated successfully
b) Error allocating the requested memory
c) Depends on the memory of the computer
d) Error
View Answer

Answer: c
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

Answer: a
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]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.