Object Oriented Programming using C++ Questions and Answers – Extern Variable

This set of Object Oriented Programming using C++ Questions and Answers for Campus interviews focuses 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: a
Explanation: 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.
advertisement
advertisement

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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: a
Explanation: The header file only contains the declaration of variables that are extern. It doesn’t contain any static variable definitions.
Note: Join free Sanfoundry classes at Telegram or Youtube

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
View Answer

Answer: d
Explanation: 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
View Answer

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

8. Even if a variable is not declared as extern, it is extern by default.
a) True
b) False
View Answer

Answerr: b
Explanation: 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
View Answer

Answer: b
Explanation: 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.
advertisement

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;
View Answer

Answer: a
Explanation: 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);
View Answer

Answer: b
Explanation: 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: c
Explanation: 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
View Answer

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

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming using C++ for Campus Interviews, 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.