Lesson 4.1.1.13
4.1.1.13 Local and global variables Quiz: AQA Computer Science, Unit 1
20 questions
In partnership with Revision Ninja
Lesson 4.1.1.13, Local and global variables: 20 multiple choice questions for the AQA Computer Science (7517), Unit 1: Fundamentals of programming, written with Revision Ninja.
Host it live on the board and students join with a game code on their own devices, or revise alone with Free Play. The answers are revealed in the game.
The 20 questions
-
What is a local variable in a subroutine?
- A pointer stored in a file
- A constant that is declared in the main program
- A variable declared inside the subroutine that is accessible only there
- A variable that can be read by every subroutine in the program
-
A local variable exists only while which condition holds?
- The program is saved to disk
- The variable is printed on screen
- The main program has finished
- The subroutine is executing
-
What is a global variable?
- A variable that exists only inside one loop, and is destroyed as soon as that loop finishes
- A variable declared outside subroutines and accessible from anywhere in the program
- A parameter passed into a subroutine by value, so the subroutine receives a private copy of it
- A constant stored in a record, which holds a fixed value that is shared between fields
-
Which is a good practice reason for using local variables rather than globals?
- They remove the need for subroutines, since the data stays inside the main program at all times
- They avoid unintended changes to data used by other parts of the program
- They guarantee the program will never fail, so no error can occur in any part of the code
- They make the program slower to run, because each subroutine must copy its values into memory
-
Two subroutines each declare a local variable named count. What is the effect on each subroutine?
- Both count values are always zero
- Each uses its own count, and the other does not see it
- They share one count, so changes in one affect the other
- The program refuses to run
-
Which statement contrasts local and global variables correctly?
- Local variables are limited to one subroutine, while global variables are accessible throughout the program
- Local and global variables have no difference in scope
- Global variables must always be constants
- Global variables exist only inside subroutines, while local variables exist everywhere
-
A subroutine sets a local variable total to 0, then adds 5 twice. After the subroutine ends, what happens to total?
- It is no longer available, so its value cannot be used outside
- It keeps the value 10 for the next call of the subroutine
- It is copied into the main program
- It becomes a global variable automatically
-
A program has a global variable score updated by several subroutines. Which problem can arise?
- The score is automatically reset after each call, so the subroutines always see a value of zero
- The value may be changed unexpectedly by any subroutine, making bugs hard to trace
- The subroutines cannot read score at all, because globals are only visible to the main program
- The program runs with no output, because the global variable stops the screen from updating
-
Why is it good practice for a subroutine to use a local variable for an intermediate result?
- It makes the result visible to every other subroutine, so the whole program can share the value
- It forces the result to be stored in a file, so that the value is always kept after the run
- It keeps the result private, and the variable is freed when the subroutine ends
- It prevents the subroutine from returning any value, so the caller must read the result in another way
-
A variable x is declared inside a function and another x is declared globally. Inside the function, which x is used?
- The local x declared in the function
- Both are added together
- Neither can be used, so the program fails
- The global x, always
-
A local variable is declared in subroutine A. Subroutine B tries to read it. What is the result?
- B cannot access it, because it is local to A
- B reads the value since all variables are shared
- B changes it automatically
- A is stopped so B can read it
-
Which statement is most accurate about the lifetime of a global variable?
- It is destroyed when the variable is first read
- It exists only during one loop iteration
- It is destroyed when the first subroutine ends
- It exists for as long as the program runs
-
A program uses local variables in every subroutine and passes data with parameters. What does this design achieve?
- Clear data flow with fewer side effects between subroutines
- A program that must share one name for every variable
- A program with no data flow at all
- More global variables with less control
-
A subroutine declares a local variable with the same name as a global variable and changes it. What happens to the global variable?
- It is always changed as well, because a local and a global variable with the same name are linked
- It is unchanged, because the local variable is a separate variable
- It is deleted from memory, since a duplicate name causes the global variable to be removed
- It is converted to a string, so that both variables share one text value during the subroutine
-
Which of these is the most appropriate use of a global variable?
- A value passed from one subroutine to another that could be a parameter, sent via a shared store
- A temporary counter used in one loop of one subroutine, which is reset every time the loop runs
- A setting that many subroutines genuinely need to read, such as a fixed application-wide rate
- A result that is only needed inside a single function, and nowhere else in the whole program
-
A program runs a subroutine twice. The subroutine uses a local variable that is not initialised on each run. What is the risk?
- The subroutine will not run the second time, because an uninitialised variable stops execution
- It may hold an unpredictable value on the second run if it is assumed to be zero
- The variable is always zero on every run, because the language resets all locals to zero for each call
- The variable becomes a constant after the first run, so its value cannot be changed on later calls
-
What does it mean that a local variable is accessible only within the subroutine?
- Code outside the subroutine cannot read or change it by name
- Code outside can change it, but it cannot read the value of the variable by its name directly
- It is visible to the operating system only, so the program itself cannot access the value
- Other subroutines can read it, but only the main program is allowed to change its value
-
A global variable called rate holds 0.2. A subroutine multiplies a price by rate. Which change makes this subroutine easier to reuse with different rates?
- Change rate to a string, so that the multiplication is carried out on the text characters instead
- Remove the multiplication, so the subroutine returns the price unchanged whatever the rate is
- Declare rate inside the loop, so that it is recalculated on every pass through the iteration
- Pass the rate in as a parameter rather than using the global
-
A programmer uses local variables in a subroutine. Which statement correctly describes the variable's memory use?
- Local variables are stored permanently in a file, so that their values survive a restart
- Memory for local variables is reserved when the subroutine is called and released when it returns
- Local variables never use any memory, because they are held in the processor registers only
- Local variables occupy memory for the whole life of the program, even after the subroutine ends
-
A subroutine has a local variable count that is initialised to 0 each call. Two calls are made. What is count's value during the second call at the start?
- It keeps its value from the first call
- 1
- 0
- 2
Related quizzes
- Data types Quiz · 4.1.1.1 · 20 questions
- Programming concepts: sequence, selection and iteration Quiz · 4.1.1.2 · 20 questions
- Arithmetic operations Quiz · 4.1.1.3 · 20 questions
- Relational and Boolean operations Quiz · 4.1.1.4 · 20 questions
- Constants and variables Quiz · 4.1.1.6 · 20 questions
- String-handling operations Quiz · 4.1.1.7 · 20 questions
- Random numbers and exception handling Quiz · 4.1.1.8 · 20 questions
- Subroutines Quiz · 4.1.1.10 · 20 questions
- Parameters and returning values Quiz · 4.1.1.11 · 20 questions
- Stack frames in subroutine calls Quiz · 4.1.1.15 · 20 questions