250+ TOP MCQs on Pointer to Function and Answers

This section on C++ questions and puzzles on “Pointer to Function”. One shall practice these questions and puzzles to improve their C++ programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams. These programming puzzles can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ questions and puzzles on “Pointer to Function” along with answers, explanations and/or solutions:

1. To which does the function pointer point to?
a) variable
b) constants
c) function
d) absolute variables
Answer: c
Clarification: A function pointer points to a function.

2. What will we not do with function pointers?
a) allocation of memory
b) deallocation of memory
c) both allocation & deallocation of memory
d) finds memory status
Answer: c
Clarification: As it is used to execute a block of code, So we will not allocate or deallocate memory.

3. What is the default calling convention for a compiler in c++?
a) __cdecl
b) __stdcall
c) __pascal
d) __fastcall
Answer: a
Clarification: __cdecl is the default calling convention for a compiler in c++.

4. What will be the output of the following C++ code?

  1.     #include 
  2.     using namespace std;
  3.     int add(int first, int second)
  4.     {
  5.         return first + second + 15;
  6.     }
  7.     int operation(int first, int second, int (*functocall)(int, int))
  8.     {
  9.         return (*functocall)(first, second);
  10.     }
  11.     int main()
  12.     {
  13.         int  a;
  14.         int  (*plus)(int, int) = add;
  15.         a = operation(15, 10, plus);
  16.         cout << a;
  17.         return 0;
  18.     }

a) 25
b) 35
c) 40
d) 45
Answer: c
Clarification: In this program, we are adding two numbers with 15, So we got the output as 40.
Output:

$ g++ pfu2.cpp
$ a.out
40

5. What will be the output of the following C++ code?

  1.     #include 
  2.     using namespace std;
  3.     void func(int x)
  4.     {
  5.         cout << x ;
  6.     }
  7.     int main()
  8.     {
  9.         void (*n)(int);
  10.         n = &func;
  11.         (*n)( 2 );
  12.         n( 2 );
  13.         return 0;
  14.     }

a) 2
b) 20
c) 21
d) 22
Answer: d
Clarification: As we are calling the function two times with the same value, So it is printing as 22.
Output:

6. What will be the output of the following C++ code?

  1.     #include 
  2.     using namespace std;
  3.     int n(char, int);
  4.     int (*p) (char, int) = n;
  5.     int main()
  6.     {
  7.         (*p)('d', 9);
  8.         p(10, 9);
  9.         return 0;
  10.     }
  11.     int n(char c, int i)
  12.     {
  13.         cout << c <<  i;
  14.         return 0;
  15.     }

a)

d9
9

b) d9d9
c) d9
d) compile time error
Answer: a
Clarification: As function pointer p is pointing to n(char, int), so for first call d9 will be printed for second call 10, which corresponds to ‘n’ character, and then 9 is printed.
Output:

$ g++ pfu1.cpp
$ a.out
d9
9

7. What will be the output of the following C++ code?

  1.     #include 
  2.     using namespace std;
  3.     int func (int a, int b)
  4.     {
  5.         cout << a;
  6.         cout << b;
  7.         return 0;
  8.     }
  9.     int main(void)
  10.     {
  11.         int(*ptr)(char, int);
  12.         ptr = func;
  13.         func(2, 3);
  14.         ptr(2, 3);
  15.         return 0;
  16.     }

a) 2323
b) 232
c) 23
d) compile time error
Answer: d
Clarification: In this program, we can’t do the casting from char to int, So it is raising an error.

8. What is the mandatory part to present in function pointers?
a) &
b) return values
c) data types
d) $
Answer: c
Clarification: The data types are mandatory for declaring the variables in the function pointers.

9. which of the following can be passed in function pointers?
a) variables
b) data types
c) functions
d) objects
Answer: c
Clarification: Only functions are passed in function pointers.

10. What is the meaning of the following declaration?

a) ptr is pointer to function
b) ptr is array of pointer to function
c) ptr is pointer to such function which return type is array
d) ptr is pointer to array of function
Answer: b
Clarification: In this expression, ptr is array not pointer.

  250+ TOP MCQs on Pointers and Answers

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top