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?
#include <stdio.h>
void foo(int*);
int main()
{
int i = 10;
foo((&i)++);
}
void foo(int *p)
{
printf("%d\n", *p);
}
a) 10
b) Some garbage value
c) Compile time error
d) Segmentation fault/code crash
View Answer
Explanation: None.
2. What will be the output of the following C code?
#include <stdio.h>
void foo(int*);
int main()
{
int i = 10, *p = &i;
foo(p++);
}
void foo(int *p)
{
printf("%d\n", *p);
}
a) 10
b) Some garbage value
c) Compile time error
d) Segmentation fault
View Answer
Explanation: None.
3. What will be the output of the following C code?
#include <stdio.h>
void foo(float *);
int main()
{
int i = 10, *p = &i;
foo(&i);
}
void foo(float *p)
{
printf("%f\n", *p);
}
a) 10.000000
b) 0.000000
c) Compile time error
d) Undefined behaviour
View Answer
Explanation: None.
4. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int i = 97, *p = &i;
foo(&i);
printf("%d ", *p);
}
void foo(int *p)
{
int j = 2;
p = &j;
printf("%d ", *p);
}
a) 2 97
b) 2 2
c) Compile time error
d) Segmentation fault/code crash
View Answer
Explanation: None.
5. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int i = 97, *p = &i;
foo(&p);
printf("%d ", *p);
return 0;
}
void foo(int **p)
{
int j = 2;
*p = &j;
printf("%d ", **p);
}
a) 2 2
b) 2 97
c) Undefined behaviour
d) Compilation Error
View Answer
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?
#include <stdio.h>
int main()
{
int i = 11;
int *p = &i;
foo(&p);
printf("%d ", *p);
}
void foo(int *const *p)
{
int j = 10;
*p = &j;
printf("%d ", **p);
}
a) Compile time error
b) 10 10
c) Undefined behaviour
d) 10 11
View Answer
Explanation: None.
7. What will be the output of the following C code?
#include <stdio.h>
void foo(int *);
int main()
{
int i = 10;
int *p = &i;
foo(p);
printf("%d ", *p);
}
void foo(int *p)
{
int j = 11;
p = &j;
printf("%d ", *p);
}
a) 11 11
b) 11 10
c) Compile time error
d) Undefined-value
View Answer
Explanation: None.
8. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int i = 10;
int *const p = &i;
foo(&p);
printf("%d\n", *p);
}
void foo(int **p)
{
int j = 11;
*p = &j;
printf("%d\n", **p);
}
a) 11 11
b) 11 10
c) Compile time error
d) Undefined behaviour
View Answer
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
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.
- Apply for C Internship
- Practice BCA MCQs
- Watch Advanced C Programming Videos
- Apply for Computer Science Internship
- Practice Computer Science MCQs