github

Made bySaurav Hathi

MCQ Test C/C++: Error and Exception Handling

Q1:

To where does the program control transfer when an exception is raised in C++?

Tags:
Section:
Core Programming
Options:
Q2:

Which keyword is used to check exception in the block of code in C++?

Tags:
Section:
Core Programming
Options:
Q3:

What will happen when the exception is not caught in the program in C++?

Tags:
Section:
Core Programming
Options:
Q4:

What is the output of this C++ program?

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

}

Tags:
Section:
Core Programming
Options:
Q5:

What is the output of this C++ program?

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

}

Tags:
Section:
Core Programming
Options:
Q6:

What is the output of this C++ program?

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

}

Tags:
Section:
Core Programming
Options:
Q7:

What is the output of this C++ program?

#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) << " Bytes allocated successfully"<<endl;

}

catch(char *strg)

{

cout<<"Exception raised: "<<strg<<endl;

}

return 0;

}

Tags:
Section:
Core Programming
Options:
Q8:

What is the output of this C++ program?

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

}

Tags:
Section:
Core Programming
Options:
Q9:

What is the output of this C++ program?

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

}

Tags:
Section:
Core Programming
Options:
Q10:

In C++, what will happen if the handler is not found for exception?

Tags:
Section:
Core Programming
Options:
Q11:

In C++, which keyword is used to handle the expection?

Tags:
Section:
Core Programming
Options:
Q12:

In C++, which keyword is used to throw an exception?

Tags:
Section:
Core Programming
Options:
Q13:

What is the output of this C++ program?

#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 = 0;

double z = 0;

try

{

z = division(x, y);

cout << z << endl;

}

catch (const char* msg)

{

cerr << msg << endl;

}

return 0;

}

Tags:
Section:
Core Programming
Options:
Q14:

What is the output of this C++ program?

#include <iostream>

using namespace std;

int main ()

{

try

{

throw 20;

}

catch (int e)

{

cout << "An exception occurred " << e << endl;

}

return 0;

}

Tags:
Section:
Core Programming
Options:
Q15:

What is the output of this C++ program?

#include <iostream>

#include <exception>

using namespace std;

class myexception: public exception

{

virtual const char* what() const throw()

{

return "My exception";

}

} myex;

int main ()

{

try

{

throw myex;

}

catch (exception& e)

{

cout << e.what() << endl;

}

return 0;

}

Tags:
Section:
Core Programming
Options:
Q16:

What is the output of this C++ program?

#include <iostream>

#include <exception>

using namespace std;

int main ()

{

try

{

int* myarray = new int[1000];

cout << "Allocated";

}

catch (exception& e)

{

cout << "Standard exception: " << e.what() << endl;

}

return 0;

}

Tags:
Section:
Core Programming
Options:
Q17:

What is the output of this C++ program?

#include <iostream>

#include <exception>

using namespace std;

struct MyException : public exception

{

const char * what () const throw ()

{

return "C++ Exception";

}

};

int main()

{

try

{

throw MyException();

}

catch(MyException& e)

{

cout << "Exception caught" << std::endl;

cout << e.what() << std::endl;

}

catch(std::exception& e)

{

}

}

Tags:
Section:
Core Programming
Options:
Q18:

How do we define the user-defined exceptions in C++?

Tags:
Section:
Core Programming
Options:
Q19:

In C++, which exception is thrown by dynamic_cast?

Tags:
Section:
Core Programming
Options:
Q20:

In C++, how many parameters does the throw expression can have?

Tags:
Section:
Core Programming
Options:
Q21:

Where are the exceptions handled in C++?

Tags:
Section:
Core Programming
Options:
Q22:

What is the output of this C++ program?

#include <iostream>

#include <exception>

using namespace std;

class myexception: public exception

{

virtual const char* what() const throw()

{

return "Exception arised";

}

} myex;

int main ()

{

try

{

throw myex;

}

catch (exception& e)

{

cout << e.what() << endl;

}

return 0;

}

Tags:
Section:
Core Programming
Options:
Q23:

What is the output of this C++ program?

#include <iostream>

using namespace std;

int main()

{

int age=5;

try

{

 if (age < 0)

throw "Positive Number Required";

cout  << age << "\n\n";

}

catch(const char* Message)

{

cout << "Error: " << Message;

}

return 0;

}

Tags:
Section:
Core Programming
Options:
Q24:

What is the output of this C++ program?

#include <iostream>

#include <string>

using namespace std;

int main()

{

double Op1 = 10, Op2 = 5, Res;

char Op;

try

{  

if (Op != '+' && Op != '-' && Op != '*' && Op != '/')

throw Op;

switch(Op)

{

case '+':

Res = Op1 + Op2;

break;

case '-':

Res = Op1 - Op2;

break;

case '*':

Res = Op1 * Op2;

break;

case '/':

Res = Op1 / Op2;

break;

}

cout << "\n" << Op1 << " " << Op << " "<< Op2 << " = " << Res;

}

catch (const char n)

{

cout << n << " is not a valid operator";

}

return 0;

}

Tags:
Section:
Core Programming
Options:
Q25:

What is the output of this C++ program?

#include<iostream>

#include "math.h"

using namespace std;

double MySqrt(double d)

{

if (d < 0.0)

throw "Cannot take sqrt of negative number";    

return sqrt(d);

}

int main()

{

double d = 5;

cout << MySqrt(d) << endl;

}

Tags:
Section:
Core Programming
Options: