Arduino Questions and Answers – The loop() Function

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

1. How many times does the loop() function run on every startup of the Arduino System?
a) 1
b) Depends upon the setup() function
c) Infinitely till the power is supplied to the Arduino
d) 3
View Answer

Answer: c
Explanation: The loop() function is generally used to carry out most of the calculations that are required to sustain the working of the Arduino project. It is preprogrammed to run continuously once the Arduino is successfully booted and the setup() function has been executed once.

2. Can the loop() function be used to call another function that is custom defined by the programmer?
a) Yes, it can call
b) No, it cannot call
c) Yes, it can call but only functions with no return values
d) Yes, it can call but only functions with return values
View Answer

Answer: a
Explanation: In C/C++ Programming, a function can call any other function whether it be the main function or any other custom defined function. In the case of the Arduino, the setup() and the loop() functions are custom predefined functions that are a part of the Arduino programming language subsystem.

3. Is the loop() function a replacement of the main() function in the Arduino Programming Environment?
a) No, it is a replacement for the constructor
b) Yes, it is a replacement
c) Yes, but only for certain Arduino Boards
d) No, it is not a replacement
View Answer

Answer: d
Explanation: The Arduino programming language is a subset of the C++ programming language, so the main function is also present here. However during the active development of any project using the Arduino Environment we do not encounter the main function()’s use since it is prebuilt for that purpose to make the development simple, so that instead of worrying about linking the correct system functions to the main function during the development cycle of the project, the programmer only needs to worry about the actual working of his/her project.
advertisement
advertisement

4. What is the output of the output on the Serial Monitor for the program given below if the input supplied to pin1 is of the form 0111?

  1. int pin1=11;
  2. void setup() {
  3.     pinMode(pin1,INPUT);
  4.     Serial.begin(9600);
  5. }
  6. void loop() {
  7.     if(digitalRead(pin1)==1) {
  8.         Serial.print(“1 0”);
  9.     }
  10.     else {
  11.         Serial.print(“-1 0”);
  12.     }
  13. }

a) -1 0 1 1
b) -1 0 0 1 0 1 0
c) 1 0 1 1 0 1 1
d) -1 1 1 0 1 1 0
View Answer

Answer: b
Explanation: The above code prints a 1 and a 0 for every digital HIGH signal that it receives and prints a -1 and 0 for every digital LOW that it receives. This is the premise for the method of RZ Encoding. It is used in telecommunications for sending and receiving signals.
Note: Join free Sanfoundry classes at Telegram or Youtube

5. How many times does the code give a digital HIGH signal at pin1?

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

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

Answer: b
Explanation: The code takes the global variable ‘x’ and sets it as the lower limit or the starting point for the for-loop. Then whenever the value of ‘x’ becomes an even number it increments the value of ‘x’ and then calls the loop() function again thus the control returns to line 7 and starts executing from there. This is an example for nested loops using both recursive and iterative methods.
advertisement

6. Can a programmer not use the loop function at all in a code?
a) Yes
b) No
c) Yes, but it needs to be called at least once
d) Yes, but it needs to be commented out
View Answer

Answer: a
Explanation: The loop function is a custom predefined function that was designed to execute code over and over again for the Arduino. However if a programmer can replicate that same functionality in his/her code without the use of the loop() function then there is compulsion for him/her to include the loop() function in the program.

7. Can the loop() function be used to replace the functionality of the setup() function?
a) Yes, it can
b) No, it cannot
c) Yes, it can, but only for certain Arduino Boards
d) Yes, it can, but only for a limited time frame
View Answer

Answer: a
Explanation: Yes, the loop() function can replace the setup() function completely, theoretically, since both the loop() function and the setup() functions are custom predefined functions in the Arduino Environment. However, this would result in more work from the developer’s end since he/she would have to find a way to make configuration code run only once.

8. What is the output given by the code?

  1. int pin1=11;
  2. int x=1;
  3. void setup() {
  4.     //Do nothing
  5. }
  6. void loop() {
  7.     if(x==1) {
  8.         pinMode(pin1,OUTPUT);
  9.         Serial.begin(9600);
  10.     }
  11.     x=2;
  12.     digitalWrite(pin1,HIGH);
  13.     digitalWrite(pin1,LOW);
  14. }

a) Infinite Series of 010101…
b) Finite Series of 11111….
c) Finite Series of 0000…
d) Infinite Series of 1010101….
View Answer

Answer: d
Explanation: The code above does the process of configuration of pin1 in within the loop() function itself without using the setup() function for anything. It then proceeds to generate a digital HIGH signal followed by a digital LOW signal continuously.

9. How to make the loop function run only once but the code inside run infinitely?
a) Create a recursive function call statement
b) Create a nested infinite loop within the loop() function
c) Call the setup() function from the loop() function
d) Call the loop() function from the setup() function
View Answer

Answer: b
Explanation: Yes, it is possible to make the loop() function execute only once while running the code infinitely. This can be done by using an infinite loop within the loop() function so that the code never goes out of the loop and thus never allowing the loop() function to iterate.

10. Can the loop() function be called recursively?
a) Yes, it can be called recursively
b) No, it cannot be called recursively
c) Yes, it can be called recursively but only by the setup() function
d) Yes, it can be called recursively but only twice
View Answer

Answer: a
Explanation: The Arduino Programming Language is a working subsystem of the C++ Programming Language. This allows for any function to be called recursively at any point in the program. Since the loop() function is a custom predefined function in the Arduino Ecosystem, it too has to follow that rule.

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.