Home » C++ Programming » Operators » Question
  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    const int SIZE = 15;
    class Example
    {
    private:
    int array[SIZE];
    public:
    Example()
    {
    register int k;
    for (k = 0; k < SIZE; k++)
    {
    array[k] = k;
    }
    }
    int &operator[](int k)
    {
    if (k > SIZE)
    {
    cout << " Index out of bounds" << endl;

    return array[0];
    }
    return array[k];
    }
    };
    int main()
    {
    Example Object;
    cout << Object[10];
    cout << Object[20];
    return 0;
    }
    1. 10 Index out of bounds
      0
    2. 15 Index out of bounds
      0
    3. 12 Index out of bounds
      0
    4. Compilation Error
    5. None of these
Correct Option: A

In this program, We are returning the elements in the specified array location and if it is out of bound means it will return the first element.



Your comments will be displayed only after manual approval.