My Report

C++ Exception Test – 2


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 empty() throw() 
    {
        cout << "In empty()";
    }
    void with_type() throw(int) 
    {
        cout << "Will throw an int";
        throw(1);
    }
    int main() 
    {
        try 
        {
            empty();
            with_type();
        }
        catch (int) 
        {
            cout << "Caught an int";
        }
    }

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

  
    #include <iostream>
    using namespace std;
    int main()
    {
        char* ptr;
        unsigned long int Test = sizeof(size_t(0) / 3);
        cout << Test << endl;
        try
        {
            ptr = new char[size_t(0) / 3];
            delete[ ] ptr;
        }
        catch (bad_alloc &thebadallocation)
        {
            cout << thebadallocation.what() << endl;
        };
        return 0;
    }

3. What is meant by exception specification?

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

  
    #include <iostream>
    #include <string>
    #include<typeinfo>
    using namespace std;
    int main( )
    {
        try
        {
            string strg1("Test");
            string strg2("ing");
            strg1.append(strg2, 4, 2);
            cout << strg1 << endl;
        }
        catch (exception &e)
        {
            cout << "Caught: " << e.what() << endl;
            cout << "Type: " << typeid(e).name() << endl;
        };
        return 0;
    }

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

  
    #include <typeinfo>
    #include <iostream>
    using namespace std;
    class Myshape
    {
        public:
        virtual void myvirtualfunc() const {}
    };
    class mytriangle: public Myshape
    {
        public:
        virtual void myvirtualfunc() const
        {   
        };
    };
    int main()
    {
        Myshape Myshape_instance;
        Myshape &ref_Myshape = Myshape_instance;
        try 
        {
            mytriangle &ref_mytriangle = dynamic_cast<mytriangle&>(ref_Myshape);
        }
        catch (bad_cast)
        {
            cout << "Can't do the dynamic_cast lor!!!" << endl;
            cout << "Caught: bad_cast exception. Myshape is not mytriangle.\n";
        }
        return 0;
    }

6. What will happen when a programs throws any other type of exception other than specified?

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

  
    #include <iostream>
    #include <exception>
    #include <typeinfo>
    using namespace std;
    class Test1
    {    
        virtual int  Funct() 
        {
        }
    };
    int main ()
    {
        try 
        {
            Test1 * var = NULL;
            typeid (*var);
        }
        catch (std::exception& typevar)
        {
            cout << "Exception: " << typevar.what() << endl;   
        }
        return 0;
    }

8. What do you mean by "No exception specification"?

9. Which operations don't throw anything?

10. Identify the correct statement about throw(type).


 

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.