Pointers(Engineering > Computer Science And Engineering > C++ Language ) Questions and Answers
Question 1.
What does the following statement mean?
int (*fp)(char*)
pointer to a pointer
pointer to an array of chars
pointer to function taking a char* argument and returns an int
function taking a char* argument and returning a pointer to int
Explanation:-
Answer: Option C. -> pointer to function taking a char* argument and returns an int
pointer to function taking a char* argument and returns an int
Question 2.
Choose the right option
string* x, y;
x is a pointer to a string, y is a string
y is a pointer to a string, x is a string
both x and y are pointer to string types
none of the mentioned
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.
Question 3. Which of the following is illegal?
int *ip;
string s, *sp = 0;
int i; double* dp = &i;
int *pi = 0;
Explanation:-
Answer: Option C. -> int i; double* dp = &i;
dp is initialized int value of i.
Question 4. The operator used for dereferencing or indirection is ____
*
&
->
->>
Explanation:-
Answer: Option A. -> *
*
Question 5. Which one of the following is not a possible state for a pointer?
hold the address of the specific object
point one past the end of an object
zero
point to a tye
Explanation:-
Answer: Option D. -> point to a tye
A pointer can be in only 3 states a,b and c.
Question 6. The correct statement for a function that takes pointer to a float, a pointer to a pointer to a char and returns a pointer to a pointer to a integer is
int **fun(float**, char**)
int *fun(float*, char*)
int ***fun(float*, char**)
int ***fun(*float, **char)
Explanation:-
Answer: Option C. -> int ***fun(float*, char**)
int ***fun(float*, char**)
Question 7.
What will happen in this code?
int a = 100, b = 200;
int *p = &a, *q = &b;
p = q;
b is assigned to a
p now points to b
a is assigned to b
q now points to a
Explanation:-
Answer: Option B. -> p now points to b
Assigning to refrence changes the object to which the refrence is bound.
Question 8.
What is the output of this program?
#include < iostream >
using namespace std;
int main()
{
int a = 5, b = 10, c = 15;
int *arr[ ] = {&a, &b, &c};
cout
5
10
15
it will return some random number
Explanation:-
Answer: Option D. -> it will return some random number
Array element cannot be address of auto variable. It can be address of static or extern variables.
Question 9.
What is the output of this program?
#include < iostream >
using namespace std;
int main()
{
char *ptr;
charStr[] = "abcdefg";
ptr = Str;
ptr += 5;
cout
fg
cdef
defg
abcd
Explanation:-
Answer: Option A. -> fg
Pointer ptr points to string 'fg'. So it prints fg.
Question 10.
What is the output of this program?
#include < iostream >
using namespace std;
int main()
{
char arr[20];
int i;
for(i = 0; i < 10; i++)
*(arr + i) = 65 + i;
*(arr + i) = '\0';
cout
ABCDEFGHIJ
AAAAAAAAAA
JJJJJJJJ
none of the mentioned
Explanation:-
Answer: Option A. -> ABCDEFGHIJ
Each time we are assigning 65 + i. In first iteration i = 0 and 65 is assigned. So it will print from A to J.