0
0
C++programming~5 mins

Scope of variables in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the scope of a variable in C++?
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
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
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
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
What keyword is used to declare a variable that keeps its value between function calls?
Astatic
Bglobal
Cconst
Dextern
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.