My Report

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 an error in C++?

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

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 < 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;
	}
		
}

4. Why do we need to handle exceptions?

5. 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;
	}
}

6. 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;
	}
}

7. Which keyword is used to throw an exception?

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

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

10. What is the difference between error and exception?

11. What are the different types of exceptions?

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

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

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

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


 

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

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.