Test Series - oops

Test Number 75/78

Q: What is extern variable?
A. Variables to be used that are declared in another object file
B. Variables to be used that are declared in another source file
C. Variables to be used that are declared in another executable file
D. Variables to be used that are declared in another program
Solution: The variables that are declared in another source file can be accessed in other files using extern variables. The extern variables must be mentioned explicitly. The source file is included to use its variables.
Q: Which among the following is a correct statement for variables?
A. Variable can be declared many times
B. Variable can be declared only one time
C. Variable declaration can’t be done more than ones
D. Variable declaration is always done more than one time
Solution: The variables can be declared any number of times. There is no restriction on how many times a single variables can be declared. Declaration is just an indication that the variable will be used in the program.
Q: Which among the following is true for the variables?
A. Variable can be defined only once
B. Variable can be defined any number of times
C. Variable must be defined more than one time
D. Variable can be defined in different files
Solution: The variables can be defined only once. Once the variable is defined, then it can’t be declared again. The definition of a variable is actual allocation of memory for the variable.
Q: To use extern variable _____________________
A. The source file must not be included in the new file code
B. The source file itself must be used for a new program
C. The source file must be included in the new file
D. The source file doesn’t matter for extern variables
Solution: The source file must be included in the file which needs to use the extern variable. This is done to ensure that the variables that are already declared can be used again. Only the declarations are used from one file to another.
Q: What does a header file contain for an extern variable?
A. Only declaration of variables
B. Only definition of variables
C. Both declaration and definition of variables
D. Neither declaration nor definition
Solution: The header file only contains the declaration of variables that are extern. It doesn’t contain any static variable definitions.
Q: Which condition is true if the extern variable is used in a file?
A. All the header files declare it
B. Only few required files declare it
C. All header files declared it if required
D. Only one header file should declare it
Solution: Only one header file should declare the extern variable to be used. There must not be more than one file declaring the same extern variable. This is to ensure that there is no ambiguity in using the extern variable.
Q: Whenever a function is declared in a program _____________________
A. extern can be used only in some special cases
B. extern can’t be used
C. function is extern by default
D. it can’t be made extern
Solution: Even if we don’t specify a function to be extern, by default all the functions are exter. The compiler adds the keyword at the beginning of the function declaration. If there is an extern function to be used then it will be used otherwise the new function only will be used.
Q: Which of the following results in the allocation of memory for the extern variables?
A. Declaration
B. Definition
C. Including file
D. Memory is not allocated for extern variables
Solution: The memory for the extern variables are allocated due to their definition. When the variables are declared, it only indicates the compiler that the variable is going to be used somewhere. But definition makes the compiler to allocate the memory for the variables.
Q: Which is the correct syntax for extern variable declaration?
A. extern data_type variable_name;
B. extern variable_name;
C. data_type variable_name extern;
D. extern (data_type)variable_name;
Solution: The syntax firstly contains the keyword extern. Then the data type of the variable is given. Then the variabel name is mentioned by which it will be used in the program.
Q: Why are functions extern by default?
A. Because functions are always private
B. Because those are not visible throughout the program
C. Because those can’t be accessed in all parts of the program
D. Because those are visible throughout the program
Solution: The program have all of its functions visible throughout the program usually. Also, there is no specific value that a function must contain. Hence the functions are extern by default.

You Have Score    /10