My Report

C++ Operator 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. Pick the other name of operator function.

2. How to declare operator function?

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

 
    #include <iostream>
    using namespace std;
    class myclass
    {
        public:
        int i;
        myclass *operator->()
        {return this;}
    };
    int main()
    {
        myclass ob;
        ob->i = 10; 
        cout << ob.i << " " << ob->i;
        return 0;
    }

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

 
    #include <iostream>
    using namespace std;
    class Integer 
    {
        int i;
        public:
        Integer(int ii) : i(ii) {}
        const Integer
        operator+(const Integer& rv) const 
        {
            cout << "operator+" << endl;
            return Integer(i + rv.i);
        }
        Integer&
        operator+=(const Integer& rv) 
        {
            cout << "operator+=" << endl;
            i += rv.i;
            return *this;
        }
    };
    int main() 
    {
        int i = 1, j = 2, k = 3;
        k += i + j;
        Integer ii(1), jj(2), kk(3);
        kk += ii + jj;
    }

5. Which of the following statements is NOT valid about operator overloading?

6. Which of the following operators can't be overloaded?

7. Operator overloading is ___________

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

 
    #include <iostream>
    using namespace std;
    class sample 
    {
        public:
        int x, y;
        sample() {};
        sample(int, int);
        sample operator + (sample);
    };
    sample::sample (int a, int b) 
    {
        x = a;
        y = b;
    }
    sample sample::operator+ (sample param) 
    {
        sample temp;
        temp.x = x + param.x;
        temp.y = y + param.y;
        return (temp);
    }
    int main () 
    {
        sample a (4,1);
        sample b (3,2);
        sample c;
        c = a + b;
        cout << c.x << "," << c.y;
        return 0;
    }

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

 
    #include <iostream>
    using namespace std;
    class Box
    {   
        double length;
        double breadth;
        double height;
        public:
        double getVolume(void)
        {  
            return length * breadth * height;
        }
        void setLength( double len )
        {   
            length = len;
        }
        void setBreadth( double bre )
        {   
            breadth = bre;
        }
        void setHeight( double hei )
        {   
            height = hei;
        }
        Box operator+(const Box& b)
        {  
            Box box;
            box.length = this->length + b.length;
            box.breadth = this->breadth + b.breadth;
            box.height = this->height + b.height;
            return box;
        } 
    };
    int main( )
    {  
        Box Box1;
        Box Box2;
        Box Box3;
        double volume = 0.0;
        Box1.setLength(6.0);
        Box1.setBreadth(7.0);
        Box1.setHeight(5.0);
        Box2.setLength(12.0);
        Box2.setBreadth(13.0);
        Box2.setHeight(10.0);
        volume = Box1.getVolume();
        cout << "Volume of Box1 : " << volume <<endl;
        volume = Box2.getVolume();
        cout << "Volume of Box2 : " << volume <<endl;
        Box3 = Box1 + Box2;
        volume = Box3.getVolume();
        cout << "Volume of Box3 : " << volume <<endl;
        return 0;
    }

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

 
    #include <iostream>
    using namespace std;
    ostream & operator<<(ostream & i, int n)
    {
        return i;
    }
    int main()
    {
        cout << 5 << endl;
        cin.get();
        return 0;
    }

 

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.