TK Study

Multiple choice › Variables and scope

Variables and scope

1. Variables and scope

Why would a developer choose to define a variable as global?

  1. It results in code that executes more quickly.
  2. It can be accessed and changed by all modules.
  3. It allows the code to run on multiple platforms.
  4. 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?

  1. 2 2
  2. 2 4
  3. 4 2
  4. 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:

  1. Class
  2. Global variable
  3. Constant
  4. Local variable
Answer

D

Local variables exist only inside their subroutine; global variables are accessible to all modules (a risk for unintended changes).