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. handlersExplanation
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. tryExplanation
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. errorExplanation
When exceptions are not caught in any program then program throws error.Q 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
Show Answer
Answer:-A. 0Explanation
As the zero marks the beginning of the positive number, it is printed as output Output: $ g++ excep.cpp $ a.out 0Q 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 17 numbers
D. prints first 19 numbers and throws exception at 20
Show Answer
Answer:-D. prints first 19 numbers and throws exception at 20Explanation
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: 20Q 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. 20
B. 25
C. Division by zero condition!
D. 35
Show Answer
Answer:-B. 25Explanation
In this program, we resembling the division by using the exception handling. Output: $ g++ excep2.cpp $ a.out 25Q 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
Show Answer
Answer:-D. Depends on the size of the data typeExplanation
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 successfullyQ 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. runtime error
D. abnormal program termination
Show Answer
Answer:-D. abnormal program terminationExplanation
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’ AbortedQ 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
Show Answer
Answer:-C. Depends on the memory of the computerExplanation
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 successfullyQ 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
Leave a Reply