My Report

C++ Inheritance 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. Which of the following advantages we lose by using multiple inheritances?

2. Which symbol is used to create multiple inheritances?

3. Which design patterns benefit from the multiple inheritances?

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

  
    #include <iostream>
    using namespace std;
    class polygon 
    {
        protected:
        int width, height;
        public:
        void set_values (int a, int b)
        { 
            width = a; height = b;}
        };
        class output1 
        {
            public:
                void output (int i);
        };
    void output1::output (int i) 
    {
        cout << i << endl;
    }
    class rectangle: public polygon, public output1 
    {
        public:
        int area ()
        { 
            return (width * height); 
        }
    };
    class triangle: public polygon, public output1 
    {
        public:
        int area ()
        {
            return (width * height / 2); 
        }
    };
    int main () 
    {
        rectangle rect;
        triangle trgl;
        rect.set_values (4, 5);
        trgl.set_values (4, 5);
        rect.output (rect.area());
        trgl.output (trgl.area());
        return 0;
    }

5. What are the things are inherited from the base class?

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

  
    #include <iostream>
    using namespace std;
    class Base1
    {
        protected:
        int SampleDataOne;
        public:
        Base1()
        {
            SampleDataOne = 100;
        }
        ~Base1()
        {
        }
        int SampleFunctOne()
        {
            return SampleDataOne;
        }
    };
    class Base2
    {
        protected:
        int SampleDataTwo;
        public:
        Base2()
        {
            SampleDataTwo = 200;
        }
        ~Base2()
        {
        }
        int SampleFunctTwo()
        {
            return SampleDataTwo;
        }
    };
    class Derived1 : public Base1, public Base2
    {
        int MyData;
        public:
        Derived1() 
        {
            MyData = 300;
        }
        ~Derived1()
        {
        }    
        int MyFunct()
        {
            return (MyData + SampleDataOne + SampleDataTwo);
        }
    };
    int main()
    {
        Base1 SampleObjOne;
        Base2 SampleObjTwo;
        Derived1 SampleObjThree;
        cout << SampleObjThree.Base1 :: SampleFunctOne() << endl;
        cout << SampleObjThree.Base2 :: SampleFunctTwo() << endl;
        return 0;
    } 

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

  
    #include <iostream>
    using namespace std;
    struct a
    {
        int count;
    };
    struct b
    {
        int* value;
    };
    struct c : public a, public b
    {
    };
    int main()
    {
        c* p = new c;
        p->value = 0;
        cout << "Inherited";
        return 0;
    }

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

  
    #include <iostream>
    using namespace std;
    class Base
    {
        public:
        virtual void print() const = 0;
    };
    class DerivedOne : public Base
    {
        public:     
        void print() const
        {
            cout << "DerivedOne\n";
        }
    };
    class DerivedTwo : public Base
    {
        public:
        void print() const
        {
            cout << "DerivedTwo\n";
        }     
    }; 
    class Multiple : public DerivedOne, public DerivedTwo
    {
        public:
        void print() const
        {
            DerivedTwo :: print();
        } 
    }; 
    int main()
    {
        int i;
        Multiple both; 
        DerivedOne one; 
        DerivedTwo two; 
        Base *array[ 3 ]; 
        array[ 0 ] = &both; 
        array[ 1 ] = &one;
        array[ 2 ] = &two;
        array[ i ] -> print();
        return 0;
    }

9. 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();
    }

10. What is meant by multiple inheritance?


 

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.