This set of Arduino Multiple Choice Questions & Answers (MCQs) focuses on “The setup() Function”.
1. How many times does the setup() function run on every startup of the Arduino System?
a) 1
b) 2
c) 3
d) 4
View Answer
Explanation: The setup() function is used predominantly to configure the pins, variables, Serial data, etc. and is executed only once throughout the entire cycle of the program. However other than the above-mentioned uses, it can also be used to execute technically all aspects of an Arduino program, but since it only runs one time, it’s not very useful for anything other than configuring.
2. Can the setup() function change the value of constant variables?
a) Yes, it can change
b) No, it cannot change
c) Yes, it can change but only integer values
d) Yes, it can change but only byte values
View Answer
Explanation: In C/C++ Programming, once a constant is declared and initialized, its value cannot be further altered throughout the entire lifecycle of the program. Thus here the setup() function also follows through the same rule since the Arduino Programming Language is a working subset of the C++ Programming Language.
3. Is it syntactically correct to write the loop() function over the setup() function while writing an Arduino program?
a) No, it is not syntactically correct
b) Yes, it is syntactically correct but will result in the loop() method executing first
c) Yes, it is syntactically correct and the setup() function will execute first
d) It is syntactically correct but logically wrong
View Answer
Explanation: The Arduino programming language is a subset of the C++ programming language, so therefore it supports a bottom-up approach and is an object-oriented programming language, making it irrelevant where the functions are declared.
4. Given below are 2 snippets of code. Which one(s) are/is correct in order to generate an output signal of the form given below?
a.
int pin1=11;
void setup() {
pinMode(pin1,OUTPUT);
digitalWrite(pin1,LOW);
}
void loop() {
int i;
for(i=1;i<=4;i++) {
if(i%2==0) {
digitalWrite(pin1,LOW)
}
else {
digitalWrite(pin1,HIGH)
}
}
}
b.
int pin1=11;
void setup() {
pinMode(pin1,OUTPUT);
int i;
for(i=1;i<=5;i++) {
if(i%2==0) {
digitalWrite(pin1,HIGH);
}
else {
digitalWrite(pin1,LOW);
}
}
}
void loop() {
//Do nothing
}
a) Only a
b) Only b
c) Neither a nor b
d) Both a and b
View Answer
Explanation: Both the codes will execute and give the same results. Here we needed 5 iterations of the loop to make the output possible. So in ‘a’, we did one iteration in the setup() function, which executed only once and then passed onto the loop() function where we carried out the remaining 4 iterations. And in ‘b’, we carried out all the 5 iterations in the setup() function itself. In ‘a’, we had to put up a HIGH signal with every odd value of the iteration variable, while in ‘b’, we had to put up a HIGH signal for every even value of the iteration variable.
5. What is the output of the following code if the input voltage to pin1 is 0V,3.3V, 3.2V, and 5V (assuming 0V – 0, 3.2V – 492, 3.3V – 501, 5V -1023)?
int pin1=11;
int pin2=12;
void setup() {
pinMode(pin1, INPUT);
pinMode(pin2, OUTPUT);
}
void loop() {
if(analogRead(pin1)>=500) {
digitalWrite(pin2,HIGH);
}
else {
digitalWrite(pin2,LOW);
}
}
a) 0101
b) 1011
c) 1100
d) 1111
View Answer
Explanation: The code sets the 2 pins as input and output ports in the setup() function. Then proceeds to set a threshold in the loop() function. According to the threshold voltages we know that Arduino maps the voltages to a positive integer set of 0 to 1023. So, we take that as a reference and create a digital signal pattern with the second pin as the output port.
6. What are the errors in the code given below?
void Setup() {
pinMode(‘A0’,OUTPUT);
analogwrite(100,’A0’);
}
void loop() {
//Do Nothing
}
a) 1
b) 2
c) 3
d) 4
View Answer
Explanation: There are 3 errors in the code given above. They are present in lines 1 and 3. Here, the setup() function’s name is spelled incorrectly with a capital ‘S’, in line 3 the arguments of the analogWrite() function are swapped, and the function’s name is again spelled incorrectly as the ‘w’ should be a capital letter.
7. What do we need to do if we want to run the setup() function in an infinite loop?
a) Call the setup() function from a custom named function
b) Call the setup() function from a constructor
c) Call the setup() function from the loop() function
d) Call the setup() function from the destructor
View Answer
Explanation: The setup() function is designed to run only at the startup of the Arduino code, in order to initialize and configure the setup before running the actual code. However it is theoretically possible to run the setup() function infinitely by calling it from the loop() function as illustrated below…
int pin1=11;
void setup() {
pinMode(pin1, OUTPUT);
}
void loop() {
setup()
}
8. How many times will the code run?
int l_var=1;
void setup() {
l_var++;
if(l_var<=5) {
setup()
}
else {
//Do Nothing.
}
}
a) 1
b) 3
c) 5
d) 7
View Answer
Explanation: In the code above the setup() function is called from itself in a recursive manner. A recursion occurs when a function call statement is issued from within the scope of the same function itself then setting up a loop. It is an alternative to another looping technique called iteration.
9. If we would require a technique which would be able to run the setup() function for ‘n’ times, which one would we use?
A-> Iterative looping B-> Recursive looping C-> Goto Function
Choose the correct Combination that works.
a) BC
b) A
c) C
d) B
View Answer
Explanation: Here the best option to use would be the iterative looping technique. This is because it has both efficient memory management and an easy to use implementation. Recursive looping can be used when there is no urgent need to consolidate and minimize memory usage for a project. However, the Goto function is generally not preferred since it is a very complex way to go about looping and is very difficult for other programmers to decipher the program after development.
10. What is the signal generated by the code at ‘y’ below if the input to ‘x’ is given as 1100?
int x=11;
int y=12;
void setup() {
Serial.begin(9600);
st:
pinMode(x,INPUT);
pinMode(y,OUPUT);
int x1=digitalRead(x);
if(x1==1) {
goto outfunc;
}
else {
goto ex;
}
outfunc:
digitalWrite(y,1);
goto st;
ex:
Serial.println(“Exited loop!”);
}
void loop() {
//Do nothing.
}
a) 11
b) 10
c) 00
d) 01
View Answer
Explanation: The code makes use of only the setup() function to create a signal output without relying on the loop() function or any iterative structure. It uses the “goto” function to change the flow of control of the program.
Sanfoundry Global Education & Learning Series – Arduino.
To practice all areas of Arduino, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check Arduino Books
- Practice Electrical & Electronics Engineering MCQs
- Practice Electrical Engineering MCQs
- Check Electrical Engineering Books
- Apply for Electrical & Electronics Engineering Internship