250+ TOP MCQs on Extern Variable and Answers

Object Oriented Programming Questions Campus interviews on “Extern Variable”.

1. 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
Answer: b
Clarification: 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.

2. 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
Answer: a
Clarification: 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.

3. 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
Answer: a
Clarification: 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.

4. 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
Answer: c
Clarification: 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.

5. 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
Answer: a
Clarification: The header file only contains the declaration of variables that are extern. It doesn’t contain any static variable definitions.

6. 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
Answer: d
Clarification: 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.

7. 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
Answer: c
Clarification: 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.

8. Even if a variable is not declared as extern, it is extern by default.
a) True
b) False
Answerr: b
Clarification: The statement is false. The variables are not extern by default. If those are made extern by default, then the memory will never be allocated for those extern variables. Hence we make the variables extern explicitly.

9. 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
Answer: b
Clarification: 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.

10. 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;
Answer: a
Clarification: 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.

11. Which is the correct syntax for extern function declaration?
a) extern function_name(argument_list);
b) extern return_type function_name(argument_list);
c) extern (return_type)function_name(argument_list);
d) return_type extern function_name(argument_list);
Answer: b
Clarification: The syntax must contain the keyword extern first, to denote that the function is extern. Though the function are extern by default but among the given choices, it should contain the keyword, for explicit declaration. Then the usual function declaration follows.

12. What will be the output of the program?

extern int var;
int main(void)
{
  var = 10;
  var++;
  cout<<var;
}

a) 10
b) 11
c) Run time error
d) Compile time error
Answer: d
Clarification: The program gives the compiler time error. There is no definition given for the extern variables. This is not allowed and hence we get a compile time error.

13. If the definition is given in the header file that we include then ________________
a) The program can run successfully
b) Also the program should define the extern variable
c) The extern variable must contain two definitions
d) Extern variable can’t be used in the program
Answer: a
Clarification: The program runs successfully. This is because only one definition of any variable is allowed. And hence the definition from the source file that is included will be used.

14. If extern variable is initialized with the declaration then _______________________
a) Also the header file with definition is required
b) The header file with definition must be included
c) There is no need to include any other header file for definition
d) The extern variable produces compile time error
Answer: c
Clarification: When the value for the extern variable is defined with its declaration, then there is no need to include any file for the definition of the variable. The Initialization acts as a definition for the extern variable in the file itself.

15. 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
Answer: a
Clarification: 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.

Object Oriented Programming for Campus Interviews,

Leave a Reply

Your email address will not be published. Required fields are marked *