Vidyalelo
C++ Programming · all questions

Exception Handling in C++
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

68

Questions

3/4

Page

Pick an option on a question to see the right answer and solution.

Which keyword is used to specify that a function will throw any exceptions in C++?

Select an option to see the answer and solution.

What is the purpose of the 'std::unexpected' function in C++ exception handling?

Select an option to see the answer and solution.

Which statement about exception handling in C++ is true?

Select an option to see the answer and solution.

What is the behavior of the 'std::terminate' function in C++?

Select an option to see the answer and solution.

Uncaught exception leads to . . . . . . . .

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

What is an error in C++?

Select an option to see the answer and solution.

What are the different types of exceptions?

Select an option to see the answer and solution.

Why do we need to handle exceptions?

Select an option to see the answer and solution.

Which keyword is used to throw an exception?

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

Header file used for exception handling in C++?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class A
{
	int a;
   public:
	A(){}
};
 
class B: public A
{
	int b;
    public:
	B(){}
};
 
void func()
{
	B b;
	throw b;
}
 
int main()
{
	try{
		func();
	}
	catch(B b){
		cout<<"Caught B Class\n";
	}
	catch(A a){
		cout<<"Caught A Class\n";
	}
}

Select an option to see the answer and solution.

Where should we place catch block of the derived class in a try-catch block?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class A
{
	int a;
    public:
	A(){}
};
 
class B: public A
{
	int b;
    public:
	B(){}
};
 
void func1()
{
	B b;
	throw b;
}
 
void func2()
{
	A a;
	throw a;
}
 
int main()
{
	try{
		func1();
	}
	catch(...){
		cout<<"Caught All types of exceptions\n";
	}
	try{
		func2();
	}
	catch(B b){
		cout<<"Caught All types of exceptions\n";
	}
}

Select an option to see the answer and solution.

An uncaught handler returns to . . . . . . . .

Select an option to see the answer and solution.

What is an exception in C++ program?

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.