Unit 7 Assessment Answers
Unit 7 Assessment Answers
Unit 7 Programming
Answers
1. Calculate each of the following expressions.
State in each of the following cases the value of variable x at the end of the program.
1
Assessment test
Unit 7 Programming
3. The following pseudocode uses a FOR loop. It is intended to find and print the largest
number in an array numbers of ten positive integers.
max = 0
for n = 0 to 9
if max > numbers[n] then
max = numbers[n]
endif
next n
print(max)
(a) The pseudocode contains an error and does not work correctly. Correct the error.
if max < numbers[n] then… or, if numbers[n] > max then…
(b) Write the corrected algorithm using a WHILE loop instead of a FOR loop [3]
max = 0
n = 0
while n < 10
if numbers[n] > max then
max = numbers[n]
endif
n = n + 1
endwhile
print(max)
4. Manjeri writes a program to draw circles in random colours, sizes and positions on the
screen. She uses a random number generator to generate a colour number which she
allocates to red, yellow, green or blue.
while True
for i = 1 to 20
Xcoord = random(1, 400)
Ycoord = random(1, 300)
size = random(20, 70)
colourNumber = random(1, 4)
if colourNumber == 1 then
colour = "red"
elseif colourNumber == 2 then
colour = "yellow"
elseif colourNumber == 3 then
colour = "green"
else
colour = "blue"
endif
brushColour = colour
FillCircle(XCoord, YCoord, size)
next i
delay(2000) //delay two seconds
clearScreen()
endwhile
2
Assessment test
Unit 7 Programming
Iteration: Iteration is repeating a section of code. (1) The FOR loop in the program is an
example of iteration (1).
(d) An array colourArray could be used instead of the IF statement to assign colours to
the variable colour. The index of the array starts at 0.
(i) How many elements would the array contain? 4
(ii) What would the array contain? Four strings, “Red”, “Yellow” “Green”, “Blue”
(iii) Write a statement to assign the string "red" to element 0 in the array.
colourArray[0] = "red"
(e) Give two reasons why it is important to use meaningful identifier names
in a program.
You are less likely to make errors in the code.
(f) Add statements to the program to make the routine repeat indefinitely.
See lines of code in red above (or a similar indefinite while or repeat loop).
3
Assessment test
Unit 7 Programming
5. Tina wants to create a multiple-choice test. Each question will have three possible answers.
She writes the first question. The correct answer is “2”. Here is the question:
What is the world’s largest ocean?
1. Atlantic
2. Pacific
3. Indian
Answer 1, 2 or 3
She writes the following pseudocode:
print("What is the world's largest ocean? ")
print("1. Atlantic")
print("2. Pacific")
print("3. Indian")
correctAnswer = "2"
answer = input("Answer 1, 2 or 3: ")
(a) Write a selection statement to output “Correct!” if the user enters the correct
answer, or “No, it’s the Pacific” if the user enters the wrong answer.
if answer == correctAnswer then
print("Correct!")
else
print("No; it's the Pacific")
endif
Tina wants to store the test questions and correct answers in a file. She decides that
for each question in the test she needs a record in the text file. The record will contain
separate fields for the question, each possible answer and the correct answer. Fields
are separated by commas.
She writes the following pseudocode algorithm to ask the test setter to enter the data
for a question. The test setter is prompted to enter the three possible answers for each
question and the correct answer, to be stored on the file.
testfile = open("geography_test.txt")
question = input("Enter the question, and press Enter: ")
answer1 = input("Enter the first answer")
answer2 = input("Enter the second answer")
answer3 = input("Enter the third answer")
correctAnswer = input("Enter the number of the correct
answer")
4
Assessment test
Unit 7 Programming
(c) Write a statement to write the data for a question to the text file.
testfile.writeLine(question + "," + answer1 + "," + answer2 +"," + answer3 + "," +
correctAnswer)
Add statements to the above pseudocode which will test whether the answer is correct
and give an appropriate response, adding 1 to the test score if the answer is correct.
At the end of the test, display the total score and close the file.
Marks for:
comparison of userAnswer to correctAnswer (1)
correct use of if/then/else (1)
correct outputs of correct/wrong (1)
incrementing testScore (1)
closing the file (1)
outputting the score (1)
5
Assessment test
Unit 7 Programming
6. The following function calculates a rail fare, based on the distance travelled. A discount of
10% is given for off-peak travel.
function RailFare(distance, day, time)
if distance < 5 then
RailFare = 3.00
else
if distance < 12 then
RailFare = 4.00
else
RailFare = 5.00
endif
What does the subroutine return if it is called with each of the following statements?:
6
Assessment test
Unit 7 Programming
Write one or more statements which will call the subroutine in order to check
whether the measurement is between 18 and 25, and output the result. [2]
8. An estate agent keeps details of all the properties it has for rent.
An example of data from the table Rental is shown below.
PropertyID Type MonthlyRent Beds Furnished DistanceToStation
Apartmen
1 £800.00 2 Y 0.3
t
2 Semi £475.00 2 N 1.5
Apartmen
3 £1150.00 3 N 0.5
t
4 House £1500.00 4 Y 0.2
Apartmen
5 £900.00 2 Y 0.3
t
Apartmen
6 £1250.00 3 Y 0.2
t
7 Semi £550.00 3 Y 2.4
8 House £600.00 3 N 0.6
(a) State the Property IDs of properties that will be found by the following queries:
(i) SELECT *
FROM Rental
WHERE MonthlyRent <= 550.00 OR Furnished = 'Y' [2]
7
Assessment test
Unit 7 Programming
1,4,5,6,7 (1)
2 (1)
8
Assessment test
Unit 7 Programming
(ii) SELECT *
FROM Rental
WHERE Type = 'Apartment' AND DistanceToStation < 0.3
6
(b) Write an SQL query that displays the Type, MonthlyRent, Beds and Furnished
fields for all properties of type Semi or House which have at least 3 bedrooms.
SELECT Type, MonthlyRent, Beds, Furnished
FROM Rental
WHERE (Type = 'Semi ' OR Type = 'HOUSE ') AND Beds >= 3
(d) Write an SQL query that shows all details of all properties in the ascending
order of their distance from the station.
SELECT *
FROM Rental
ORDER BY DistanceToStation ASC
Marks:
SELECT * FROM Rental (1)
ORDER BY DistanceToStation ASC (1)
Accept if ASC is not present as ascending is usually the default in SQL.
[Total 60 marks]