Subscripting(C++ Programming ) Questions and Answers
Question 3.
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 35.
cout
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 35.
cout
Explanation:-
Answer: Option A. -> 252015105In this program, we are printing the array in the reverse order by using subscript operator.
Output:
$ g++ sub4.cpp
$ a.out
252015105
Question 4.
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 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 31.
sa1[j] = j*10;
32.
for(int j = 0; j 33.
{
34.
int temp = sa1[j];
35.
cout
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 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 31.
sa1[j] = j*10;
32.
for(int j = 0; j 33.
{
34.
int temp = sa1[j];
35.
cout
Explanation:-
Answer: Option A. -> 0102030In this program, we are returning the array element by the multiple of 10.
Output:
$ g++ sub2.cpp
$ a.out
0102030
Explanation:-
Answer: Option D. -> 001In this program, we overloading the operator[] by using subscript operator.
Output:
$ g++ sub3.cpp
$ a.out
001
Question 6.
1.
#include
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
What is the output of this program?
1.
#include
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
Explanation:-
Answer: Option B. -> 4In this program, We are getting the values and returning it by overloading the subscript operator.
Output:
$ g++ sub1.cpp
$ a.out
4