0% found this document useful (0 votes)
763 views9 pages

Unit 7 Assessment Answers

Uploaded by

rappidofc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
763 views9 pages

Unit 7 Assessment Answers

Uploaded by

rappidofc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Assessment test

Unit 7 Programming

Answers
1. Calculate each of the following expressions.

(a) 1 + 5/2 3.5


(b) (25 – 4) / 3 7
(c) 18 MOD 5 3
(d) ((27 + 5) /2) DIV 3 5
2. (a) A program starts with the lines of code:
a = 7
b = 6

State in each of the following cases the value of variable x at the end of the program.

(i) if (a <= 7 AND b > 4) then


x = 'a'
else
x = 'b' [1]
'a'

(ii) if (a > 7 OR b == 1) then


x = 'c'
else
x = 'd' [1]
'd'

(iii) if (NOT (a > b) OR (b <= 7)) then


x = 'e'
else
x = 'f' [1]
'e'
(b) Write an IF statement involving a Boolean operator (AND, OR or NOT) which performs
the same function as the following nested IF statement:
if day == "sunny"
if temperature > 20
picnic = "Yes"
else
picnic = "No"
endif
endif [2]

if day == "sunny" AND temperature > 20 then


picnic = "Yes"
else
picnic = "No"
endif

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

(a) What datatype is the variable colour? String


(b) State what is meant by selection and iteration, using examples from
Manjeri’s program.
Selection: Selection is choosing a route through the program depending on a condition.
(1) The IF statement in the program is an example of selection (1).

Iteration: Iteration is repeating a section of code. (1) The FOR loop in the program is an
example of iteration (1).

(c) What does the statement colourNumber = random(1, 4) do?


Finds a random number between 1 and 4 (1) and assigns it to colourNumber (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.

It makes the program easier to understand/debug/maintain.

(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")

(b) What is the function of the first pseudocode statement?


It opens the test file (1) and assigns the reference to the variable testfile (1).

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)

1 mark for function name and data from variables


1 mark for correct concatenation and commas
(d) Tina has started to write an algorithm which allows a student to take the test. She has
stored 10 questions in the file named “geography_test.txt”.
testfile = open("geography_test.txt")
testScore = 0
for n = 1 to 10
nextQuestion = testfile.readLine()
split nextQuestion into separate fields question, answer1,
answer2, answer3, correctAnswer
print(question, answer1, answer2, answer3)
userAnswer = input("Enter 1, 2 or 3: ")
if userAnswer == correctAnswer then
print(“Correct!”)
testScore = testScore + 1
else
print(“Wrong – bad luck”)
endif
next n
testfile.close()
print(“Your score is” + str(testScore))

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

if day == "Saturday" or day == "Sunday" then


RailFare = 0.9 * RailFare
else
if time >= 10.00 and time < 16.00 then
RailFare = 0.9 * RailFare
endif
return RailFare
endfunction

What does the subroutine return if it is called with each of the following statements?:

(a) RailFare (4, “Monday”, 8.00) 3.00 [1]

(b) RailFare (15, “Saturday”, 14.00) 4.50 [1]

(c) RailFare (8, “Tuesday”, 18.00) 4.00 [1]

7. The following function returns a value when called.


function checkval(value, low, high)
if value < low then
return "Low"
elseif value > high
return "High"
else
return "OK"
endif
endfunction

(a) (i) Name one parameter used by the subroutine.


value / low / high (1)
(ii) State the data type of the return value.
string

6
Assessment test
Unit 7 Programming

(iii) In the main program, the following statements are written.


print("Please enter measurement: ")
measurement = input()

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]

result = checkval(measurement, 18, 25)


print(result)
or
print(checkval(measurement, 18, 25))

(b) Give two reasons why programmers use subroutines.


Any two of:
A modular program which puts separate subtasks into subroutines is easier to
understand and maintain (1).
Subroutines can be tested individually so the program is easier to debug (1).
A large complex program can be developed more quickly because different
programmers can write different subroutines (1).
Subroutines can be re-used in several programs (1).
A subroutine may be used several times in the same program, but the code only has to
be written once (1) which will reduce the number of errors made (1).

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

SELECT/FROM/WHERE keywords (1)


correct fields (1)
correct Boolean operators (1)
correct brackets (1)
(c) What will the result of the following SQL query be?
SELECT PropertyID, MonthlyRent
FROM Rental
WHERE Type = 'Apartment' AND Beds = 3
ORDER BY MonthlyRent DESC
6 £1250
3 £1150
Correct two fields (1)
two records 6 and 3 (1)
sorted in descending order by monthly rent (1)

(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]

You might also like