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
View Answer
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?
void setup() {
Serial.begin(9600);
}
void loop() {
int i, j, n=3, temp;
int a[3]={2, 1, 3};
for(i=0;i<n-1;i++) {
for(j=0;j<n-1-I;j++) {
if(a[j]>a[j+1]) {
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<n;i++) {
Serial.print(a[i]);
Serial.print(“ ”);
}
}
a) 1 2 3
b) 3 2 1
c) 1 3 2
d) 2 1 3
View Answer
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?
void setup() {
Serial.begin(9600);
}
void loop() {
int a[4]={1,2,3,4};
int search_item=2;
int i;
for(i=0;i<4;i++) {
if(a[i]==search_item) {
Serial.print(“Found!”);
}
}
}
a) Search
b) Sort
c) Delete
d) Append
View Answer
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?
void setup() {
Serial.begin(9600);
}
void loop() {
int n,c,d,position,swap;
int array[4]={1,2,4,3};
n=4;
for(c=0;c<n;c++) {
position=c;
for(d=c+1;d<n;d++) {
if(array[position]>array[d]) {
position=d;
}
}
if(position!=c) {
swap=array[c];
array[c]=array[position];
array[position]=swap;
}
}
for(c=0;c<n;c++) {
Serial.print(array[c]);
Serial.print(“ ”);
}
}
a) 1 2 4 3
b) 1 3 2 4
c) 3 1 2 4
d) 1 2 3 4
View Answer
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
View Answer
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?
int pin1=10;
void setup() {
Serial.begin(9600);
pinMode(pin1,INPUT);
}
void loop() {
for(int i=0;i<10;i--) {
if(digitalRead(pin1)==1) {
Serial.println(“HELLO”);
}
}
}
a) 1
b) 2
c) infinitely
d) 5
View Answer
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
View Answer
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?
void setup() {
Serial.begin(9600);
}
void loop() {
int x=0;
do {
x=analogRead(10);
Serial.println(x);
}while(x!=1023);
}
a) 1023
b) 1024
c) null
d) 1029
View Answer
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
View Answer
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
View Answer
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.
- Apply for Electrical Engineering Internship
- Practice Electrical Engineering MCQs
- Apply for Electrical & Electronics Engineering Internship
- Check Electrical & Electronics Engineering Books
- Check Arduino Books