Which term describes the part of a program in which a variable can be used?
Scope
What is meant by a local variable?
A variable that is created inside a sub program and can only be used while that sub program runs
Which term describes data created in the main program that can be used in every part of the program, including all sub programs?
Global variable
What does the keyword global do when it is written at the start of a sub program?
It lets the sub program change a variable created in the main program
What is meant by a constant?
A value that does not change while the program runs and is named in capital letters
Which term describes a fixed value that is created inside a sub program and can only be used there?
Local constant
A sub program stores its result in a local variable. The main program then tries to print that variable. What happens?
The program stops with an error, because the variable does not exist there
A value is passed into a sub program through a parameter. What is the scope of that parameter?
It is local to the sub program it belongs to
Two different sub programs each create a local variable called total. Which statement is true?
They are separate variables that cannot affect each other
What does this program print?
It prints 10
A sub program only reads a global variable and never assigns to it. What does the sub program need?
Nothing, it can read the global variable directly
Where is a global variable created in a program?
In the main program, outside any sub program
Which is a benefit of using local variables rather than global ones?
The sub program can be tested and reused on its own
A program stores a running total in a global variable that many sub programs can change. Why can this make the program hard to debug?
Any sub program can change it, so a wrong value is hard to trace
A program creates a global variable called points. A procedure contains the line points = points + 5 and does not use the keyword global. What happens when the procedure is called?
The program stops with an error, because points is treated as local
A fixed tax rate of 0.2 is needed by four sub programs. What is the benefit of creating TAX_RATE = 0.2 once in the main program?
The value is set in one place, so it only has to be changed once
A game keeps the value of lives at 3 in the main program. A procedure starts with the line global lives and contains lives = lives - 1. After the procedure has been called twice, what does the main program print for lives?
1
In a game, the score is read and updated by several different sub programs. Why might the programmer use a global variable for the score?
The same value is needed by many parts of the program
A sub program creates some local variables while it runs. What happens to the memory they use when the sub program ends?
It is freed and can be used elsewhere
A program has a global variable called lives. Two different procedures both need to change it. Which statement is true?
Each procedure needs its own global lives line
We use cookies to enhance your experience. You can manage your preferences or learn more in our Cookie Policy.