21.
What will be the output of the following C++ code?
#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;
}

22.
Which of the following is correct about friend functions?

24.
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;
}

25.
What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A
{
	static int a;
 
   public:
	void show()
        {
		a++;
		cout<<"a: "<<a<<endl;
	}
	void operator.()
        {
		cout<<"Objects are added\n";
	}
};
 
class B
{
     public:
};
 
int main(int argc, char const *argv[])
{
	A a1, a2;
	return 0;
}

28.
Which is the correct statement about operator overloading?

30.
What will be the output of the following C++ code?
#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;
}

Read More Section(Classes and Objects in C plus plus)

Each Section contains maximum 100 MCQs question on Classes and Objects in C plus plus. To get more questions visit other sections.