Arduino Questions and Answers – Finding Out Program Outputs

This set of Arduino Multiple Choice Questions & Answers (MCQs) focuses on “Finding Out Program Outputs”.

1. What is the output of the program given below if a voltage of 5V is supplied to the pin corresponding to the A0 pin on an Arduino UNO?

void setup() {
    Serial.begin(9600);
    pinMode(A0, INPUT);
}
void loop() { 
    int s = analogRead(A0);
    Serial.println(s);
}

a) null
b) Error
c) 0
d) 1024
View Answer

Answer: d
Explanation: The Arduino UNO’s analog pins map the value of the sensed voltage to an internal numbering scale which makes it easier for the programmer to work since it is more difficult to analog voltage levels in programming practice. Thus, for a value of 5V, which reaches the maximum limit, we get a value of 1024.
advertisement
advertisement

2. What is the value of “val” in the following program if the pin 12 is given 5V 3 times?

int val=0;
void setup() {
    Serial.begin(9600);
    PinMode(13,INPUT);
}
void loop() {
    int s = digitalRead(13);
    if(s==1){
        val=val+1;
    }
}

a) null
b) 113
c) 3
d) 2
View Answer

Answer: c
Explanation: In the program the variable “val” is declared as a global variable, I.e. its value can be accessed by all functions in the current program. This variable is then setup to 1 higher than itself every time the digitalRead() function detects a HIGH or a 5V signal at its end. Hence raising the value every time a 5V signal is applied.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

3. What will the output for the code given below be, if executed on an Arduino UNO?

advertisement
void setup() {
    Serial.begin(9600);
    Serial.print(sizeof(int));
}

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

Answer: c
Explanation: The above code uses the sizeof() function to find the capacity of the integer for that particular board. It returns the number of bytes allocated for an int. Normally on 64-bit systems like laptops and desktop computers, this would give a value of 4, however on an Arduino UNO, it will give a value of 2 since the ATMega Board powering the Arduino UNO has a 16-bit architecture.
advertisement

4. What is the output of the following program?

void setup() { 
    String my_str = "This is the sentence"; 
    Serial.begin(9600); 
    my_str.replace("sentence", "Arduino sketch"); 
    Serial.println(my_str);
}
void loop() { }

a) “This is the sentence”
b) “This is the ARDUINO SKETCH”
c) “This is the Arduino Sketch”
d) null
View Answer

Answer: c
Explanation: The code given here, takes a string and then proceeds to replace the value of the string with custom word(s). It is an example of String manipulation in Arduino. It uses the replace() function for achieving this.

5. What will be the nature of the output of the program?

long rn; 
void setup() { 
    Serial.begin(9600); 
    randomSeed(analogRead(0)); 
} 
void loop() { 
    randNumber = random(300); 
    Serial.println(rn);
    delay(1000);
}

a) Series of purely mathematical indeterministic random numbers
b) Series of purely mathematical deterministic random numbers
c) null
d) Series of indeterministic random numbers based on noise
View Answer

Answer: d
Explanation: The function here attempts to generate random numbers based on the analog noise generated at one of the pins present in the board. This will create a seemingly indeterministic set of numbers ranging from 0 to 300, which is great for a random number generator function.

6. What is the output of the following line of code?

void main() {  
    printf("%lu\n", sizeof(char));
}

a) 21
b) %fdf
c) 1
d) null
View Answer

Answer: c
Explanation: When sizeof() is used with the data types such as int, float, char, etc. It returns the amount of memory that is allocated to the respective data types.

7. What will be the output of the following code given below?

void main() {
    int a = 0; 
    double d = 10.21;
    printf("%lu", sizeof(a + d));
}
void loop() {}

a) 23
b) null
c) 8
d) 10.21
View Answer

Answer: c
Explanation: The sizes of int and double are 4 and 8 respectively, a is an integer variable whereas d is a double variable. The final result will be a double in this case in order to keep the precision. Hence the output of the code is 8 bytes.

8. What does the following code do?

void main() {
    int* ptr = (int*)malloc(100 * sizeof(int));
}

a) Static Memory Allocation
b) Static Memory Clearance
c) Dynamic Memory Allocation
d) Dynamic Memory Clearance
View Answer

Answer: c
Explanation: The sizeof() function here is being used to allocate a block of memory. This code is dynamic because the size of int is different on different machine architectures. So we have allocated a block of memory that is enough to hold 100 integers irrespective of the size of the int data type in the machine in which this code is going to be executed.

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

void main() {
    Serial.begin(); 
    int x = 2; 
    printf("%d\n", sizeof(x++)); 
    printf("x = %d", x);
}

a) 2
b) 4
c) Runtime Error
d) null
View Answer

Answer: a
Explanation: The output of the code will be 2 instead of 3 because inspite of the increment operator put inside the sizeof() function, the function never really processes the variable as a whole and puts into memory, but only processes the value that is held.

10. What will be the output of the following code given below?

void main() { 
    int a = 10; 
    double d = 10.21;
    printf("%lu", sizeof(a + d));
}

a) 8
b) null
c) 9
d) 20.21
View Answer

Answer: a
Explanation: The sizes of int and double are 4 and 8 respectively, a is an integer variable whereas d is a double variable. The final result will be a double in this case in order to keep the precision. Hence the output of the code is 8 bytes.

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.