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
Explanation: 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
View Answer
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
Explanation: __cdecl is the default calling convention for a compiler in c++.
4. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int add(int first, int second)
{
return first + second + 15;
}
int operation(int first, int second, int (*functocall)(int, int))
{
return (*functocall)(first, second);
}
int main()
{
int a;
int (*plus)(int, int) = add;
a = operation(15, 10, plus);
cout << a;
return 0;
}
a) 25
b) 35
c) 40
d) 45
View Answer
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
5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void func(int x)
{
cout << x ;
}
int main()
{
void (*n)(int);
n = &func;
(*n)( 2 );
n( 2 );
return 0;
}
a) 2
b) 20
c) 21
d) 22
View Answer
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?
#include <iostream>
using namespace std;
int n(char, int);
int (*p) (char, int) = n;
int main()
{
(*p)('d', 9);
p(10, 9);
return 0;
}
int n(char c, int i)
{
cout << c << i;
return 0;
}
a)
d9 9
b) d9d9
c) d9
d) compile time error
View Answer
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?
#include <iostream>
using namespace std;
int func (int a, int b)
{
cout << a;
cout << b;
return 0;
}
int main(void)
{
int(*ptr)(char, int);
ptr = func;
func(2, 3);
ptr(2, 3);
return 0;
}
a) 2323
b) 232
c) 23
d) compile time error
View Answer
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
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
Explanation: Only functions are passed in function pointers.
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
Explanation: In this expression, ptr is array not pointer.
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice Computer Science MCQs
- Check Computer Science Books
- Check C++ Books
- Check Programming Books
- Apply for Computer Science Internship