The scope of a variable is the part of the program where the variable can be accessed or used.
Click to reveal answer
beginner
What is the difference between local and global variables?
Local variables are declared inside a function or block and can only be used there. Global variables are declared outside all functions and can be used anywhere in the program.
Click to reveal answer
intermediate
What happens if a local variable has the same name as a global variable?
The local variable 'shadows' the global variable inside its scope, so the local variable is used instead.
Click to reveal answer
beginner
What is block scope in C++?
Block scope means a variable declared inside curly braces { } can only be used inside those braces.
Click to reveal answer
beginner
Can a variable declared inside a function be accessed outside it?
No, variables declared inside a function are local to that function and cannot be accessed outside.
Click to reveal answer
Where can a global variable be accessed in a C++ program?
ANowhere, it is private
BOnly inside the function where it is declared
CAnywhere in the program
DOnly inside the block where it is declared
✗ Incorrect
Global variables are declared outside all functions and can be accessed anywhere in the program.
What is the scope of a variable declared inside a for loop?
AOnly inside the function
BOnly inside the for loop block
CEntire program
DGlobal scope
✗ Incorrect
Variables declared inside a for loop have block scope and exist only inside the loop.
If a local variable and a global variable have the same name, which one is used inside the function?
ALocal variable
BGlobal variable
CBoth at the same time
DNone
✗ Incorrect
The local variable shadows the global variable inside the function scope.
Can a variable declared inside a function be used in another function?
AOnly if declared static
BYes, always
COnly if passed as a parameter
DNo, local variables are limited to their function
✗ Incorrect
Local variables exist only inside the function where they are declared.
What keyword is used to declare a variable that keeps its value between function calls?
Astatic
Bglobal
Cconst
Dextern
✗ Incorrect
The 'static' keyword makes a local variable keep its value between calls.
Explain the difference between local and global variable scope in C++.
Think about where you declare the variable and where you can use it.
You got /4 concepts.
Describe what happens when a local variable has the same name as a global variable.
Consider which variable the program uses inside the function.
You got /3 concepts.
Practice
(1/5)
1. What is the scope of a variable declared inside a function in C++?
easy
A. It can only be used inside that function.
B. It can be used anywhere in the program.
C. It can be used only in the main function.
D. It can be used in all functions after declaration.
Solution
Step 1: Understand local variable scope
A variable declared inside a function is local to that function only.
Step 2: Check usage outside the function
Such variables cannot be accessed outside their function block.
Final Answer:
It can only be used inside that function. -> Option A
Quick Check:
Local variable scope = inside function only [OK]
Hint: Local variables live only inside their function block [OK]
Common Mistakes:
Thinking local variables are accessible globally
Confusing local and global variable scopes
2. Which of the following is the correct way to declare a global variable in C++?
easy
A. int x; inside main() function
B. int x; outside all functions
C. int x; inside a block { }
D. int x; inside a for loop
Solution
Step 1: Identify global variable declaration
Global variables must be declared outside all functions and blocks.
Step 2: Check each option
Only int x; outside all functions declares the variable outside all functions, making it global.
Final Answer:
int x; outside all functions -> Option B
Quick Check:
Global variable declared outside functions [OK]
Hint: Global variables declared outside all functions [OK]
Common Mistakes:
Declaring global variables inside functions
Declaring variables inside loops thinking they are global
3. What will be the output of the following C++ code?
int x = 5;
void func() {
int x = 10;
std::cout << x << std::endl;
}
int main() {
func();
std::cout << x << std::endl;
return 0;
}
medium
A. 10\n5
B. 10\n10
C. 5\n5
D. 5\n10
Solution
Step 1: Analyze variable shadowing in func()
Inside func(), local x = 10 shadows global x = 5, so prints 10.
Step 2: Analyze main() output
After func(), main() prints global x = 5.
Final Answer:
10\n5 -> Option A
Quick Check:
Local shadows global, prints 10 then 5 [OK]
Hint: Local variable shadows global inside function [OK]
Common Mistakes:
Assuming global x changes inside func()
Confusing which x is printed in main()
4. Identify the error in the following C++ code:
int main() {
std::cout << x << std::endl;
int x = 5;
return 0;
}
medium
A. Error: x declared twice
B. No error, prints 5
C. Error: Missing semicolon
D. Error: x used before declaration
Solution
Step 1: Check variable usage before declaration
The code tries to print x before it is declared.
Step 2: Understand C++ variable initialization rules
Variables must be declared before use; using x before declaration causes error.
Final Answer:
Error: x used before declaration -> Option D
Quick Check:
Use after declaration required [OK]
Hint: Declare variables before using them [OK]
Common Mistakes:
Assuming declaration order does not matter
Ignoring compiler errors about undeclared variables
5. Consider the following code snippet:
int x = 1;
void update() {
x = 5;
int x = 10;
std::cout << x << std::endl;
}
int main() {
update();
std::cout << x << std::endl;
return 0;
}
What will be the output and why?
hard
A. 10\n5 because local x shadows global, but global x updated before local declaration
B. 10\n1 because global x not updated due to local shadowing
C. Compilation error due to variable redeclaration
D. 5\n10 because global x updated first, then local x printed
Solution
Step 1: Analyze variable declaration order
The line x = 5; appears before the local variable x is declared in the same function.
Step 2: Understand C++ rules on variable usage
Using a variable before its local declaration in the same scope is illegal and causes a compilation error.
Step 3: Reason about the error
Because local x is declared after x = 5;, the assignment tries to assign to local x before its declaration, which is invalid.
Final Answer:
Compilation error due to variable redeclaration -> Option C
Quick Check:
Using variable before declaration in same scope causes error [OK]
Hint: Cannot use variable before local declaration in same scope [OK]
Common Mistakes:
Thinking there's a compilation error on x=5
Believing local shadowing prevents global update before declaration
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.