Here is a listing of C programming questions on “Pointers to Functions” along with answers, explanations and/or solutions:
1. What will be the output of the following 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
Answer: b
Clarification: None.
2. What will be the output of the following 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
Answer: c
Clarification: None.
3. 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
Answer: d
Clarification: None.
4. What will be the output of the following C code?
-
#include -
void f(int);
-
void (*foo)() = f;
-
int main(int argc, char *argv[])
-
{ -
foo(10);
-
return 0;
-
} -
void f(int i)
-
{ -
printf("%dn", i);
-
}
a) Compile time error
b) 10
c) Undefined behaviour
d) None of the mentioned
Answer: b
Clarification: None.
5. What will be the output of the following 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("%dn", i);
-
}
a) Compile time error
b) 10
c) Undefined behaviour
d) None of the mentioned
Answer: a
Clarification: None.
6. What will be the output of the following C code?
-
#include -
void f(int);
-
void (*foo)(float) = f;
-
int main()
-
{ -
foo(10);
-
} -
void f(int i)
-
{ -
printf("%dn", i);
-
}
a) Compile time error
b) 10
c) 10.000000
d) Undefined behaviour
Answer: d
Clarification: None.
7. What will be the output of the following 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("%dn", i);
-
return i;
-
}
a) Compile time error
b) Undefined behaviour
c) 10 11
d) 10 Segmentation fault
Answer: d
Clarification: None.
8. What will be the output of the following 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("%dn", i);
-
return i;
-
}
a) 10 11
b) 11
c) 10
d) Undefined behaviour
Answer: b
Clarification: None.
