C Programming Questions and Answers on Pointers and Addresses for Freshers

https://www.computersprofessor.com/2017/12/c-programming-questions-and-answers-on_13.html
1. What is the output of this C code?
#include
int main()
{
char *p = NULL;
char *q = 0;
if (p)
printf(" p ");
else
printf("nullp");
if (q)
printf("q\n");
else
printf(" nullq\n");
}
a) nullp nullq
b) Depends on the compiler
c) x nullq where x can be p or nullp depending on the value of NULL
d) p q
b) Depends on the compiler
c) x nullq where x can be p or nullp depending on the value of NULL
d) p q
Answer: a
2. What is the output of this C code?
#include
int main()
{
int i = 10;
void *p = &i;
printf("%d\n", (int)*p);
return 0;
}
a) Compile time error
b) Segmentation fault/runtime crash
c) 10
d) Undefined behaviour
b) Segmentation fault/runtime crash
c) 10
d) Undefined behaviour
Answer: a
3. What is the output of this C code?
#include
int main()
{
int i = 10;
void *p = &i;
printf("%f\n", *(float*)p);
return 0;
}
a) Compile time error
b) Undefined behaviour
c) 10
d) 0.000000
b) Undefined behaviour
c) 10
d) 0.000000
Answer: d
4. What is the output of this C code?
#include
int *f();
int main()
{
int *p = f();
printf("%d\n", *p);
}
int *f()
{
int *j = (int*)malloc(sizeof(int));
*j = 10;
return j;
}
a) 10
b) Compile time error
c) Segmentation fault/runtime crash since pointer to local variable is returned
d) Undefined behaviour
b) Compile time error
c) Segmentation fault/runtime crash since pointer to local variable is returned
d) Undefined behaviour
Answer: a
5. What is the output of this C code?
#include
int *f();
int main()
{
int *p = f();
printf("%d\n", *p);
}
int *f()
{
int j = 10;
return &j;
}
a) 10
b) Compile time error
c) Segmentation fault/runtime crash
d) Undefined behaviour
b) Compile time error
c) Segmentation fault/runtime crash
d) Undefined behaviour
Answer: a
6. Comment on the following pointer declaration?
int *ptr, p;
int *ptr, p;
a) ptr is a pointer to integer, p is not
b) ptr and p, both are pointers to integer
c) ptr is a pointer to integer, p may or may not be
d) ptr and p both are not pointers to integer
Answer: a
7. What is the output of this C code?
#include
int main()
{
int *ptr, a = 10;
ptr = &a;
*ptr += 1;
printf("%d,%d/n", *ptr, a);
}
a) 10,10
b) 10,11
c) 11,10
d) 11,11
b) 10,11
c) 11,10
d) 11,11
Answer: d
8. Comment on the following?
const int *ptr;
const int *ptr;
a) You cannot change the value pointed by ptr
b) You cannot change the pointer ptr itself
c) You May or maynot change the value pointed by ptr
d) You can change the pointer as well as the value pointed by it
Answer: a
9. Which is an indirection operator among the following?
a) &
b) *
c) ->
d) .
Answer: b
10. Which of the following does not initialize ptr to null (assuming variable declaration of a as int a=0;?
a) int *ptr = &a;
b) int *ptr = &a – &a;
c) int *ptr = a – a;
d) All of the mentioned
Answer: a
11. What is the output of this C code?
#include
int x = 0;
void main()
{
int *ptr = &x;
printf("%p\n", ptr);
x++;
printf("%p\n ", ptr);
}
a) Same address
b) Different address
c) Compile time error
d) Varies
b) Different address
c) Compile time error
d) Varies
Answer: a
12. What is the output of this C code?
#include
int x = 0;
void main()
{
int *const ptr = &x;
printf("%p\n", ptr);
ptr++;
printf("%p\n ", ptr);
}
a) 0 1
b) Compile time error
c) 0xbfd605e8 0xbfd605ec
d) 0xbfd605e8 0xbfd605e8
b) Compile time error
c) 0xbfd605e8 0xbfd605ec
d) 0xbfd605e8 0xbfd605e8
Answer: b
13. What is the output of this C code?
#include
void main()
{
int x = 0;
int *ptr = &x;
printf("%p\n", ptr);
ptr++;
printf("%p\n ", ptr);
}
a) 0xbfd605e8 0xbfd605ec
b) 0xbfd605e8 0cbfd60520
c) 0xbfd605e8 0xbfd605e9
d) Run time error
b) 0xbfd605e8 0cbfd60520
c) 0xbfd605e8 0xbfd605e9
d) Run time error
Answer: a
14. What is the output of this C code?
#include
void main()
{
int x = 0;
int *ptr = &5;
printf("%p\n", ptr);
}
a) 5
b) Address of 5
c) Nothing
d) Compile time error
b) Address of 5
c) Nothing
d) Compile time error
Answer: d
15. What is the output of this C code?
#include
void main()
{
int x = 0;
int *ptr = &x;
printf("%d\n", *ptr);
}
a) Address of x
b) Junk value
c) 0
d) Run time error
b) Junk value
c) 0
d) Run time error
Answer: c