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 (interviews, walk-in interviews, company interviews) 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.
C++ questions and puzzles on “Pointer to Function” along with answers, explanations and/or solutions:
Q 1. To which does the function pointer point to?
A. variable
B. function
C. constants
D. absolute variables
Show Answer
Answer:-B. functionExplanation
A function pointer points to a function.Q 2. What will we not do with function pointers?
A. allocation of memory
B. deallocation of memory
C. finds memory status
D. both allocation & deallocation of memory
Show Answer
Answer:-D. both allocation & deallocation of memoryExplanation
As it is used to execute a block of code, So we will not allocate or deallocate memory.Q 3. What is the default calling convention for a compiler in c++?
A. __stdcall
B. __cdecl
C. __pascal
D. __fastcall
Show Answer
Answer:-B. __cdeclExplanation
__cdecl is the default calling convention for a compiler in c++. advertisementQ 4. What will be the output of the following C++ code?
include 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
Show Answer
Answer:-C. 40Explanation
In this program, we are adding two numbers with 15, So we got the output as 40. Output: Subscribe Now: C++ Newsletter | Important Subjects Newsletters $ g++ pfu2.cpp $ a.out 40Q 5. What will be the output of the following C++ code?
include using namespace std;
void func(int x)
{
cout << x ;
}
int main()
{
void (n)(int); n = &func; (n)( 2 );
n( 2 );
return 0;
}
A. 19
B. 20
C. 21
D. 22
Show Answer
Answer:-D. 22Explanation
As we are calling the function two times with the same value, So it is printing as 22. Output: $ g++ pfu.cpp $ a.out 22Q 6. What will be the output of the following C++ code?
include 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. d99
B. d9d9
C. d9
D. compile time error
Show Answer
Answer:-A. d99Explanation
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 9Q 7. What will be the output of the following C++ code?
include 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. 2329
C. 230
D. compile time error
Show Answer
Answer:-D. compile time errorExplanation
In this program, we can’t do the casting from char to int, So it is raising an error.Q 8. What is the mandatory part to present in function pointers?
A. &
B. return values
C. data types
D. $
Show Answer
Answer:-C. data typesExplanation
The data types are mandatory for declaring the variables in the function pointers.Q 9. Which of the following can be passed in function pointers?
A. variables
B. data types
C. objects
D. functions
Show Answer
Answer:-D. functionsExplanation
Only functions are passed in function pointers.Q 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
Leave a Reply