Multiple choice › Variables and scope
Variables and scope
1. Variables and scope
Why would a developer choose to define a variable as global?
- It results in code that executes more quickly.
- It can be accessed and changed by all modules.
- It allows the code to run on multiple platforms.
- It can have different values in different modules.
Answer
B
NESA answer key: B. Old syllabus (SDD): useful as overlap practice only.
2. Variables and scope
Consider the following code. BEGIN Main Num is local Num = 4 Sub2 print Num END Main BEGIN Sub2 Num is local Num = 4 Num = Num − 2 print Num END Sub2 What is the output produced when the above code is executed?
- 2 2
- 2 4
- 4 2
- 4 4
Answer
B
NESA answer key: B. Num in Sub2 is a different (local) variable: Sub2 prints 2, then Main prints its own 4. Old syllabus (SDD): useful as overlap practice only.
3. Variables and scope
A variable declared inside a function and used only there is a:
- Class
- Global variable
- Constant
- Local variable
Answer
D
Local variables exist only inside their subroutine; global variables are accessible to all modules (a risk for unintended changes).