# Arduino Questions and Answers – Looping

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

1. Is there any difference between exit-controlled loop structure and entry-controlled loop structure?
a) Yes
b) No

Explanation: The minimum number of times an entry-controlled loop will run if the condition fails at first attempt is 0. However, the minimum number of times an exit-controlled loop will run if the condition fails at first attempt is 1. Here the difference is that in an entry-controlled loop, the condition is checked before entry into the loop structure, while in an exit-controlled loop, the condition is checked after the completion of the loop structure.

2. What is the output of the code given below?

1. `void setup() {`
2. `    Serial.begin(9600);`
3. `}`
4. `void loop() {`
5. `    int i, j, n=3, temp;`
6. `    int a[3]={2, 1, 3};`
7. `    for(i=0;i<n-1;i++) {`
8. `        for(j=0;j<n-1-I;j++) {`
9. `            if(a[j]>a[j+1]) {`
10. `                temp=a[j];`
11. `                a[j]=a[j+1];`
12. `                a[j+1]=temp;`
13. `            }`
14. `        }`
15. `    }`
16. `    for(i=0;i<n;i++) {`
17. `        Serial.print(a[i]);`
18. `        Serial.print(“ ”);`
19. `    }`
20. `}`

a) 1 2 3
b) 3 2 1
c) 1 3 2
d) 2 1 3

Explanation: The above code demonstrates the Bubble Sort Technique using the concept of nested for-loop. Bubble Sort is a sorting technique. The version which we have used here sorts the contents (numbers) of the array in ascending order.

3. What is the purpose of the following code?

1. `void setup() {`
2. `    Serial.begin(9600);`
3. `}`
4. `void loop() {`
5. `    int a[4]={1,2,3,4};`
6. `    int search_item=2;`
7. `    int i;`
8. `    for(i=0;i<4;i++) {`
9. `        if(a[i]==search_item) {`
10. `            Serial.print(“Found!”);`
11. `        }`
12. `    }`
13. `}`

a) Search
b) Sort
c) Delete
d) Append

Explanation: The code given above performs continuously checks whether a number called “search_item” is present in the array given or not. This operation is called a Linear Search wherein the search operation sweeps the array linearly from one end to another and checks if an item is present or not.

4. What is the output of the following code?

1. `void setup() {`
2. `    Serial.begin(9600);`
3. `}`
4. `void loop() {`
5. `    int n,c,d,position,swap;`
6. `    int array[4]={1,2,4,3};`
7. `    n=4;`
8. `    for(c=0;c<n;c++) {`
9. `        position=c;`
10. `        for(d=c+1;d<n;d++) {`
11. `            if(array[position]>array[d]) {`
12. `                position=d;`
13. `	    }`
14. `	}`
15. `        if(position!=c) {`
16. `            swap=array[c];`
17. `            array[c]=array[position];`
18. `            array[position]=swap;`
19. `        }`
20. `    }`
21. `    for(c=0;c<n;c++) {`
22. `        Serial.print(array[c]);`
23. `        Serial.print(“ ”);`
24. `    }`
25. `}`

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

Explanation: The above code takes 4 integers in an array and then attempts to sort the elements in ascending order using the Selection Sort Algorithm. This is done by repeatedly finding the smallest number from the array and putting it at the beginning.

5. What are the three components of a for-loop?
a) initialization, conditional, increment/decrement
b) conditional, memory allocation, memory deletion
c) reset, increment, conditional
d) reset, increment/decrement, memory allocation

Explanation: The for-loop structure is an entry controlled looping structure. It is generally used when there is a clear starting point and ending point to the loop. Unlike the while loop in which the ending point may or may not be included in the loop structure.

6. How many times will the following loop run if the input to the code is a constant 5V signal?

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

a) 1
b) 2
c) infinitely
d) 5

Explanation: The above code is going to run infinitely since the in the increment/decrement section the condition given is i–. Which literally means that the loop will execute until i becomes 10-1, and each time the loop executes, we decrement it by 1. So in effect, i will continuously become lesser and lesser and never be equal to 10-1.

7. Which one of the control structures is similar to the if-else statement?
a) Switch-case
b) For loop
c) While loop
d) Continue

Explanation: The switch case is another type of decision-making control structure which can be used as a total replacement for the if-else structure. However, since it’s a more rigid structure, it’s mostly used for menu driven programs.

8. What is the output of the code given below if there is a constant 5V supply to pin 10?

1. `void setup() {`
2. `    Serial.begin(9600);`
3. `}`
4. `void loop() {`
5. `    int x=0;`
6. `    do {`
7. `        x=analogRead(10);`
8. `        Serial.println(x);`
9. `    }while(x!=1023);  `
10. `}`

a) 1023
b) 1024
c) null
d) 1029

Explanation: The above code takes an input and the prints the value of the voltage that is mapped to an integer set of 0 to 1023. The loop runs continuously until the value of ‘x’ is equal to 1023 or 5V as input. The control structure that is used here is the do-while loop and is an exit-controlled loop.

9. Is there a difference between iteration and recursion?
a) Yes
b) No
c) Yes, but only in some programming languages
d) Yes, but it depends on the version of programming language used

Explanation: Iteration is the method of executing a set of statements repeatedly with the use of looping control structures like the for-loop, while-loop, do-while loop, etc. while recursion does the same thing but without using any looping control structures. It uses function call statements.

10. Can an infinite loop be dangerous to embedded systems due to memory leak?
a) Yes
b) Yes, but only if it is used within a limited scope
c) No
d) Yes, but only for some Arduino Boards

Explanation: Infinite loops are a good way to ensure that your program is running continuously without any interruptions. However in programming languages that allow raw memory management, it’s very easy to have a memory leak, or a continuous allocation of memory which eventually leads to the programming using all the memory available and eventually crashing the entire system.

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]