1. subscript operator is used to access which elements?
A. string
B. char
C. array
D. float
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
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. a = a – 5
B. a = a / b
C. a -= 5
D. a = a + 5
Answer: C
Explanation:
When we want to modify the value of a variable by performing an operation on the value currently stored, We will use compound assignment statement. In this option, a -=5 is equal to a = a-5.
4. What will be the output of the following C++ code?
#include
using namespace std;
const int SIZE = 10;
class safe
{
private:
int arr[SIZE];
public:
safe()
{
register int i;
for (i = 0; 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
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.
5. What will be the output of the following C++ code?
#include
using namespace std;
class numbers
{
private:
int m_nValues[10];
public:
int& operator[] (const int nValue);
};
int& numbers::operator[](const int nValue)
{
return m_nValues[nValue];
}
int main()
{
numbers N;
N[5] = 4;
cout << N[5];
return 0;
}
A. 5
B. 4
C. 3
D. 6
Answer: B
Explanation:
In this program, We are getting the values and returning it by overloading the subscript operator.
6. What will be the output of the following C++ code?
#include
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 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
Answer: A
Explanation:
In this program, we are returning the array element by the multiple of 10.
7. What will be the output of the following C++ code?
#include
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
Answer: D
Explanation:
In this program, we overloading the operator[] by using subscript operator.
8. What will be the output of the following C++ code?
#include
using namespace std;
class sample
{
private:
int* i;
int j;
public:
sample (int j);
~sample ();
int& operator [] (int n);
};
int& sample::operator [] (int n)
{
return i[n];
}
sample::sample (int j)
{
i = new int [j];
j = j;
}
sample::~sample ()
{
delete [] i;
}
int main ()
{
sample m (5);
m [0] = 25;
m [1] = 20;
m [2] = 15;
m [3] = 10;
m [4] = 5;
for (int n = 0; n < 5; ++ n)
cout << m [n];
return 0;
}
A. 252015105
B. 510152025
C. 51015
D. 51015210
Answer: A
Explanation:
In this program, we are printing the array in the reverse order by using subscript operator.
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
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<>
Answer: A
Explanation:
The reason is that operator[] always takes exactly one parameter, but operator() can take any number of parameters.