Here is a listing of C multiple choice questions on “Pointers and Function Arguments” along with answers, explanations and/or solutions:
1. What will be the output of the following C code?
-
#include
-
void foo(int*);
-
int main()
-
{
-
int i = 10;
-
foo((&i)++);
-
}
-
void foo(int *p)
-
{
-
printf("%dn", *p);
-
}
a) 10
b) Some garbage value
c) Compile time error
d) Segmentation fault/code crash
Answer: c
Clarification: None.
2. What will be the output of the following C code?
-
#include
-
void foo(int*);
-
int main()
-
{
-
int i = 10, *p = &i;
-
foo(p++);
-
}
-
void foo(int *p)
-
{
-
printf("%dn", *p);
-
}
a) 10
b) Some garbage value
c) Compile time error
d) Segmentation fault
Answer: a
Clarification: None.
3. What will be the output of the following C code?
-
#include
-
void foo(float *);
-
int main()
-
{
-
int i = 10, *p = &i;
-
foo(&i);
-
}
-
void foo(float *p)
-
{
-
printf("%fn", *p);
-
}
a) 10.000000
b) 0.000000
c) Compile time error
d) Undefined behaviour
Answer: b
Clarification: None.
4. What will be the output of the following C code?
-
#include
-
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
Answer: a
Clarification: None.
5. What will be the output of the following C code?
-
#include
-
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) Segmentation fault/code crash
Answer: a
Clarification: None.
6. What will be the output of the following C code?
-
#include
-
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
Answer: a
Clarification: None.
7. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
int i = 10;
-
int *p = &i;
-
foo(&p);
-
printf("%d ", *p);
-
printf("%d ", *p);
-
}
-
void foo(int **const p)
-
{
-
int j = 11;
-
*p = &j;
-
printf("%d ", **p);
-
}
a) 11 11 11
b) 11 11 Undefined-value
c) Compile time error
d) Segmentation fault/code-crash
Answer: b
Clarification: None.
8. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
int i = 10;
-
int *const p = &i;
-
foo(&p);
-
printf("%dn", *p);
-
}
-
void foo(int **p)
-
{
-
int j = 11;
-
*p = &j;
-
printf("%dn", **p);
-
}
a) 11 11
b) Undefined behaviour
c) Compile time error
d) Segmentation fault/code-crash
Answer: a
Clarification: None.
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]);
Answer: a
Clarification: None.