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?
✗ 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?
✗ 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?
✗ Incorrect
The local variable shadows the global variable inside the function scope.
Can a variable declared inside a function be used in another 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?
✗ 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.