Site icon T4Tutorials.com

Pointers to functions — C++ MCQs

1. What is a pointer to a function?

(A) Pointer storing an integer value


(B) Pointer storing address of a function


(C) Pointer storing address of a variable


(D) None of these



2. How do you declare a pointer to a function returning int and taking int parameters?

(A) int *p(int);


(B) int (*p)(int);


(C) int p(*int);


(D) int &p(int);



3. If int add(int a,int b){ return a+b; }, how do you assign a pointer to it?

(A) int (*p)(int,int) = &add;


(B) int *p = add;


(C) int (*p) = add;


(D) int p(*int,int) = add;



4. Which of the following calls the function using pointer?

(A) p(5,10);


(B) (*p)(5,10);


(C) Both A and B


(D) None



5. Function pointer can be passed as an argument to another function?

(A) Yes


(B) No


(C) Only in C


(D) Only for void functions



6. What is output?
int square(int x){ return x*x; }
int main(){ int (*p)(int) = square; cout << (*p)(4); }

(A) 4


(B) 8


(C) 16


(D) 0



7. Which of the following is valid declaration of pointer to function returning void and taking no arguments?

(A) void *p();


(B) void (p)();


(C) void p();


(D) void &p();



8. Function pointers are useful for

(A) Callback functions


(B) Implementing tables of functions


(C) Event-driven programming


(D) All of the above



9. Can a pointer to a function point to NULL?

(A) Yes


(B) No


(C) Only if function returns void


(D) Only for int functions



10. Which of the following is correct for array of function pointers?

(A) int (*p[5])(int,int);


(B) int p5


(C) int p5


(D) int (*p)(5)(int,int);



11. What is the output?
int f(int x){ return x+1; }
int main(){ int (*p)(int)=f; cout << p(9); }

(A) 8


(B) 9


(C) 10


(D) Garbage



12. How do you pass function pointer to another function?

(A) By value


(B) By reference


(C) Cannot be passed


(D) Both A and B



13. Which of the following is valid to assign function pointer without & operator?

(A) int (*p)(int) = f;


(B) int (*p)(int) = &f;


(C) Both A and B


(D) None



14. Which of the following is valid to call function using pointer?

(A) p(5)


(B) (p)(5)


(C) p5


(D) Both A and B



15. Can function pointer point to member function of class?

(A) Yes, with special syntax


(B) No


(C) Only static functions


(D) Only void functions



16. Which of the following is correct typedef for function pointer returning int and taking two int parameters?

(A) typedef int (*funcPtr)(int,int);


(B) typedef int *funcPtr(int,int);


(C) typedef int funcPtr(*int,int);


(D) typedef int &funcPtr(int,int);



17. What is output?
int add(int a,int b){ return a+b; }
int main(){ int (*fp)(int,int)=add; cout << (*fp)(2,3); }

(A) 2


(B) 3


(C) 5


(D) 6



18. Which is true about function pointers?

(A) They can be reassigned to different functions of same type


(B) They store actual function code


(C) They cannot be dereferenced


(D) They can store variables



19. Function pointers are mainly used for

(A) Callback functions


(B) Event-driven programs


(C) Jump tables


(D) All of the above



Exit mobile version