C++ Programming Questions and Answers – Subscripting

This section on C++ interview questions and answers focuses on “Subscripting”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ interview questions on “Subscripting” along with answers, explanations and/or solutions:

1. subscript operator is used to access which elements?
a) string
b) char
c) array
d) float
View Answer

Answer: c
Explanation: To access any element of an array we use following syntax array[i], where i is called subscript representing the ith element of an array, whereas no such cases in char and strings.

2. How many arguments will the subscript operator will take for overloading?
a) 1
b) 2
c) 0
d) as many as possible
View Answer

Answer: a
Explanation: The subscript operator overload takes only one argument, but it can be of any type.

3. Pick out the correct statement.
a) subscript operator has a higher precedence than the assignment operator
b) subscript operator has a lower precedence than the assignment operator
c) subscript operator is used with string elements
d) subscript operator is used with char elements
View Answer

Answer: a
Explanation: Subscription operator has more precedence otherwise if that is not the case then the statement var = arr[i] will be meaningless and will have no effect.
advertisement
advertisement

4. What will be the output of the following C++ code?

  1.     #include <iostream>
  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.     }

a)

5Index out of bounds
 0

b) 40
c) 50
d) 51
View Answer

Answer: a
Explanation: 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.
Output:

advertisement
$ g++ sub.cpp
$ a.out
5Index out of bounds
0

5. What will be the output of the following C++ code?

advertisement
  1.     #include <iostream>
  2.     using namespace std;
  3.     class numbers
  4.     {
  5.         private:
  6.         int m_nValues[10];
  7.         public:
  8.         int& operator[] (const int nValue);
  9.     };
  10.     int& numbers::operator[](const int nValue)
  11.     {
  12.         return m_nValues[nValue];
  13.     }
  14.     int main()
  15.     {
  16.         numbers N;
  17.         N[5] = 4;
  18.         cout <<  N[5];
  19.         return 0;
  20.     }

a) 5
b) 4
c) 3
d) 6
View Answer

Answer: b
Explanation: In this program, We are getting the values and returning it by overloading the subscript operator.
Output:

$ g++ sub1.cpp
$ a.out
4

6. What will be the output of the following C++ code?

  1.     #include <iostream>
  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.     }

a) 0102030
b) 1020300
c) 3020100
d) error
View Answer

Answer: a
Explanation: In this program, we are returning the array element by the multiple of 10.
Output:

$ g++ sub2.cpp
$ a.out
0102030

7. What will be the output of the following C++ code?

  1.     #include <iostream> 
  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.     }

a) 110
b) 111
c) 011
d) 001
View Answer

Answer: d
Explanation: In this program, we overloading the operator[] by using subscript operator.
Output:

$ g++ sub3.cpp
$ a.out
001

8. What will be the output of the following C++ code?

  1.     #include <iostream>
  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.     }

a) 252015105
b) 510152025
c) 51015
d) 51015210
View Answer

Answer: a
Explanation: In this program, we are printing the array in the reverse order by using subscript operator.
Output:

$ g++ sub4.cpp
$ a.out
252015105

9. What do we need to do to pointer for overloading the subscript operator?
a) reference pointer
b) dereference pointer
c) store it in heap
d) memory locator
View Answer

Answer: b
Explanation: 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.

10. What do we need to use when we have multiple subscripts?
a) operator()
b) operator[]
c) operator
d) operator<>
View Answer

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

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.