C++ Programming Questions and Answers – Subscripting

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, walk in interviews, company interviews)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.

C++ interview questions on “Subscripting” along with answers, explanations and/or solutions:

Q 1. subscript operator is used to access which elements?
A. string
B. char
C. array
D. float

Show Answer Answer:-C. array
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.

Q 2. How many arguments will the subscript operator will take for overloading?
A. 1
B. 2
C. 0
D. as many as possible

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

Q 3. Pick out the correct statement.
A. subscript operator is used with string elements
B. subscript operator has a lower precedence than the assignment operator
C. subscript operator has a higher precedence than the assignment operator
D. subscript operator is used with char elements

Show Answer Answer:-C. subscript operator has a higher precedence than the assignment operator
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.

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

#include <iostream>

using namespace std;

const int SIZE = 10;

class safe

{

private:

int arr[SIZE];

public:

safe()

{

register int i;

for (i = 0; i < SIZE; i++)

{

arr[i] = i;

}

}

int &operator[](int i)

{

if (i > SIZE)

{

cout << “Index out of bounds” <<endl;

return arr[0];

}

return arr[i];

}

};

int main()

{

safe A;

cout << A[5];

cout << A[12];

return 0;

}

A. 5Index out of bounds 0

B. 40
C. 50
D. 51

Show Answer Answer:-A. 5Index out of bounds 0
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: $ g++ sub.cpp $ a.out 5Index out of bounds 0

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

A. 3
B. 4
C. 5
D. 6

Show Answer Answer:-B. 4
Explanation In this program, We are getting the values and returning it by overloading the subscript operator. Output: $ g++ sub1.cpp $ a.out 4

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

#include <iostream>

using namespace std;

const int limit = 4;

class safearray

{

private:

int arr[limit];

public:

int& operator [](int n)

{

if (n == limit – 1)

{

int temp;

for (int i = 0; i < limit; i++)

{

if (arr[n + 1] > arr[n])

{

temp = arr[n];

arr[n] = arr[n + 1];

arr[n + 1] = temp;

}

}

}

return arr[n];

}

};

int main()

{

safearray sa1;

for(int j = 0; j < limit; j++)

sa1[j] = j*10;

for(int j = 0; j < limit; j++)

{

int temp = sa1[j];

cout << “Element ” << j << ” is ” << temp;

}

return 0;

}

A. 0102030
B. 1020300
C. 3020100
D. error

Show Answer Answer:-A. 0102030
Explanation In this program, we are returning the array element by the multiple of 10. Output: $ g++ sub2.cpp $ a.out 0102030

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

#include <iostream>

using namespace std;

class A

{

public:

int x;

A(int n = 0) : x(n) {};

int& operator[](int n)

{

cout << “0” ;

return x;

}

int operator[](int n) const

{

cout << “1” ;

return x;

}

};

void foo(const A& a)

{

int z = a[2];

}

int main()

{

A a(7);

a[3] = 8;

int z = a[2];

foo(a);

return 0;

}

A. 110
B. 111
C. 011
D. 001

Show Answer Answer:-D. 001
Explanation In this program, we overloading the operator[] by using subscript operator. Output: $ g++ sub3.cpp $ a.out 001

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

A. 51015
B. 510152025
C. 252015105
D. 51015210

Show Answer Answer:-C. 252015105
Explanation In this program, we are printing the array in the reverse order by using subscript operator. Output: $ g++ sub4.cpp $ a.out 252015105

Q 9. What do we need to do to pointer for overloading the subscript operator?
A. reference pointer
B. memory locator
C. store it in heap
D. dereference pointer

Show Answer Answer:-D. dereference pointer
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.

Q 10. What do we need to use when we have multiple subscripts?
A. operator[]
B. operator()
C. operator
D. operator<>

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

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page