My Report (&Account)

C++ Exception Handling Test – 1


Correct Answer: 2 points | Wrong: -1 point
Grades: A* (100% score) | A (80%-99%) | B (60%-80%) | C (40%-60%) | D (0%-40%)
advertisement

1. What is the difference between error and exception?

2. Which part of the try-catch block is always fully executed?

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

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
void func(int a, int b)
{
	if(b == 0){
		throw "This value of b will make the product zero. " 
                       "So please provide positive values.\n";
	}
	else{
		cout<<"Product of "<<a<<" and  "<<b<<" is: "<<a*b<<endl;
	}
}

int main()
{
	try{
		func(5,0);
	}
	catch(char* e){
		cout<<e;
	}
}

4. What is the correct syntax of the try-catch block?

5. Why do we need to handle exceptions?

6. Which of the following is an exception in C++?

7. How Exception handling is implemented in the C++ program?

8. What are the different types of exceptions?

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

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
void func(int a, int b)
{
	if(b < 1){
		throw b;
	}
	else{
		cout<<"Product of "<<a<<" and  "<<b<<" is: "<<a*b<<endl;
	}
}

int main()
{
	try
        {
		try
                {			
		    func(5,-1);
		}
		catch(int b)
                {
			if(b==0)
				throw "value of b is zero\n";
			else
				throw "value of b is less than zero\n";
		}
	}
	catch(const char* e)
        {
		cout<<e;
	}
		
}

10. By default, what a program does when it detects an exception?

11. What is an exception in C++ program?

12. Which keyword is used to throw an exception?

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

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

void func(int a, int b)
{

	if(b == 0){
		throw "This value of b will make the product zero. " 
                      "So please provide positive values.\n";
	}
	else{
		cout<<"Product of "<<a<<" and  "<<b<<" is: "<<a*b<<endl;
	}
}

int main()
{
	try{
		func(5,0);
	}
	catch(const char* e){
		cout<<e;
	}
}

14. What is an error in C++?

15. What is Re-throwing an exception means in C++?


 

Start practicing “1000 MCQs on C++”, and once you are ready, you can take tests on all topics by attempting our “C++ Test Series”.

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