My Report

C++ Exception Test – 3


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 will be the output of the following C++ code?

  
    #include <iostream>
    using namespace std;
    void test(int x)
    {
        try
        {
            if (x > 0)
                throw x;
            else
                throw 'x';
        }
        catch(char)
        {
            cout << "Catch a integer and that integer is:" << x;
        }
    }
    int main()
    {
        cout << "Testing multiple catches\n:";
        test(10);
        test(0);
    }

2. What will happen when we move to try block far away from catch block?

3. Why is it expensive to use objects for the exception?

4. What operation can be performed by destructor?

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

    #include <iostream>
    #include <string>
    using namespace std;
    int main ()
    {
        int num = 3;
        string str_bad = "wrong number used";
        try
        {
            if ( num == 1 )
            {       
                throw 5;
            }
            if ( num == 2 )
            {
                throw 1.1f;
            }
            if ( num != 1 || num != 2 )
            {    
                throw str_bad;
            }
        }
        catch (int a)
        {
            cout << "Exception is: " << a << endl;
        }
        catch (float b)
        {
            cout << "Exception is: " << b << endl;
        }
        catch (...)
        {
            cout  << str_bad << endl;
        }
        return 0;
    }

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

    #include <iostream>
    #include <exception>
    using namespace std;
    int main () 
    {
        try
        {
            double* i= new double[1000];
            cout << "Memory allocated";
        }
        catch (exception& e)
        {
            cout << "Exception arised: " << e.what() << endl;
        }
        return 0;
    }

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

    #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 msg) 
        {
            cerr << msg << endl;
        }
        return 0;
    }

8. What will happen if an exception that is thrown may cause a whole load of objects to go out of scope?

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

    #include <stdexcept>
    #include <limits>
    #include <iostream>
    using namespace std;
    void func(int c)
    {
        if (c < numeric_limits<char> :: max())
            throw invalid_argument("MyFunc argument too large.");
        else
        {
            cout<<"Executed";
        }
    }
    int main()
    {
        try
        {
            func(256);
        }
        catch(invalid_argument& e)
        {
            cerr << e.what() << endl;
            return -1;
        }
        return 0;
    }

10. What is the main purpose of the constructor?


 

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.