Arduino Questions and Answers – Preprocessing

This set of Arduino Multiple Choice Questions & Answers (MCQs) focuses on “Preprocessing”.

1. What is the #include Preprocessor Directive used for?
a) For getting operating system information
b) For performing read/write operations
c) For importing header files or other codes into the existing code
d) For compiling the written code
View Answer

Answer: c
Explanation: A preprocessing directive is any line in a code that starts with a ‘#’ symbol. Whenever there is something like this in a C or C++ code, the compiler is forced to execute that before compiling the code. They are mostly used for importing other codes or header files into the existing code base and can be also used for defining constants.

2. Check the validity of the two statements derived from the two lines of code supplied below.

#include<file.h>
#include “file.h”
i. #include<file.h> makes the preprocessor check for file.h only in directories specified by the IDE/Compiler.
ii. #include “file.h” makes the preprocessor check for file.h in the directory specified within the double quotes.
iii. Both the lines of code are the same.

a) Statements i, ii and iii are all false
b) Only Statement iii is false
c) Statements ii and iii are true
d) Only statement i is true
View Answer

Answer: b
Explanation: When the preprocessor comes across the above situation, if the file name is surrounded by a double quote then it first navigates to the specified folder or directory and then searches for the specified file. If no directory path is specified, then it searches for the file in the same directory where the existing code file is situated.
advertisement
advertisement

3. What is the use of #define?
a) To define macros
b) To define pointer variables
c) To define file names
d) To create variables
View Answer

Answer: a
Explanation: The DEFINE directive allows for the creation of constant variables whose values are to remain constant throughout the entire program. These are also called macros. A common practice or convention is to use the upper-case letters when defining such macros. For the programmer to better understand which variable is a constant and which one can change its value during the program.

4. What is the correct execution process of an Arduino code.
a) Editor->Compiler->Preprocessor
b) Editor->Preprocessor->Compiler
c) Preprocessor->Editor->Compiler
d) Compiler->Preprocessor->Editor
View Answer

Answer: b
Explanation: The code that is written is first done so with the help of the editor. Without the editor there will be no place where the programmer can write his or her code. After that the code is handed over to the preprocessor and finally the compiler which translates the code into the Arduino’s Assembly Instruction Set.
advertisement

5. What would the following code output?

#define X 10;
void setup(){
    X=0;
    Serial.begin(9600);
    Serial.print(X);
}
void loop(){
    //Do nothing…
}

a) Error
b) 0
c) 0xa
d) 0xAB
View Answer

Answer: a
Explanation: In the above code we have attempted to modify the value of X which is initialized using the preprocessing directive “define” which makes its value unchangeable throughout the entire code. Thus, the compiler will throw an error.
advertisement

6. Give the output of the following code.

#ifndef MULTIPLY
#define MULTIPLY(A, B) A * B
#endif
void setup() {
    Serial.begin(9600);
    int result = MULTIPLY(2, 6);
    Serial.print(result);
}
void loop(){
    //Do nothing...
}

a) Error
b) 12
c) 1
d) 13
View Answer

Answer: b
Explanation: In this code we used preprocessor directives to perform a simple multiplication operation. The reason for using this method over normally multiplying the variables is that this method saves processing time and memory during the execution of the program.

7. What is the way of throwing an error using preprocessing directives to the Arduino Compiler and forcing it to stop compilation?
a) #stop
b) #cut
c) #error
d) #warning
View Answer

Answer: c
Explanation: The #error directive first throws an error to the Arduino IDE which is then displayed and then the compilation process is stopped prematurely. This is used to enable the user to stop the compilation process programmatically if some criteria or condition is not met before he or she can allow the program to start running.

8. What character does the preprocessor look out for whilst searching for commands in the code?
a) x
b) @
c) #
d) !
View Answer

Answer: c
Explanation: Any special commands that need to be executed by the preprocessor is always initialized with the help of the ‘#’ symbol. This helps in enhancing the clarity of the code that the programmer writes and also makes it easier for the preprocessor to complete it’s job faster since it does not have to process each and every token throughout the program to simply find out what are the preprocessor commands that have been used.

9. #warning and #error are two preprocessor directives and the job of #warning is to throw a message to the Arduino IDE. However, which of these preprocessor directives stops the compilation process?
a) #error
b) #warning
c) #include
d) #define
View Answer

Answer: a
Explanation: As explained in the question, the job of the #warning preprocessor directive is to throw a message to the Arduino IDE as a warning to notify the user or the programmer of something. However, it does not hinder in the compilation process.

10. How many types of macros are there?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: b
Explanation: A macro is a constant variable that gets initialized in a program here with the help of a preprocessor. There are two types of macros, namely object type macros and function type macros. An object type macro is one which is simply a variable name with a value attached to it while a function type macro is one which is of the form of expression.

Sanfoundry Global Education & Learning Series – Arduino.

To practice all areas of Arduino, here is complete set of 1000+ Multiple Choice Questions and Answers.

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.