CPP

CPP

Subscripting has 9 Questions
subscript operator is used to access which elements?

string
char
array
all of the mentioned
How many arguments will the subscript operator will take for overloading?

1
2
0
as many as possible
Pick out the correct statement.

subscript operator has a higher precedence than the assignment operator.
subscript operator has a lower precedence than the assignment operator.
subscript operator is used with string elements.
None of the mentioned
What is the output of this program?
  1.     #include 
  2.     using namespace std;
  3.     const int SIZE = 10;
  4.     class safe
  5.     {
  6.         private:
  7.         int arr[SIZE];
  8.         public:
  9.         safe()
  10.         {
  11.             register int i;
  12.             for (i = 0; i < SIZE; i++)
  13.             {
  14.                 arr[i] = i;
  15.             }
  16.         }
  17.         int &operator[](int i)
  18.         {
  19.             if (i > SIZE)
  20.             {
  21.                 cout << "Index out of bounds" <<endl;
  22.                 return arr[0];
  23.             }
  24.             return arr[i];
  25.         }
  26.     };
  27.     int main()
  28.     {
  29.         safe A;
  30.         cout << A[5];
  31.         cout  << A[12];
  32.         return 0;
  33.     }

5Index out of bounds ? ? ? 0
40
50
51
What is the output of this program?
  1.     #include 
  2.     using namespace std;
  3.     const int limit = 4;
  4.     class safearray
  5.     {
  6.         private:
  7.         int arr[limit];
  8.         public:
  9.         int& operator [](int n)
  10.         {
  11.             if (n == limit - 1)
  12.             {
  13.                 int temp;
  14.                 for (int i = 0; i < limit; i++)
  15.                 {
  16.                     if (arr[n + 1] > arr[n])
  17.                     {
  18.                         temp = arr[n];
  19.                         arr[n] = arr[n + 1];
  20.                         arr[n + 1] = temp;
  21.                     }     
  22.                 }  
  23.             }
  24.             return arr[n];
  25.         }
  26.     };
  27.     int main()
  28.     {
  29.         safearray sa1;
  30.         for(int j = 0; j < limit; j++)
  31.             sa1[j] = j*10;
  32.         for(int j = 0; j < limit; j++)
  33.         {
  34.             int temp = sa1[j];
  35.             cout << "Element " << j << " is " << temp;
  36.         }
  37.         return 0;
  38.     }

0102030
1020300
3020100
error
What is the output of this program?
  1.     #include  
  2.     using namespace std;
  3.     class A
  4.     {
  5.         public:
  6.         int x;
  7.         A(int n = 0) : x(n) {};
  8.         int& operator[](int n)
  9.         {
  10.              cout << "0" ;
  11.              return x;
  12.         }
  13.         int operator[](int n) const
  14.         {
  15.              cout << "1" ;
  16.              return x;
  17.         }
  18.      };
  19.     void foo(const A& a)
  20.     {
  21.         int z = a[2];
  22.     }
  23.     int main()
  24.     {
  25.         A a(7);
  26.         a[3]  = 8;
  27.         int z = a[2];
  28.         foo(a);
  29.         return 0;
  30.     }

110
111
011
001
What is the output of this program?
  1.     #include 
  2.     using namespace std;
  3.     class sample
  4.     {
  5.         private:
  6.         int* i;
  7.         int j;
  8.         public:
  9.         sample (int j);
  10.         ~sample ();
  11.         int& operator [] (int n);
  12.     };
  13.     int& sample::operator [] (int n)
  14.     {
  15.         return i[n];
  16.     }
  17.     sample::sample (int j)
  18.     {
  19.         i = new int [j];
  20.         j = j;
  21.     }
  22.     sample::~sample ()
  23.     {
  24.         delete [] i;
  25.     }
  26.     int main ()
  27.     {
  28.         sample m (5);
  29.         m [0] = 25;
  30.         m [1] = 20;
  31.         m [2] = 15;
  32.         m [3] = 10;
  33.         m [4] = 5;
  34.         for (int n = 0; n < 5; ++ n)
  35.         cout << m [n];
  36.         return 0;
  37.     }

252015105
510152025
51015
None of the mentioned
What do we need to do to pointer for overloading the subscript operator?

reference pointer
dereference pointer
store it in heap
None of the mentioned
What do we need to use when we have multiple subscripts?

operator()
operator[]
operator
None of the mentioned