C Programming MCQ – Pointers

This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Pointers”.

Pre-requisite for C Pointers MCQ set: Video Tutorial on C Pointers.

1. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         char *p = NULL;
  5.         char *q = 0;
  6.         if (p)
  7.             printf(" p ");
  8.         else
  9.             printf("nullp");
  10.         if (q)
  11.             printf("q\n");
  12.         else
  13.             printf(" nullq\n");
  14.     }

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
View Answer

Answer: a
Explanation: None.
advertisement
advertisement

2. What will be the output of the following C code?

Note: Join free Sanfoundry classes at Telegram or Youtube
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int i = 10;
  5.         void *p = &i;
  6.         printf("%d\n", (int)*p);
  7.         return 0;
  8.     }

a) Compile time error
b) Segmentation fault/runtime crash
c) 10
d) Undefined behaviour
View Answer

Answer: a
Explanation: None.
advertisement

3. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int i = 10;
  5.         void *p = &i;
  6.         printf("%f\n", *(float*)p);
  7.         return 0;
  8.     }

a) Compile time error
b) Undefined behaviour
c) 10
d) 0.000000
View Answer

Answer: d
Explanation: None.

4. What will be the output of the following C code?

advertisement
  1.     #include <stdio.h>
  2.     int *f();
  3.     int main()
  4.     {
  5.         int *p = f();
  6.         printf("%d\n", *p);
  7.     }
  8.     int *f()
  9.     {
  10.         int *j = (int*)malloc(sizeof(int));
  11.         *j = 10;
  12.         return j;
  13.     }

a) 10
b) Compile time error
c) Segmentation fault/runtime crash since pointer to local variable is returned
d) Undefined behaviour
View Answer

Answer: a
Explanation: None.

5. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     int *f();
  3.     int main()
  4.     {
  5.         int *p = f();
  6.         printf("%d\n", *p);
  7.     }
  8.     int *f()
  9.     {
  10.         int j = 10;
  11.         return &j;
  12.     }

a) 10
b) Compile time error
c) Segmentation fault/runtime crash
d) Undefined behaviour
View Answer

Answer: a
Explanation: We are returning address of a local variable which should not be done. In this specific instance, we are able to see the value of 10, which may not be the case if we call other functions before calling printf() in main().

6. Comment on the following pointer declaration.

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
View Answer

Answer: a
Explanation: None.

7. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int *ptr, a = 10;
  5.         ptr = &a;
  6.         *ptr += 1;
  7.         printf("%d,%d/n", *ptr, a);
  8.     }

a) 10,10
b) 10,11
c) 11,10
d) 11,11
View Answer

Answer: d
Explanation: None.

8. Comment on the following C statement.

const int *ptr;

a) You cannot change the value pointed by ptr
b) You cannot change the pointer ptr itself
c) You May or may not change the value pointed by ptr
d) You can change the pointer as well as the value pointed by it
View Answer

Answer: a
Explanation: None.

More Pointers MCQs in C Programming:

Sanfoundry Global Education & Learning Series – C Programming Language.

To practice all areas of C language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.