C Programming Questions and Answers on Pointers to Functions for Freshers

https://www.computersprofessor.com/2017/12/c-programming-questions-and-answers-on_23.html
1. Which function is not called in the following program?
#include
void first()
{
printf("first");
}
void second()
{
first();
}
void third()
{
second();
}
void main()
{
void (*ptr)();
ptr = third;
ptr();
}
a) Function first
b) Function second
c) Function third
d) None of the mentioned
b) Function second
c) Function third
d) None of the mentioned
Answer: d
2. How to call a function without using the function name to send parameters?
a) typedefs
b) Function pointer
c) Both typedefs and Function pointer
d) None of the mentioned
Answer: b
3. Correct syntax to pass a Function Pointer as an argument
a) void pass(int (*fptr)(int, float, char)){}
b) void pass(*fptr(int, float, char)){}
c) void pass(int (*fptr)){}
d) void pass(*fptr){}
Answer: a
4. Which of the following is not possible in C?
a) Array of function pointer
b) Returning a function pointer
c) Comparison of function pointer
d) None of the mentioned
Answer: d
5. What is the output of this C code?
#include
void first()
{
printf("Hello World");
}
void main()
{
void *ptr() = first;
ptr++
ptr();
}
a) Illegal application of ++ to void data type
b) pointer function initialized like a variable
c) Illegal application of ++ to void data type & pointer function initialized like a variable
d) None of the mentioned
b) pointer function initialized like a variable
c) Illegal application of ++ to void data type & pointer function initialized like a variable
d) None of the mentioned
Answer: c
6. What is the output of this C code?
#include
int mul(int a, int b, int c)
{
return a * b * c;
}
void main()
{
int (*function_pointer)(int, int, int);
function_pointer = mul;
printf("The product of three numbers is:%d",
function_pointer(2, 3, 4));
}
a) The product of three numbers is:24
b) Run time error
c) Nothing
d) Varies
b) Run time error
c) Nothing
d) Varies
Answer: a
7. What is the output of this C code?
#include
int mul(int a, int b, int c)
{
return a * b * c;
}
void main()
{
int (function_pointer)(int, int, int);
function_pointer = mul;
printf("The product of three numbers is:%d",
function_pointer(2, 3, 4));
}
a) The product of three numbers is:24
b) Compile time error
c) Nothing
d) Varies
b) Compile time error
c) Nothing
d) Varies
Answer: b
8. What is the output of this C code?
#include
void f(int (*x)(int));
int myfoo(int);
int (*fooptr)(int);
int ((*foo(int)))(int);
int main()
{
fooptr = foo(0);
fooptr(10);
}
int ((*foo(int i)))(int)
{
return myfoo;
}
int myfoo(int i)
{
printf("%d\n", i + 1);
}
a) 10
b) 11
c) Compile time error
d) Undefined behaviour
b) 11
c) Compile time error
d) Undefined behaviour
Answer: b
9. What is the output of this C code?
#include
int mul(int a, int b, int c)
{
return a * b * c;
}
void main()
{
int *function_pointer;
function_pointer = mul;
printf("The product of three numbers is:%d",
function_pointer(2, 3, 4));
}
a) The product of three numbers is:24
b) Compile time error
c) Nothing
d) Varies
b) Compile time error
c) Nothing
d) Varies
Answer: b
10. What is the output of this C code?
#include
int sub(int a, int b, int c)
{
return a - b - c;
}
void main()
{
int (*function_pointer)(int, int, int);
function_pointer = ⊂
printf("The difference of three numbers is:%d",
(*function_pointer)(2, 3, 4));
}
a) The difference of three numbers is:1
b) Run time error
c) The difference of three numbers is:-5
d) Varies
b) Run time error
c) The difference of three numbers is:-5
d) Varies
Answer: c
11. One of the uses for function pointers in C is
a) Nothing
b) There are no function pointers in c
c) To invoke a function
d) To call a function defined at run-time.
a) Nothing
b) There are no function pointers in c
c) To invoke a function
d) To call a function defined at run-time.
Answer: d
12. What is the output of this C code?
#include
void f(int);
void (*foo)() = f;
int main(int argc, char *argv[])
{
foo(10);
return 0;
}
void f(int i)
{
printf("%d\n", i);
}
a) Compile time error
b) 10
c) Undefined behaviour
d) None of the mentioned
b) 10
c) Undefined behaviour
d) None of the mentioned
Answer: b
13. What is the output of this C code?
#include
void f(int);
void (*foo)(void) = f;
int main(int argc, char *argv[])
{
foo(10);
return 0;
}
void f(int i)
{
printf("%d\n", i);
}
a) Compile time error
b) 10
c) Undefined behaviour
d) None of the mentioned
b) 10
c) Undefined behaviour
d) None of the mentioned
Answer: a
14. What is the output of this C code?
#include
void f(int);
void (*foo)(float) = f;
int main()
{
foo(10);
}
void f(int i)
{
printf("%d\n", i);
}
a) Compile time error
b) 10
c) 10.000000
d) Undefined behaviour
b) 10
c) 10.000000
d) Undefined behaviour
Answer: d
15. What is the output of this C code?
#include
void f(int (*x)(int));
int myfoo(int i);
int (*foo)(int) = myfoo;
int main()
{
f(foo(10));
}
void f(int (*i)(int))
{
i(11);
}
int myfoo(int i)
{
printf("%d\n", i);
return i;
}
a) Compile time error
b) Undefined behaviour
c) 10 11
d) 10 Segmentation fault
b) Undefined behaviour
c) 10 11
d) 10 Segmentation fault
Answer: d
16. What is the output of this C code?
#include
void f(int (*x)(int));
int myfoo(int);
int (*foo)() = myfoo;
int main()
{
f(foo);
}
void f(int(*i)(int ))
{
i(11);
}
int myfoo(int i)
{
printf("%d\n", i);
return i;
}
a) 10 11
b) 11
c) 10
d) Undefined behaviour
b) 11
c) 10
d) Undefined behaviour
Answer: b