Which of the following statements about global variables is true? (A) A global variable is declared in the highest-level block in which it is used.(B) A global variable can have the same name as a variable that is declared locally within a function.(C) Global variables cannot be declared in C++. (D) A global variable is accessible only to the main function.

Respuesta :

Answer:

(B) A global variable can have the same name as a variable that is declared locally within a function.

Explanation:

The variables which are declared outside of all functions in the program are called global variables and variables that have scope limited to function or a block are called local variables.

For example:-

#include<iostream>

using namespace std;

int g=50;

int main()

{

    int g=100;

cout<<"Local g= "<<g<<"  Global g = "<<::g<<endl;//Use scope resolution operator to access global variables.

return 0;

}

Output:-

Local g= 100  Global g = 50.

In this code local g has value 100 and global g has value 50 and both the variables have same name.