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) #macro
Answer: c
Clarification: #define is the keyword which is used to define the macros in c++.
2. Which symbol is used to declare the preprocessor directives?
a) #
b) $
c) *
d) ^
Answer: a
Clarification: # symbol is used to declare the preprocessor directives.
3. How many types of macros are there in c++?
a) 1
b) 2
c) 3
d) 4
Answer: b
Clarification: There are two types of macros. They are object-like and function-like.
4. What is the mandatory preprocessor directive for c++?
a) #define
b) #include
c) #undef
d) #macro
Answer: b
Clarification: For a c++ program to execute, we need #include
5. What will be the output of the following C++ code?
-
#include -
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) 100
Answer: a
Clarification: 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 will be the output of the following C++ code?
-
#include -
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) runtime error
Answer: b
Clarification: 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
