Pointers(C++ Programming ) Questions and Answers
Question 1.
What is the output of this program?
1.
#include
2.
using namespace std;
3.
int main()
4.
{
5.
char *ptr;
6.
char Str[] = "abcdefg";
7.
ptr = Str;
8.
ptr += 5;
9.
cout
- fg
- cdef
- defg
- abcd
Explanation:-
Answer: Option A. -> fgPointer ptr points to string ‘fg’. So it prints fg.
Output:
$ g++ point.cpp
$ a.out
fg
Question 2.
What is the output of this program?
1.
#include
2.
using namespace std;
3.
int main()
4.
{
5.
char arr[20];
6.
int i;
7.
for(i = 0; i < 10; i++)
8.
*(arr + i) = 65 + i;
9.
*(arr + i) = '\0';
10.
cout
- ABCDEFGHIJ
- AAAAAAAAAA
- JJJJJJJJ
- none of the mentioned
Explanation:-
Answer: Option A. -> ABCDEFGHIJEach time we are assigning 65 + i. In first iteration i = 0 and 65 is assigned. So it will print from A to J.
$ g++ point1.cpp
$ a.out
ABCDEFGHIJ
Explanation:-
Answer: Option C. -> int ***fun(float*, char**)None.
Question 4.
What is the output of this program?
1.
#include
2.
using namespace std;
3.
int main()
4.
{
5.
int a = 5, b = 10, c = 15;
6.
int *arr[ ] = {&a, &b, &c};
7.
cout
- 5
- 10
- 15
- it will return some random number
Explanation:-
Answer: Option D. -> it will return some random numberArray element cannot be address of auto variable. It can be address of static or extern variables.
Explanation:-
Answer: Option B. -> p now points to bAssigning to refrence changes the object to which the refrence is bound.
Explanation:-
Answer: Option C. -> int i; double* dp = &i;dp is initialized int value of i.
Explanation:-
Answer: Option A. -> x is a pointer to a string, y is a string* is to be grouped with the variables not the data types.
Explanation:-
Answer: Option C. -> pointer to function taking a char* argument and returns an intNone.
Explanation:-
Answer: Option A. -> *None.
Explanation:-
Answer: Option D. -> point to a tyeA pointer can be in only 3 states a,b and c.