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 has a fixed value that cannot be altered after it is declared.
Step 2
State the output from the following lines of program code.
(i) print(first.length)
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 will be 16, which is the number of characters in the string 'Computer Science'.
Step 3
State the output from the following lines of program code.
(ii) print(second.length DIV 3)
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
The output will be 2, as the length of the string 'is great' is 8. Performing integer division, 8 DIV 3 equals 2.
Step 4
State the output from the following lines of program code.
(iii) print(3 ^ 2)
98%
120 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 will be 9, as 3 raised to the power of 2 (3 squared) equals 9.
Step 5
Use string manipulation with the variables first and/or second to produce the following output.
(i) great
97%
117 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
To produce 'great', you can use: second which contains the string 'is great'. Output: second.substring(3).
Step 6
Use string manipulation with the variables first and/or second to produce the following output.
(ii) Computer
97%
121 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
To output 'Computer', utilize: first.substring(0, 8). This extracts the first 8 characters from the string in first.
Step 7
Use string manipulation with the variables first and/or second to produce the following output.
(iii) Science is great
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
To produce 'Science is great', concatenate 'Computer Science'. You can do this by combining parts of first and second: first.substring(4) + " " + second. This outputs 'Science is great'.