Operators


  1. Which operator works only with integer variables?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    both decrement & increment


  1. What do we need to use when we have multiple subscripts?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    The reason is that operator[] always takes exactly one parameter, but operator() can take any number of parameters.



  1. What do we need to do to pointer for overloading the subscript operator?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    If you have a pointer to an object of some class type that overloads the subscript operator, you have to dereference that pointer in order to free the memory.


  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    class Example
    {
    private:
    int* K;
    int L;
    public:
    Example (int L);
    ~Example ();
    int& operator [] (int num);
    };
    int& Example::operator [] (int num)
    {
    return K[num];
    }
    Example::Example (int L)
    {
    K = new int [L];
    L = L;
    }
    Example::~Example ()
    {
    delete [] K;
    }
    int main ()
    {
    Example obj (10);
    obj [0] = 15;
    obj [1] = 10;
    obj [2] = 35;
    obj [3] = 30;
    for (int num = 0; num < 5; ++ num)
    cout << obj [num] <<" ";
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: E

    In this program, we are printing the array in the reverse order by using subscript operator.



  1. What is the output of this program?
    #include <iostream> 
    using namespace std;
    class Example
    {
    public:
    int p;
    Example(int num = 0) : p(num) {};
    int& operator[](int num)
    {
    cout << "Interview " ;
    return p;
    }
    int operator[](int num) const
    {
    cout << "Mania" ;
    return p;
    }
    };
    void foo(const Example& n)
    {
    int Res = n[2];
    }
    int main()
    {
    Example n(7);
    n[3] = 8;
    int Res = n[2];
    foo(n);
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    In this program, we overloading the operator[] by using subscript operator.