Exceptions and Efficiency

1

Why is it expensive to use objects for exception?

A. Exception object is created only if an error actually happens
B. Because of execution time
C. Memory space involved in creating an exception object
D. None of the mentioned

2

What is the main purpose of the constructor?

A. Begin the execution of the class
B. Include the macros for the program
C. Establish the class invariant
D. None of the mentioned

3

What is the output of this program?

1.
    #include 
2.
    using namespace std;
3.
    double division(int a, int b)
4.
    {
5.
        if (b == 0)
6.
        {
7.
            throw "Division by zero condition!";
8.
        }
9.
        return (a / b);
10.
    }
11.
    int main ()
12.
    {
13.
        int x = 50;
14.
        int y = 0;
15.
        double z = 0;
16.
        try 
17.
        {
18.
            z = division(x, y);
19.
            cout << z << endl;
20.
        }
21.
        catch (const msg) 
22.
        {
23.
            cerr << msg << endl;
24.
        }
25.
        return 0;
26.
    }
A. 50
B. 0
C. Division by zero condition
D. Error

4

What is the output of this program?

1.
    #include 
2.
    #include 
3.
    using namespace std;
4.
    int main ()
5.
    {
6.
        int num = 3;
7.
        string str_bad = "wrong number used";
8.
        try
9.
        {
10.
            if ( num == 1 )
11.
            {       
12.
                throw 5;
13.
            }
14.
            if ( num == 2 )
15.
            {
16.
                throw 1.1f;
17.
            }
18.
            if ( num != 1 || num != 2 )
19.
            {    
20.
                throw str_bad;
21.
            }
22.
        }
23.
        catch (int a)
24.
        {
25.
            cout << "Exception is: " << a << endl;
26.
        }
27.
        catch (float b)
28.
        {
29.
            cout << "Exception is: " << b << endl;
30.
        }
31.
        catch (...)
32.
        {
33.
            cout  << str_bad << endl;
34.
        }
35.
        return 0;
36.
    }
A. Exception is 5
B. Exception is 1.1f
C. wrong number used
D. None of the mentioned

5

What is the output of this program?

1.
    #include 
2.
    #include 
3.
    #include 
4.
    using namespace std;
5.
    void func(int c)
6.
    {
7.
        if (c < numeric_limits :: max())
8.
            throw invalid_argument("MyFunc argument too large.");
9.
        else
10.
        {
11.
            cout<<"Executed";
12.
        }
13.
    }
14.
    int main()
15.
    {
16.
        try
17.
        {
18.
            func(256);
19.
        }
20.
        catch(invalid_argument& e)
21.
        {
22.
            cerr << e.what() << endl;
23.
            return -1;
24.
        }
25.
        return 0;
26.
    }
A. Invalid arguments
B. Executed
C. Error
D. Runtime error