A programmer declares the following variables - OCR - GCSE Computer Science - Question 4 - 2021 - Paper 1
Question 4
A programmer declares the following variables.
first = "Computer Science"
second = "is great"
(a) State one difference between a variable and a constant.
(b) Stat... show full transcript
Worked Solution & Example Answer:A programmer declares the following variables - OCR - GCSE Computer Science - Question 4 - 2021 - Paper 1
Step 1
State one difference between a variable and a constant.
96%
114 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
A variable can change its value during program execution, while a constant remains fixed and cannot be altered once defined.
Step 2
State the output from the following lines of program code.
99%
104 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
The output of the code provided will be as follows:
(i) print(first.length) will output 15, as 'Computer Science' contains 15 characters.
(ii) print(second.length DIV 3) will output 2, since 'is great' has 8 characters and dividing 8 by 3 gives 2 when considering integer division.
(iii) print(3 ^ 2) will output 9, as this is a calculation of 3 raised to the power of 2.
Step 3
Use string manipulation with the variables first and/or second to produce the following output.
96%
101 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
(i) great can be obtained with:
print(second)
(ii) Computer can be obtained with:
print(first.substring(0, 8))
(iii) Science is great can be obtained with:
print(first.substring(9) + " " + second)
This uses substring to extract 'Science' from first and concatenate it with second.