Arduino Questions and Answers – Variable Scope and Qualifiers

This set of Arduino Multiple Choice Questions & Answers (MCQs) focuses on “Variable Scope and Qualifiers”.

1. What functions are a static variable visible to?
a) Only Static Functions
b) All Types of Functions
c) Only Functions with a return type
d) Only Non Static Functions
View Answer

Answer: a
Explanation: The static keyword makes the variable only visible to one function. The data held by static variables is not erased when the function call is over. This is what differentiates any static variable with a non-static variable.

2. If a program has 4 functions then will all of them be able to access the data held by a global variable?
a) Yes
b) No
View Answer

Answer: a
Explanation: Any global variable when defined in a program automatically is open for all functions present in the program to access and perform read or write operations. On the contrary any variable defined within the scope of a function, will only be able to be accessed within the function itself.

3. From which memory does the compiler revert to if a variable is declared volatile?
a) RAM
b) Storage Register
c) ROM
d) EEPROM
View Answer

Answer: a
Explanation: ‘volatile’ is a directive to the compiler. It makes the compiler load the variable from the Random-Access Memory instead of the storage register. It is also called a variable qualifier. It can be used if exactly precise values are to be stored.
advertisement
advertisement

4. What would be the output of the program?

int pin=11;
void setup() {
    pinMode(pin,OUTPUT);
    int a=0;
    Serial.begin(9600);
}
void loop() {
    digitalWrite(pin,HIGH);
    Serial.println(a);
}

a) 0
b) null
c) error
d) 1
View Answer

Answer: b
Explanation: The code above is wrong since the variable ‘a’ which is declared within the scope of the setup() function is invoked for printing in the subsequent loop() function. This is a violation of the scope policies hence the program will give an error.
Note: Join free Sanfoundry classes at Telegram or Youtube

5. Which line in the code snippet is wrong?

advertisement
void setup() {
    int a = 0;
    const int b = 1;
    b=a;
}

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

Answer: c
Explanation: The Line 4 is wrong because it uses the variable ‘b’ and tries to put the value of the variable ‘a’ into it. However, that is not possible since variable ‘b’ is declared a ‘const’ variable. This means once the variable is declared, it’s value cannot be changed throughout the length of the entire program.
advertisement

6. What would be the wrong line in this program?

void setup() {
    int i;
    for(i=0;i<10;i++) {
        int a=10;
        a=9;
        break;
    }
    Serial.begin(9600);
    Serial.println(a);
}

a) 1
b) 3
c) 7
d) 9
View Answer

Answer: c
Explanation: In the code above, the line 9 is wrong since it refers to a variable ‘a’ that was declared and initiated inside the scope of the for loop and so inaccessible to the rest of the variables inside the function setup() but outside the loop.

7. Which of the sizes of data given below require us to shut off all interrupts while inputting them into a volatile variable?
a) 1 bit
b) 2 bit
c) 2 bytes
d) 0.5 bytes
View Answer

Answer: c
Explanation: The volatile keyword has a lot of uses and advantages in programming. However, when it comes to inputting large quantities of data, anything more than a bit in size will definitely result in the formation of garbage values. So, in order to input that kind of data we need to shut off all interrupts before inputting the value.

8. What is the output of the code below?

#include<stdio.h> 
int function() 
{ 
    static int c = 0; 
    c++; 
    return c; 
} 
int main() 
{ 
    printf("%d  ", function()); 
    printf("%d ",  function()); 
    return 0; 
}

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

Answer: a
Explanation: The variable used in function() is declared static which means it will retain the last value after each function call statement which is then demonstrated in the code, when it is called through the print statement. Had the variable not been declared static, then it would print 1 1, since after every function call it’s memory would be wiped.

9. In which line is there an error in the code snippet given below?

int x=10;
const int b;
x=b;
b=x;

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

Answer: d
Explanation: In the last line, the variable ‘b’ cannot accept any other value since it is declared ‘const’ at the beginning of the code snippet. However, the previous line is correct since the variable ‘a’ is not declared ‘const’ in the code snippet, so it can accept the value from ‘b’.

10. How many times can a variable declared ‘const’ be fed a value in any program?
a) 1
b) 26
c) 30
d) 0
View Answer

Answer: a
Explanation: Any variable declared ‘const’ at any point in a program will never be able to change it’s value throughout the course of the program. So, the programmer has to be careful to put in the appropriate correct value into the variable before declaring it as ‘const’.

Sanfoundry Global Education & Learning Series – Arduino.

To practice all areas of Arduino, here is complete set of 1000+ Multiple Choice Questions and Answers.

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.