My Report

C++ Programming Practice Test 6


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 id the syntax for catching any type of exceptions?

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

  
    #include <iostream>
    using namespace std;
    class student
    {
        public:
        int rno , m1 , m2 ;
        void get()
        {
            rno = 15, m1 = 10, m2 = 10;
        }
    };
    class sports
    {
        public:
        int sm;
        void getsm()
        {
            sm = 10;
        }
    };
    class statement:public student,public sports
    {
        int tot,avg;
        public:
        void display()
        {
            tot = (m1 + m2 + sm);
            avg = tot / 3;
            cout << tot;
            cout << avg;
        }
    };
    int main()
    {
        statement obj;
        obj.get();
        obj.getsm();
        obj.display();
    }

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

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class A
{
	int a;
    public:
	A(){}
};
class B: public A
{
	int b;
    public:
	B(){}
};

void func()
{
	B b;
	throw b;
}
int main()
{
	try{
		func();
	}
	catch(B *b){
		cout<<"Caught B Class\n";
	}
	catch(A a){
		cout<<"Caught A Class\n";
	}
}

4. Exception handlers are declared with ____________ keyword.

5. How many specifiers are used to derive a class?

6. What happens when this C++ program is compiled?

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class A
{
	int a;
    public:
	A(){}
};

class B: public A
{
	int b;
    public:
	B(){}
};

void func()
{
	B b;
	throw b;
}

int main()
{
	try{
		func();
	}
	catch(A a){
		cout<<"Caught A Class\n";
	}
	catch(B b){
		cout<<"Caught B Class\n";
	}
}

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

  
    #include <iostream>
    using namespace std;
    class Test1 
    { 
    };
    class Test2 : public Test1 { };
    void Funct();
    int main()
    {
        try
        {
            Funct();
        }
        catch (const Test1&)
        {
            cerr << "Caught a exception" << endl;
        }
        return 0;
    }
    void Funct()
    {
        throw Test2();
    }

8. When do we call that resource is leaked?

9. In nested try catch blocks, if both inner and outer catch blocks are unable to handle the exception thrown, then ______________

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

  
    #include <iostream>
    using namespace std;
    #include <cstdlib>
    #include <exception>
    void Funct()
    {
        cout << "Funct() was called by terminate()." << endl;
        exit(0);
    }
    int main()
    {
        try
        {
            set_terminate(Funct);
            throw "Out of memory!";
        }
        catch(int)
        { 
            cout << "Integer exception raised." << endl; 
        }
        return 0;
    }

 

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.