C++ Questions and Answers - Macros

1. which of the following can be passed in function pointers?
a) variables
b) data types
c) functions
d) objects

  Discussion

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

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

  Discussion

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

3. which keyword is used to define the macros in c++?
a) macro
b) define
c) #define
d) #macro

  Discussion

Answer: c
Explanation: #define is the keyword which is used to define the macros in c++.

4. Which symbol is used to declare the preprocessor directives?
a) #
b) $
c) *
d) ^

  Discussion

Answer: a
Explanation: # symbol is used to declare the preprocessor directives.

5. How many types of macros are there in c++?
a) 1
b) 2
c) 3
d) 4

  Discussion

Answer: b
Explanation: There are two types of macros. They are object-like and function-like.

6. What is the mandatory preprocessor directive for c++?
a) #define <iostream>
b) #include <iostream>
c) #undef <iostream>
d) #macro <iostream>

  Discussion

Answer: b
Explanation: For a c++ program to execute, we need #include.

7. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
#define SquareOf(x) x * x
int main()
{
int x;
cout << SquareOf(x + 4);
return 0;
}
a) 16
b) 64
c) compile time error
d) 75386824

  Discussion

Answer: d
Explanation: In this program, as we have not initialize the variable x, we will get a output of ending digit of 4.
Output:
75386824

8. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
#define PR(id) cout << "The value of " #id " is "<< id
int main()
{
int i = 10;
PR(i);
return 0;
}
a) 10
b) 15
c) 20
d) 12

  Discussion

Answer: a
Explanation: In this program, we are just printing the declared values.
Output:
10

9. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
#define MAX 10
int main()
{
int num;
num = ++MAX;
cout << num;
return 0;
}
a) 10
b) 11
c) compile time error
d) 13

  Discussion

Answer: c
Explanation: Macro Preprocessor only replaces occurance of macro symbol with macro symbol value, So we can’t increment the value.

10. What is the other name of the macro?
a) scripted directive
b) executed directive
c) link directive
d) executed & link directive

  Discussion

Answer: a
Explanation: When the compiler encounters a previously defined macro, it will take the result from that execution itself.