C Programming Questions and Answers – Pointers and Function Arguments – 1

This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Pointers and Function Arguments – 1”.

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

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

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

a) 10
b) Some garbage value
c) Compile time error
d) Segmentation fault/code crash
View Answer

Answer: c
Explanation: None.
advertisement
advertisement

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

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

a) 10
b) Some garbage value
c) Compile time error
d) Segmentation fault
View Answer

Answer: a
Explanation: None.
advertisement

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

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

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

Answer: b
Explanation: None.

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

advertisement
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int i = 97, *p = &i;
  5.         foo(&i);
  6.         printf("%d ", *p);
  7.     }
  8.     void foo(int *p)
  9.     {
  10.         int j = 2;
  11.         p = &j;
  12.         printf("%d ", *p);
  13.     }

a) 2 97
b) 2 2
c) Compile time error
d) Segmentation fault/code crash
View Answer

Answer: a
Explanation: None.

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

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int i = 97, *p = &i;
  5.         foo(&p);
  6.         printf("%d ", *p);
  7.         return 0;
  8.     }
  9.     void foo(int **p)
  10.     {
  11.         int j = 2;
  12.         *p = &j;
  13.         printf("%d ", **p);
  14.     }

a) 2 2
b) 2 97
c) Undefined behaviour
d) Compilation Error
View Answer

Answer: c
Explanation: The main() function calls foo(&p) passing the address of an integer pointer. Inside foo() function, we are assigning the address of a local variable j to the pointer (*p) and then printing **p, which will display the value 2. However, once we return back to the caller, i.e., the main() function, we are trying to access the address of the local variable j, which was on another stack frame and it might have got destroyed once we returned back to the caller. If the memory for the stack frame of foo() function was still there, the program will print the value 2 in the main() function also. However, if the memory was not there, it will be invalid memory access, resulting in segmentation fault and maybe, a core dump. So, the correct answer will be “Undefined behaviour”.

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

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int i = 11;
  5.         int *p = &i;
  6.         foo(&p);
  7.         printf("%d ", *p);
  8.     }
  9.     void foo(int *const *p)
  10.     {
  11.         int j = 10;
  12.         *p = &j;
  13.         printf("%d ", **p);
  14.     }

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

Answer: a
Explanation: None.

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

  1.     #include <stdio.h>
  2.     void foo(int *);
  3.     int main()
  4.     {
  5.         int i = 10;
  6.         int *p = &i;
  7.         foo(p);
  8.         printf("%d ", *p);
  9.     }
  10.     void foo(int *p)
  11.     {
  12.         int j = 11;
  13.         p = &j;
  14.         printf("%d ", *p);
  15.     }

a) 11 11
b) 11 10
c) Compile time error
d) Undefined-value
View Answer

Answer: b
Explanation: None.

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

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

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

Answer: d
Explanation: p points to invalid memory(local variable of another function) after it returns from foo() function.

9. Which of the following is the correct syntax to send an array as a parameter to function?
a) func(&array);
b) func(#array);
c) func(*array);
d) func(array[size]);
View Answer

Answer: a
Explanation: None.

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.

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.