Home » C++ Programming » Operators » Question
  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    const int limit = 3;
    class arrayExample
    {
    private:
    int array[limit];
    public:
    int& operator [](int num)
    {
    if (num == limit - 2)
    {
    int temp;
    for (int k = 0; k < limit; k++)
    {
    if (array[num + 2] > array[num])
    {
    temp = array[num];
    array[num] = array[num + 2];
    array[num + 2] = temp;
    }
    }
    }
    return array[num];
    }
    };
    int main()
    {
    arrayExample sa1;
    for(int L = 0; L < limit; L++)
    sa1[L] = L*10;
    for(int L = 0; L < limit; L++)
    {
    int temp = sa1[L];
    cout << "Element of " << L << " is " << temp << endl;

    }
    return 0;
    }
    1. Element of 0 is 0
      Element of 1 is 5
      Element of 2 is 10
    2. Element of 0 is 10
      Element of 1 is 0
      Element of 2 is 20
    3. Element of 0 is 20
      Element of 1 is 10
      Element of 2 is 0
    4. Compilation Error
    5. None of these
Correct Option: A

In this program, we are returning the array element by the multiple of 5.



Your comments will be displayed only after manual approval.