C++ Questions and Answers - Pointer to Function

1. What we can’t place followed by the non-default arguments?
a) trailing arguments
b) default arguments
c) both trailing & default arguments
d) leading arguments

  Discussion

Answer: b
Explanation: To avoid the ambiguity in arguments. eg. if func(int a=3, int b); so if we call func(5), here will 5 will be value of a or b, because 5 is first parameter so a should be 5 but as only one argument is given b should be 5. So to remove such ambiguity default parameters are kept at the end or rightmost side.

2. If we start our function call with default arguments means, what will be proceeding arguments?
a) user argument
b) empty arguments
c) default arguments
d) user & empty arguments

  Discussion

Answer: c
Explanation: As a rule, the default argument must be followed by default arguments only.

3. What is the default return type of a function?
a) int
b) void
c) float
d) char

  Discussion

Answer: b
Explanation: void is the default return value of any function, to handle both empty and non-empty values.

4. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int func(int m = 10, int n)
{
int c;
c = m + n;
return c;
}
int main()
{
cout << func(5);
return 0;
}
a) 15
b) 10
c) compile time error
d) 30

  Discussion

Answer: c
Explanation: In function parameters the default arguments should always be the rightmost parameters.

5. To which does the function pointer point to?
a) variable
b) constants
c) function
d) absolute variables

  Discussion

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

6. 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

  Discussion

Answer: c
Explanation: As it is used to execute a block of code, So we will not allocate or deallocate memory.

7. What is the default calling convention for a compiler in c++?
a) __cdecl
b) _stdcall
c) __pascal
d) __fastcall

  Discussion

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

8. 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

  Discussion

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

9. 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

  Discussion

Answer: d
Explanation: In this program, we can’t do the casting from char to int, So it is raising an error.

10. What is the mandatory part to present in function pointers?
a) &
b) return values
c) data types
d) $

  Discussion

Answer: c
Explanation: The data types are mandatory for declaring the variables in the function pointers.