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
View Answer
Explanation: #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) ^
View Answer
Explanation: # 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
View Answer
Explanation: There are two types of macros. They are object-like and function-like.
4. What is the mandatory preprocessor directive for c++?
a) #define <iostream>
b) #include <iostream>
c) #undef <iostream>
d) #macro <iostream>
View Answer
Explanation: For a c++ program to execute, we need #include<iostream>.
5. What will be the output of the following C++ code?
#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) 100
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 will be the output of the following C++ code?
#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) runtime error
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 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
View Answer
Explanation: In this program, as we have not initialize the variable x, we will get a output of ending digit of 4.
Output:
$ g++ mac1.cpp $ a.out 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
View Answer
Explanation: In this program, we are just printing the declared values.
Output:
$ g++ mac.cpp $ a.out 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) 11
b) 10
c) compile time error
d) 13
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) executed & link directive
View Answer
Explanation: When the compiler encounters a previously defined macro, it will take the result from that execution itself.
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Check Computer Science Books
- Practice Programming MCQs
- Apply for C++ Internship