C++ Programming Question and Answers – Exceptions

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,, 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.

C++ Programming Questions & Answers focuses on “Exceptions” along with answers, explanations and/or solutions:

Q 1. To where does the program control transfers when the exception is arisen?
A. handlers
B. catch
C. throw
D. try

Show Answer Answer:-A. handlers
Explanation When an exception is arisen mean, the exception is caught by handlers and then it decides the type of exception.

Q 2. Which keyword is used to check exception in the block of code?
A. catch
B. throw
C. try
D. handlers

Show Answer Answer:-C. try
Explanation The try() statement is used for exceptions in c++.

Q 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

Show Answer Answer:-A. error
Explanation When exceptions are not caught in any program then program throws error.

Q 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

Show Answer Answer:-A. 0
Explanation As the zero marks the beginning of the positive number, it is printed as output Output: $ g++ excep.cpp $ a.out 0

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

  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 17 numbers
D. prints first 19 numbers and throws exception at 20

Show Answer Answer:-D. prints first 19 numbers and throws exception at 20
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

Q 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. 20
B. 25
C. Division by zero condition!
D. 35

Show Answer Answer:-B. 25
Explanation In this program, we resembling the division by using the exception handling. Output: $ g++ excep2.cpp $ a.out 25

Q 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

Show Answer Answer:-D. Depends on the size of the data type
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

Q 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. runtime error
D. abnormal program termination

Show Answer Answer:-D. abnormal program termination
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

Q 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

Show Answer Answer:-C. Depends on the memory of the computer
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

Q 10. What will happen when the handler is not found for an exception?
A. executes the remaining block
B. raise an error
C. calls the standard library function terminate()
D. raise an error and executes the remaining block

Show Answer Answer:-C. calls the standard library function terminate()
Explanation None.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page