Pointer(C Program ) Questions and Answers

Question 1. Determine Output:
void main()
{
char far *farther, *farthest;
printf("%d..%d", sizeof(farther), sizeof(farthest));
}
  1.    4..2
  2.    2..2
  3.    4..4
  4.    2..4
Explanation:-
Answer: Option A. -> 4..2
The second pointer is of char type and not a far pointer.

Question 2. Determine Output:
main()
{
char *str1 = "abcd";
char str2[] = "abcd";
printf("%d %d %d", sizeof(str1), sizeof(str2), sizeof("abcd"));
}
  1.    2 5 5
  2.    2 4 4
  3.    8 5 5
  4.    2 4 5
Explanation:-
Answer: Option C. -> 8 5 5
In first sizeof, str1 is a character pointer so it gives you the size of the pointer variable. In second sizeof the name str2 indicates the name of the array whose size is 5 (including the 'null' termination character). The third sizeof is similar to the second one.

Question 3. Choose the best answer.
Prior to using a pointer variable
  1.    It should be declared.
  2.    It should be initialized.
  3.    It should be both declared and initialized.
  4.    None of these.
Explanation:-
Answer: Option C. -> It should be both declared and initialized.
Using a pointer variable, without initializing it, will be disastrous, as it will have a garbage value.

Question 4. Comment on the following pointer declaration?
int *ptr, p;
  1.    ptr is a pointer to integer, p is not.
  2.    ptr and p, both are pointers to integer.
  3.    ptr is pointer to integer, p may or may not be.
  4.    ptr and p both are not pointers to integer.
Explanation:-
Answer: Option A. -> ptr is a pointer to integer, p is not.

Question 5. What will be the output?
main()
{
char *p;
p = "Hello";
printf("%cn",*&*p);
}
  1.    Hello
  2.    H
  3.    Some address will be printed
  4.    None of these.
Explanation:-
Answer: Option B. -> H
* is a dereference operator & is a reference operator. They can be applied any number of times provided it is meaningful. Here p points to the first character in the string "Hello". *p dereferences it and so its value is H. Again & references it to an address and * dereferences it to the value H.

Question 6. What will be printed after compiling and running the following code?
main()
{
char *p;
printf("%d %d",sizeof(*p), sizeof(p));
}
  1.    1 1
  2.    1 2
  3.    2 1
  4.    2 2
Explanation:-
Answer: Option B. -> 1 2
The sizeof() operator gives the number of bytes taken by its operand. p is a character pointer, which needs one byte for storing its value (a character). Hence sizeof(*p) gives a value of 1. Since it needs two bytes to store the address of the character pointer sizeof(p) gives 2.

Question 7. Find the output of the following program.
void main()
{
char *msg = "hi";
printf(msg);
}
  1.    hi
  2.    h
  3.    hi followed by garbage value
  4.    Error
  5.    Garbage Value
Explanation:-
Answer: Option A. -> hi

Question 8. Find the output of the following program.
void main()
{
int array[10];
int *i = &array[2], *j = &array[5];
int diff = j-i;
printf("%d", diff);
}
  1.    3
  2.    6
  3.    Garbage value
  4.    Error
Explanation:-
Answer: Option A. -> 3
When subtracting pointers you get the number of elements between those addresses, not the number of bytes.

Question 9. Which of the following is the correct way of declaring a float pointer:
  1.    float ptr;
  2.    float *ptr;
  3.    *float ptr;
  4.    None of the above
Explanation:-
Answer: Option B. -> float *ptr;

Question 10. What will be the output of the following program code?
#include
void main()
{
int i=3, *j, **k;
j = &i;
k = &j;
printf("%d%d%d", *j, **k, *(*k));
}
  1.    444
  2.    000
  3.    333
  4.    433
  5.    Garbage Value
Explanation:-
Answer: Option C. -> 333