(a) State what is meant by encapsulation in object-oriented programming. [1]
(b) Explain two differences between a local variable and a global variable. [2]
- (a) Combining/bundling the data (attributes) and the methods that act on that data into a single object/class; accept: hiding an object's internal state so it can only be accessed through its methods(1)
- (b) A local variable is only accessible/has scope within the subroutine/block in which it is declared, whereas a global variable is accessible throughout the whole program(1)
- (b) A local variable only exists (is allocated memory) while its subroutine is executing, whereas a global variable exists for the whole time the program runs; accept: a local variable with the same name as a global variable takes precedence inside that subroutine(1)
- Answer: (a) Bundling data and the methods that operate on it into one class/object (with internal state hidden). (b) Scope: local = only within its subroutine, global = whole program; Lifetime: local exists only while subroutine runs, global for the whole run.
- Examiner tip: For 'explain two differences' each mark needs both sides of the comparison – saying only what a local variable does is not a difference.