This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Automatic Variable”.
1. What are automatic variables?
a) Global variables
b) Implicit/temporary variables
c) Local variables
d) System variables
Explanation: The local variables are also known as automatic variables. The variables in any local scope that are created and destroyed as the program executes its scope.
2. The memory for automatic variables ___________________
a) Have to be allocated and deallocated explicitly
b) Are allocated and deallocated automatically
c) Is never actually allocated
d) Are never safe
Explanation: The memory is allocated and deallocated automatically for the automatic variables. As soon as the variable comes in scope, the memory is allocated. The variables are destroyed as soon as those go out of scope.
3. Scope of an automatic variable _______________
a) Is actually the whole program
b) Is actually never fixed
c) Is always equal to the whole program execution
d) Is actually function or block in which it is defined
Explanation: The automatic variables scope is limited only within the block or the function where those are defined. This is property of all the automatic variables.
4. Which among the following is true for automatic variables in general?
a) Automatic variables are invisible to called function
b) Automatic variables are always visible to the called function
c) Automatic variables can’t interact with the called function
d) Automatic variables can’t be variable
Explanation: The automatic variables are hidden from the called function. Even if passed by reference or address, the address of the variable is used and not the actual variable of calling function. Automatic variables can be const or variable.
5. If an automatic variable is created and then a function is called then ________________
a) The automatic variable created gets destroyed
b) The automatic variable doesn’t get destroyed
c) The automatic variable may or may not get destroyed
d) The automatic variable can’t be used in this case
Explanation: The automatic variables are saved till the called function gets executed. This is done so as to ensure that the program can continue its execution after the called function is returned. The automatic variables gets destroyed only if those go out of scope.
6. Where are the automatic variables stored if another function is called in between the execution of program?
d) Temp variable
Explanation: All the automatic variables are stored in a new stack entry as soon as their scope is created. If another function is called, the present data is saved in stack and new entry in stack is made for the called function. When the function returns, the automatic variables are used again from where those were left.
7. The static variables of a function ________________
a) Are also automatic variables
b) Are not automatic variables
c) Are made automatic by default
d) Can be made automatic explicitly
Explanation: The static members can’t be automatic. This is because the automatic variables are created and destroyed with each call to a specific function. But the static members remain throughout the execution of program once created.
8. All variables declared within a block ____________________
a) Are not always automatic
b) Can be made non-automatic
c) Are static by default
d) Are automatic by default
Explanation: The variables declared inside a block, are make automatic by default. This is to ensure that the variables get destroyed when not required. The variables remain live only till those are required, the life is dependent on the scope of variable.
9. What values does uninitialized automatic variables contain?
a) Null value
b) Void value
d) Zero value
Explanation: The automatic variable which are not initialized, contain garbage value. If we just declare a variable and try to print its value, the result is some unknown value. The value is garbage as that was not expected value.
10. Constructor of automatic variables is called ____________________
a) When execution reaches the place of declaration of automatic variables
b) When the program is compiled
c) When the execution is just started
d) Just before the execution of program
Explanation: Only when the execution reaches the place where the automatic variable was declared, the constructor is called. This is to ensure that the memory is not allocated if not needed. The memory is allocated and then destroyed as soon as it goes out of scope.
11. Does java contain auto or register keywords?
a) Yes, for declaring every type of variable
b) Yes, only to declare cache registers
c) No, because java doesn’t support automatic variables
d) No, java supports local variable concept
Explanation: The auto and register keywords are not supported in java. Though the same is allowed in java without specifying any of those keywords. The variables are local variables. But java makes it mandatory to initialize all of the local variables in a program.
12. The automatic variables _________________________
a) Must be declared after its use
b) Must be declared before using
c) Must be declared, can be anytime
d) Must not be initialized
Explanation: All the automatic variables in a program must be declared before their use. The compiler won’t allow any use of variable if those are not declared before their use.
13. Which error is produced if the automatic variables are used without declaration?
a) Undefined symbol
b) Memory error
c) Type mismatch
d) Statement missing
Explanation: If the automatic variables are used without declaration or are used before the declaration then the compiler throws an error. The error that the symbol is undefined. The compiler must know everything before that can be used.
14. In perl, using which operator are the local variables created?
c) Scope resolution
Explanation: The language perl supports local variables but the concept is bit different. And if the values are not assigned to the local variables then it contains undef value.
15. How are automatic variables different from the instance variables?
a) Automatic variables are initialized automatically but instances are not
b) Automatic variables are given zero values initially and not instances
c) Instance variables have to be initialized explicitly and automatic implicitly
d) Instance variables are initialized implicitly while automatic are not
Explanation: The automatic variables have to be initialized explicitly. But in case of instances, those are initialized automatically during execution of program. The conventions are mandatory.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming (OOPs), here is complete set of 1000+ Multiple Choice Questions and Answers.