Arduino Questions and Answers – Control Structures

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

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

  1. void setup() {
  2.     Serial.begin(9600);
  3. }
  4. void loop() {
  5.     int a=10;
  6.     int b=12;
  7.     int c=13;
  8.     if(a>b && a>c) {
  9.         Serial.println(a);
  10.     }
  11.     else if(b>a && b>c) {
  12.         Serial.println(b);
  13.     }
  14.     else if(c>a && c>b) {
  15.         Serial.println(c);
  16.     }
  17. }

a) 10
b) 12
c) 13
d) null
View Answer

Answer: c
Explanation: The above code uses the if-else-if structure to find out the maximum of three integers. We must use 3 conditional blocks here to compare each value with the other one such that we can find the integer which is greater than the both the other integers.
advertisement
advertisement

2. Which of the following control structures is an exit-controlled loop?
a) For loop
b) While loop
c) Const and Goto
d) Do-While loop
View Answer

Answer: d
Explanation: An entry-controlled loop is a looping structure that performs the initialization, condition-checking, and increment/decrement operation at the beginning of the loop. So therefore, before a loop even starts to execute, a check is made if the condition is satisfied or not. In an exit controlled only the condition checking is done after the execution.

3. What is the output of the following code?

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1. void setup() {
  2.     Serial.begin(9600);
  3. }
  4. void loop() {
  5.     int a=1011;
  6.     int rev=0,t=a;
  7.     while(t!=0) {
  8.         rev=rev*10;
  9.         rev=rev+t%10;
  10.     }
  11.     If(n==rev) {
  12.         Serial.println(“Palindrome”);
  13.     }
  14.     else {
  15.         Serial.println(“Not a palindrome”);
  16.     }
  17. }

a) Null
b) Palindrome
c) Not a palindrome
d) Compilation Error
View Answer

Answer: c
Explanation: The above code checks whether a given number is a palindrome number or not. A palindrome number is one which when reversed gives the same number as the original number. This concept can also be used with strings and not just numbers.
advertisement

4. What is the output of the following code?

  1. void setup() {
  2.     Serial.begin(9600);
  3.     int i,n,t1=0,t2=1,nt;
  4.     n=4;
  5.     for(i=1;i<=n;++i) {
  6.         Serial.print(t1);
  7.         Serial.print(“ ”);
  8.         nt=t1+t2;
  9.         t1=t2;
  10.         t2=nt;
  11.     }
  12. }
  13. void loop() {
  14.     //Do Nothing
  15. }

a) 0 1 1 2
b) 1 1 2 3
c) 1 2 3 5
d) 2 3 5 8
View Answer

Answer: a
Explanation: The above code demonstrates the Fibonacci series in which 2 numbers in the series are added up together to make the third number and so on and so forth. The above code uses 0 and 1 as the initial two numbers so the series starts with 0 and 1 then goes on to print 4 more numbers since the for loop used here is limited to 4 only.
advertisement

5. What is the name of the control structure used to stop any loop prematurely and only exit out of the loop, not affecting the running of the rest of the program?
a) The continue statement
b) The break statement
c) The exit statement
d) The purge statement
View Answer

Answer: b
Explanation: From all the above options only the break statement is generally used for exiting loops. This can be also applied for switch case control structures. The difference between the break statement and the exit statement is that the break statement can only abort looping control structures, but the exit statement completely aborts the program execution.

6. Is the code snippet given below wrong?

  1. if(a=b) {
  2.     Serial.print(“Yes”);
  3. }

a) No
b) Yes
View Answer

Answer: a
Explanation: The above code is wrong since the if statement deals with conditional expressions only and not allocation statements. It is a control structure that allows the user to make basic decisions to control the flow of control of the program.

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

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

Answer: a
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. What is the output of the code given below if the input is 1101 to pin 10?

  1. void setup() {
  2.     Serial.begin(9600);
  3. }
  4. void loop() {
  5.     if(digitalRead(10)==1) {
  6.         Serial.print(“HIGH ”);
  7.     }
  8. }

a) HIGH HIGH LOW HIGH
b) HIGH HIGH HIGH HIGH
c) HIGH HIGH HIGH
d) LOW LOW HIGH HGH
View Answer

Answer: c
Explanation: The code given above reads a digital signal from pin 10 and then prints a “HIGH” statement to the Serial Monitor. However, this only happens when the signal given as input is a digital 1, or 5V. When a digital 0 or any other voltage level is detected that is below 5V, the code does not print anything.

10. Can the break statement be used to abort a program?
a) Yes
b) Yes, but only if it is used within the global scope
c) No
d) Yes, but only for some Arduino Boards
View Answer

Answer: c
Explanation: The break statement is almost never used for aborting the execution of a program. It was designed for exiting looping structures and shifting the control to the next scope available. The control statement that helps with that is the exit function.

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.