Arduino Questions and Answers – The setup() Function

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

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

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

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

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?
Find the correct order to generate an output signal of the 2 snippets of code
a.

  1. int pin1=11;
  2. void setup() {
  3.     pinMode(pin1,OUTPUT);
  4.     digitalWrite(pin1,LOW);
  5. }
  6. void loop() {
  7.     int i;
  8.     for(i=1;i<=4;i++) {
  9.         if(i%2==0) {
  10.             digitalWrite(pin1,LOW)
  11.         }
  12.         else {
  13.             digitalWrite(pin1,HIGH)
  14.         }
  15.     }
  16. }

b.

  1. int pin1=11;
  2. void setup() {
  3.     pinMode(pin1,OUTPUT);
  4.     int i;
  5.     for(i=1;i<=5;i++) {
  6.         if(i%2==0) {
  7.             digitalWrite(pin1,HIGH);
  8.         }
  9.         else {
  10.             digitalWrite(pin1,LOW);
  11.         }
  12.     }
  13. }
  14. void loop() {
  15.     //Do nothing
  16. }

a) Only a
b) Only b
c) Neither a nor b
d) Both a and b
View Answer

Answer: d
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.
advertisement

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)?

advertisement
  1. int pin1=11;
  2. int pin2=12;
  3. void setup() {
  4.     pinMode(pin1, INPUT);
  5.     pinMode(pin2, OUTPUT);
  6. }
  7. void loop() {
  8.     if(analogRead(pin1)>=500) {
  9.         digitalWrite(pin2,HIGH);
  10.     }
  11.     else {
  12.         digitalWrite(pin2,LOW);
  13.     }
  14. }

a) 0101
b) 1011
c) 1100
d) 1111
View Answer

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

  1. void Setup() {
  2.     pinMode(‘A0’,OUTPUT);
  3.     analogwrite(100,’A0’);
  4. }
  5. void loop() {
  6.     //Do Nothing
  7. }

a) 1
b) 2
c) 3
d) 4
View Answer

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

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

  1. int pin1=11;
  2. void setup() {
  3.     pinMode(pin1, OUTPUT);
  4. }
  5. void loop() {
  6.     setup()
  7. }

8. How many times will the code run?

  1. int l_var=1;
  2. void setup() {
  3.     l_var++;
  4.     if(l_var<=5) {
  5.         setup()
  6.     }
  7.     else {
  8.         //Do Nothing.
  9.     }
  10. }

a) 1
b) 3
c) 5
d) 7
View Answer

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

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

  1. int x=11;
  2. int y=12;
  3. void setup()  {
  4.     Serial.begin(9600);
  5.     st:
  6.     pinMode(x,INPUT);
  7.     pinMode(y,OUPUT);
  8.     int x1=digitalRead(x);
  9.     if(x1==1) {
  10.         goto outfunc;
  11.     }
  12.     else {
  13.         goto ex;
  14.     }
  15.     outfunc:
  16.     digitalWrite(y,1);
  17.     goto st;
  18.     ex:
  19.     Serial.println(“Exited loop!”);
  20. }
  21. void loop() {
  22.     //Do nothing.
  23. }

a) 11
b) 10
c) 00
d) 01
View Answer

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

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.