Here is a listing of C++ interview questions on “Macros” along with answers, explanations and/or solutions:
1. which keyword is used to define the macros in c++?
a) macro
b) define
c) #define
d) none of the mentioned
View Answer
Explanation: None.
2. Which symbol is used to declare the preprocessor directives?
a) #
b) $
c) *
d) ^
View Answer
Explanation: None.
3. How many types of macros are there in c++?
a) 1
b) 2
c) 3
4) 4
View Answer
Explanation: There are two types of macros. They are object-like and function-like.
4. What is the mandatory preprosessor directive for c++?
a) #define <iostream>
b) #include <iostream>
c) #undef <iostream>
d) none of the mentioned
View Answer
Explanation: For a c++ program to execute, we need #include<iostream>.
5. What is the output of this program?
#include <iostream>
using namespace std;
#define MIN(a,b) (((a)<(b)) ? a : b)
int main ()
{
float i, j;
i = 100.1;
j = 100.01;
cout <<"The minimum is " << MIN(i, j) << endl;
return 0;
}
a) 100.01
b) 100.1
c) compile time error
d) none of the mentioned
View Answer
Explanation: In this program, we are getting the minimum number using conditional operator.
Output:
$ g++ mac3.cpp
$ a.out
The minimum value is 100.01
6. What is the output of this program?
#include <iostream>
using namespace std;
int main ()
{
cout << "Value of __LINE__ : " << __LINE__ << endl;
cout << "Value of __FILE__ : " << __FILE__ << endl;
cout << "Value of __DATE__ : " << __DATE__ << endl;
cout << "Value of __TIME__ : " << __TIME__ << endl;
return 0;
}
a) 5
b) details about your file
c) compile time error
d) none of the mentioned
View Answer
Explanation: In this program, we are using the macros to print the information about the file.
Output:
$ g++ mac2.cpp
$ a.out
Value of __LINE__ : 5
Value of __FILE__ : mac1.cpp
Value of __DATE__ : Oct 10 2012
Value of __TIME__ : 22:24:37
7. What is the output of this program?
#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) none of the mentioned
View Answer
Explanation: In this program, as we haven’t initiailzed the variable x, we will get a output of ending digit of 4.
Output:
$ g++ mac1.cpp
$ a.out
75386824
8. What is the output of this program?
#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) none of the mentioned
View Answer
Explanation: In this program, we are just printing the declared values.
Output:
$ g++ mac.cpp
$ a.out
10
9. What is the output of this program?
#include <iostream>
using namespace std;
#define MAX 10
int main()
{
int num;
num = ++MAX;
cout << num;
return 0;
}
a) 11
b) 10
c) compile time error
d) none of the mentioned
View Answer
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) none of the mentioned
View Answer
Explanation: When the compiler encounters a previously defined macro, it will take the result from that execution itself.
To practice all features of C++ programming language, here is complete set on 1000+ Multiple Choice Questions and Answers on C++.