adplus-dvertising

Welcome to the Classes and Operator Overloading in C++ MCQs Page

Dive deep into the fascinating world of Classes and Operator Overloading in C++ with our comprehensive set of Multiple-Choice Questions (MCQs). This page is dedicated to exploring the fundamental concepts and intricacies of Classes and Operator Overloading in C++, a crucial aspect of Object Oriented Programming Using C++. In this section, you will encounter a diverse range of MCQs that cover various aspects of Classes and Operator Overloading in C++, from the basic principles to advanced topics. Each question is thoughtfully crafted to challenge your knowledge and deepen your understanding of this critical subcategory within Object Oriented Programming Using C++.

frame-decoration

Check out the MCQs below to embark on an enriching journey through Classes and Operator Overloading in C++. Test your knowledge, expand your horizons, and solidify your grasp on this vital area of Object Oriented Programming Using C++.

Note: Each MCQ comes with multiple answer choices. Select the most appropriate option and test your understanding of Classes and Operator Overloading in C++. You can click on an option to test your knowledge before viewing the solution for a MCQ. Happy learning!

Classes and Operator Overloading in C++ MCQs | Page 12 of 39

Explore more Topics under Object Oriented Programming Using C++

Q111.
What is the output of this program?
    #include <iostream>
    using namespace std;
    const int SIZE = 10;
    class safe
    {
        private:
        int arr[SIZE];
        public:
        safe()
        {
            register int i;
            for (i = 0; i < SIZE; i++)
            {
                arr[i] = i;
            }
        }
        int &operator[](int i)
        {
            if (i > SIZE)
            {
                cout << "Index out of bounds" <<endl;
                return arr[0];
            }
            return arr[i];
        }
    };
    int main()
    {
        safe A;
        cout << A[5];
        cout  << A[12];
        return 0;
    }
Discuss
Answer: (a).5Index out of bounds 0
Q112.
What is the output of this program?
    #include <iostream>
    using namespace std;
    class numbers
    {
        private:
        int m_nValues[10];
        public:
        int& operator[] (const int nValue);
    };
    int& numbers::operator[](const int nValue)
    {
        return m_nValues[nValue];
    }
    int main()
    {
        numbers N;
        N[5] = 4;
        cout <<  N[5];
        return 0;
    }

a.

5

b.

4

c.

3

d.

6

Discuss
Answer: (b).4
Q113.
What is the output of this program?
    #include <iostream>
    using namespace std;
    const int limit = 4;
    class safearray
    {
        private:
        int arr[limit];
        public:
        int& operator [](int n)
        {
            if (n == limit - 1)
            {
                int temp;
                for (int i = 0; i < limit; i++)
                {
                    if (arr[n + 1] > arr[n])
                    {
                        temp = arr[n];
                        arr[n] = arr[n + 1];
                        arr[n + 1] = temp;
                    }     
                }  
            }
            return arr[n];
        }
    };
    int main()
    {
        safearray sa1;
        for(int j = 0; j < limit; j++)
            sa1[j] = j*10;
        for(int j = 0; j < limit; j++)
        {
            int temp = sa1[j];
            cout << "Element " << j << " is " << temp;
        }
        return 0;
    }
Discuss
Answer: (a).0102030
Q114.
What is the output of this program?
    #include <iostream> 
    using namespace std;
    class A
    {
        public:
        int x;
        A(int n = 0) : x(n) {};
        int& operator[](int n)
        {
             cout << "0" ;
             return x;
        }
        int operator[](int n) const
        {
             cout << "1" ;
             return x;
        }
     };
    void foo(const A& a)
    {
        int z = a[2];
    }
    int main()
    {
        A a(7);
        a[3]  = 8;
        int z = a[2];
        foo(a);
        return 0;
    }
Discuss
Answer: (d).001
Q115.
What is the output of this program?
    #include <iostream>
    using namespace std;
    class sample
    {
        private:
        int* i;
        int j;
        public:
        sample (int j);
        ~sample ();
        int& operator [] (int n);
    };
    int& sample::operator [] (int n)
    {
        return i[n];
    }
    sample::sample (int j)
    {
        i = new int [j];
        j = j;
    }
    sample::~sample ()
    {
        delete [] i;
    }
    int main ()
    {
        sample m (5);
        m [0] = 25;
        m [1] = 20;
        m [2] = 15;
        m [3] = 10;
        m [4] = 5;
        for (int n = 0; n < 5; ++ n)
        cout << m [n];
        return 0;
    }
Discuss
Answer: (a).252015105
Q116.
What do we need to do to pointer for overloading the subscript operator?
Discuss
Answer: (b).dereference pointer
Q117.
What do we need to use when we have multiple subscripts?
Discuss
Answer: (a).operator()
Discuss
Answer: (b).overloading the objects
Discuss
Answer: (a).virtual functions does not give the ability to write a templated function
Discuss
Answer: (d).It will modify the operator to be interpreted

Suggested Topics

Are you eager to expand your knowledge beyond Object Oriented Programming Using C++? We've curated a selection of related categories that you might find intriguing.

Click on the categories below to discover a wealth of MCQs and enrich your understanding of Computer Science. Happy exploring!