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.

Host this setFree Play

The 20 questions

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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

All AQA Computer Science quizzes