C++ Programming Questions and Answers – Pointer to Function

«
»
This section on C++ questions and puzzles focuses 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
View Answer

Answer: c
Explanation: A function pointer points to a function.
advertisement

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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: a
Explanation: __cdecl is the default calling convention for a compiler in c++.

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

  1.     #include <iostream>
  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
View Answer

Answer: c
Explanation: In this program, we are adding two numbers with 15, So we got the output as 40.
Output:

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

advertisement

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

  1.     #include <iostream>
  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
View Answer

Answer: d
Explanation: As we are calling the function two times with the same value, So it is printing as 22.
Output:

$ g++ pfu.cpp
$ a.out
22

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

  1.     #include <iostream>
  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
advertisement

b) d9d9
c) d9
d) compile time error
View Answer

Answer: a
Explanation: 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 <iostream>
  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
View Answer

Answer: d
Explanation: 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) $
View Answer

Answer: c
Explanation: 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
View Answer

Answer: c
Explanation: Only functions are passed in function pointers.
advertisement

10. What is the meaning of the following declaration?

int(*ptr[5])();

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
View Answer

Answer: b
Explanation: In this expression, ptr is array not pointer.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn