My Report

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

    #include <iostream>
    using namespace std;
    void PrintSequence(int StopNum)
    {
        int Num;
        Num = 1;
        while (true)
        {
            if (Num >= StopNum)
                throw Num;
            cout << Num << endl;
            Num++;
        }
    }
    int main(void)
    {
        try
        {
            PrintSequence(2);
        }
        catch(int ExNum)
        {
            cout << "exception: " << ExNum << endl;
        }
        return 0;
    }

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

    #include <iostream>
    using namespace std;
    int main()
    {
        int a = 10, b = 20, c = 30;
        float  d;
        try
        {
            if ((a - b) != 0)
            {
                d = c / (a - b);
                cout << d;
            }
            else
            {
                throw(a - b);
            }
        }
        catch (int i)
        {
            cout<<"Answer is infinite "<<i;
        }
    } 

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

    #include <iostream>
    using namespace std;
    int main()
    {
        try
        {
            throw 1;
        }
        catch (int a)
        {
            cout << "exception number:  " << a << endl;
            return 0;
        }
        cout << "No exception " << endl;
        return 0;
    } 

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

    #include <iostream>
    using namespace std;
    int main()
    {
        double a = 10, b = 5, res;
        char Operator = '/';
        try 
        {
            if (b == 0)
                throw "Division by zero not allowed";
            res = a / b;
            cout << a << " / " << b << " = " << res;
        }
        catch(const char* Str)
        {
            cout << "\n Bad Operator: " << Str;
        }
        return 0;
    }

5. How many types of exception handling are there in c++?

6. When exceptions are used?

7. Pick out the correct answer.

8. 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(int x)
        {
            cout<<"integer:"<<x;
        }
        catch(char x)
        {
            cout << "character:" << x;
        }
    }
    int main()
    {
        test(10);
        test(0);
    } 

9. Which block should be placed after try block?

10. How many runtime error messages associated with exception?


 

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.