C++ Programming MCQ – Macros

This section on C++ interview questions and answers focuses on “Macros”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ interview questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.

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

Answer: c
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

Answer: a
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

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

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

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

5. What will be the output of the following C++ code?

Note: Join free Sanfoundry classes at Telegram or Youtube
  1.     #include <iostream>
  2.     using namespace std;
  3.     #define MIN(a,b) (((a)<(b)) ? a : b)
  4.     int main ()
  5.     {
  6.         float i, j;
  7.         i = 100.1;
  8.         j = 100.01;
  9.         cout <<"The minimum is " << MIN(i, j) << endl;
  10.         return 0;
  11.     }

a) 100.01
b) 100.1
c) compile time error
d) 100
View Answer

Answer: a
Explanation: In this program, we are getting the minimum number using conditional operator.
Output:

advertisement
$ g++ mac3.cpp
$ a.out
The minimum value is 100.01

6. What will be the output of the following C++ code?

advertisement
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main ()
  4.     {
  5.         cout << "Value of __LINE__ : " << __LINE__ << endl;
  6.         cout << "Value of __FILE__ : " << __FILE__ << endl;
  7.         cout << "Value of __DATE__ : " << __DATE__ << endl;
  8.         cout << "Value of __TIME__ : " << __TIME__ << endl;
  9.         return 0;
  10.     }

a) 5
b) details about your file
c) compile time error
d) runtime error
View Answer

Answer: b
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?

  1.     #include <iostream>
  2.     using namespace std;
  3.     #define SquareOf(x) x * x
  4.     int main()
  5.     {
  6.         int x;
  7.         cout << SquareOf(x + 4);
  8.         return 0;
  9.     }

a) 16
b) 64
c) compile time error
d) 75386824
View Answer

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:

$ g++ mac1.cpp
$ a.out
75386824

8. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     using namespace std;
  3.     #define PR(id) cout << "The value of " #id " is "<<id
  4.     int main()
  5.     {
  6.         int i = 10;
  7.         PR(i);
  8.         return 0;
  9.     }

a) 10
b) 15
c) 20
d) 12
View Answer

Answer: a
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?

  1.     #include <iostream>
  2.     using namespace std;
  3.     #define MAX 10
  4.     int main()
  5.     {
  6.         int num;
  7.         num = ++MAX;
  8.         cout << num;
  9.         return 0;
  10.     }

a) 11
b) 10
c) compile time error
d) 13
View Answer

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
View Answer

Answer: a
Explanation: When the compiler encounters a previously defined macro, it will take the result from that execution itself.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.