Implementation (Constructs) (SQA National 5 Computing Science): Exam Questions

Exam code: X816 75

1 hour24 questions
1
1 mark

A user enters the value 2 when running the program below.

Line 1 DECLARE answer INITIALLY 0
Line 2 DECLARE numOne INITIALLY 3
Line 3 RECEIVE numTwo FROM KEYBOARD
Line 4 SET answer TO numOne ^ numTwo
Line 5 SEND answer TO DISPLAY

State the output

2
4 marks

A spelling game stores 20 words. Each word has an accompanying sound file where an actor’s voice speaks the word.

When the game is running the program repeats the following 20 times:

  • selects one of the 20 words

  • loads a sound file matching the selected word

  • plays the sound file through a speaker

  • asks the user to type in the word

  • compares the user’s entry to the stored word

  • informs the user if they have spelled the word correctly

When the game is over the program displays the total number of words that have been spelled correctly by the user.

Part of the program code is shown below

…
Line 27 REPEAT 20 TIMES
Line 28 SET choice TO <a number between 0 to 19>
Line 29 <load selected sound file>
Line 30 SEND <sound file> TO <speaker>
Line 31 RECEIVE usersWord FROM KEYBOARD
Line 32 IF usersWord = NOT(storedWords[choice]) THEN
Line 33 SEND "Sorry, the correct spelling is " &
storedWords[choice] TO DISPLAY
Line 34 ELSE
Line 35 SEND "Well Done" TO DISPLAY
Line 36 SET correctGuesses TO correctGuesses + 1
Line 37 END IF
Line 38 END REPEAT
Line 39 SEND "You guessed " & correctGuesses & " words
correctly" TO DISPLAY

(i) Identify the logical operator used in the above code.

[1]

(ii) Using a programming language of your choice, re-write Line 28 to show how the value stored in the variable choice would be generated. Your answer should use a function.

[2]

(iii) When the above code was tested several times, it was found that the user was not asked to spell all 20 of the stored words.

Explain why the program did not ask the user to spell every stored word.

[1]

3a
5 marks

A company runs a sightseeing trip around Iron Craig Island each Saturday and Sunday. Their boat can hold 100 passengers.

Every weekend the available tickets are numbered as follows.

Saturday’s ticket numbers

1 to100

Sunday’s ticket numbers

101 to 200

A program is being developed to:

  • allow the company to check the validity of each passenger’s ticket as they board the boat

  • calculate and display the total number of the passengers on each trip.

The program design is shown below.

Flowchart for a daily ticket analyser program. Steps include setting passenger count, getting ticket range, checking validity, updating count, and displaying total.

(i) State the type of loop required when implementing this design.

[1]

(ii) State the standard algorithm used in this design.

[1]

(iii) Several different programming constructs will be required when the program code is written.

Complete the table below to show this

[3]

Example from design

Matching construct

Set totalPassengers to 0.00

Conditional statement

Arithmetic operation

3b
3 marks

The program is edited to calculate the total value of the passengers’ tickets. The price of a ticket is different for each deck.

Deck 1

Deck 2

Saturday’s ticket numbers

1 to50

51 to 100

Sunday’s ticket numbers

101 to 150

151 to 200

Ticket price

£5

£10

The edited code is shown below.

…
Line 5 RECEIVE lower FROM KEYBOARD
Line 6 RECEIVE upper FROM KEYBOARD
…
Line 14 IF ticketNumber < lower OR ticketNumber > upper THEN
Line 15 SEND "Ticket Refused" TO DISPLAY
Line 16 ELSE
Line 17 SET numberOfPassengers TO numberOfPassengers + 1
Line 18 IF ticketNumber <= (lower + 49) THEN
Line 19 SET totalValue TO totalValue + 5
Line 20 END IF
Line 21 IF ticketNumber >= (lower + 50) THEN
Line 22 SET totalValue TO totalValue + 10
Line 23 END IF
Line 24 END IF

Using a programming language of your choice, re-write lines 18 to 23 in a more efficient way

4a
3 marks

A farm uses a robot to scan mushrooms and measure their diameter. If they have grown to the correct size, the mushrooms are picked and packed into boxes.

Robotic arm harvesting mushrooms with a mechanical gripper. The arm is poised above a bed of mushrooms, indicating an automated collection process.

The program that controls the robot is shown below.

Line 1 DECLARE maxSize AS REAL INITIALLY 4.0
Line 2 DECLARE fullBox AS INTEGER INITIALLY 20
Line 3 DECLARE count AS INTEGER INITIALLY 0
Line 4 DECLARE mushroomSize AS REAL INITIALLY 0.0
Line 5 WHILE <there are more mushrooms to scan> DO
Line 6 RECEIVE mushroomSize FROM <scanner>
Line 7 IF mushroomSize >= maxSize/2 AND mushroomSize <=
maxSize THEN
Line 8 <pick and pack scanned mushroom>
Line 9 SET count TO count + 1
Line 10 IF count = fullBox THEN
Line 11 SEND "Box Full" TO TOUCHSCREEN
Line 12 SEND "Replace with Empty Box" TO
TOUCHSCREEN
Line 13 <pause until box replaced>
Line 14 SET count TO 0
Line 15 END IF
Line 16 END IF
Line 17 END WHILE

Explain fully how this program informs the farmer when a box of mushrooms is full.

4b
2 marks

The robot currently picks mushrooms that are no more than 4 cm in diameter and packs 20 mushrooms into a box.

(i) State the smallest size a picked mushroom could be.

[1]

(ii) Explain why line 14 is necessary.

[1]

4c
2 marks

The scanner on a second robot calculates how white each mushroom is and outputs this as a ‘whiteness’ reading between 0 and 10.

Line 1 DECLARE maxSize AS REAL INITIALLY 4.0
Line 2 DECLARE fullBox AS INTEGER INITIALLY 20
Line 3 DECLARE count AS INTEGER INITIALLY 0
Line 4 DECLARE whiteness AS REAL INITIALLY 0.0
Line 5 WHILE <there are more mushrooms to scan> DO
Line 6 RECEIVE mushroomSize FROM <scanner>
Line 7 IF mushroomSize >= maxSize/2 AND mushroomSize <=
maxSize THEN
Line 8 <pick and pack scanned mushroom>
Line 9 SET count TO count + 1
Line 10 IF count = fullBox THEN
Line 11 SEND "Box Full" TO TOUCHSCREEN
Line 12 SEND "Replace with Empty Box" TO TOUCHSCREEN
Line 13 <pause until box replaced>
Line 14 SET count TO 0
Line 15 END IF
Line 16 END IF
Line 17 END WHILE

Line 4 of the original program has been edited.

Describe how else the original program could be edited so that mushrooms of any size, with a whiteness reading of at least 9 would be picked by the robot.

5
1 mark

Part of a program is shown below.

…
Line 34 RECEIVE xyz FROM (INTEGER) KEYBOARD
Line 35 SET abc TO xyz ^ 2
…

State the value stored in abc when ‘3’ is entered by the user at Line 34

6
1 mark

The program below issues customers with a username.

…
Line 11 RECEIVE firstName FROM (STRING) KEYBOARD
Line 12 RECEIVE yearOfBirth FROM (STRING) KEYBOARD
Line 13 SET userName TO firstName & yearOfBirth
Line 14 SEND userName TO DISPLAY

Describe how the value assigned to the userName variable is created in Line 13.

7
2 marks

A program is required for passengers to book a ticket on a bus.

Write a line of code that will randomly allocate a seat number and store this in the variable seatNum. There are 50 seats available.

8
1 mark

A new supermarket self‑service till is being designed for customers. Part of the program design is shown below.

Illustration of a supermarket checkout counter with a conveyor belt, monitor, card reader, receipt printer, and shelves in the background.
Flowchart: Add items to conveyor belt, scan and price them, check if belt is empty, if yes, display total; if no, continue scanning.

State which type of loop is used in this design.

9
4 marks

Luna Life is a company that creates animations.

Luna Life uses a program to calculate the cost of creating an animation. Part of the program is shown below.

SET basicCost TO timeInSeconds * animatorCharge

After the basic cost has been calculated, the following discounts can be applied to animations depending on their use:

  • education — deduct £20 from cost

  • charity — deduct £30 from cost.

Using a programming language of your choice, write the code to ask for the purpose of the animation, calculate any discount, and display the final cost in the variable finalCost.

10a
2 marks

A gym wants to encourage members to burn more calories than their monthly target.

It uses a program to calculate additional calories burned over a 12‑month period.

.....

April

May

June

.....

.....

6821.34

5129.89

4997.67

......

The design below shows how a member’s average additional calories burned is calculated and displayed.

  1. Store each month’s additional calories burned

  2. Calculate the average additional calories burned

  3. Display the average additional calories burned

The data structure calories is used to store additional calories burned each month. The variable avgCalories is used to store the user’s average additional calories burned.

A member’s average additional calories burned is displayed using the line of code below.

SEND avgCalories TO DISPLAY

This code displays the value below to a member.

5123.879

Using a programming language of your choice, rewrite this code to display the average additional calories to one decimal place.

10b
3 marks

If a member’s average additional calories burned is over 6000 calories, and they have a personal trainer at the gym, they will be given a 15% discount on their next session.

The code to implement this is shown below.

…
Line 27 SET memberDiscount TO 0.00
Line 28 <repeat for every gym member>
Line 29 IF avgCalories < 6000 AND trainer = TRUE
Line 30 SEND "Congratulations on a 15% discount"
TO DISPLAY
Line 31 SET memberDiscount TO sessionCost * 0.15
Line 32 END IF
Line 33 <end repeat>
…

(i) Identify the logical operator in the above code.

[1]

(ii) It was identified that a member who burned 6578.1 average additional calories and who has a personal trainer did not receive the 15% discount.

State the type of error in the program and how the error can be corrected.

Type of error ................................................................................................

Correction...................................................................................................

[2]

11
1 mark

In an archery game, players score points when they hit the target.

The points entered are whole numbers in the range 0 to 10.

The game has 10 rounds and each player shoots two arrows in each round.

The program below is written to record a player’s score.

…
Line 24 SET totalScore TO 0.00
Line 25 SEND "round1" TO DISPLAY
Line 26 RECEIVE arrow1 FROM (REAL) KEYBOARD
Line 27 RECEIVE arrow2 FROM (REAL) KEYBOARD
Line 28 SET roundTotal1 TO arrow1 + arrow2
Line 29 SET totalScore TO totalScore + roundTotal1
…
Line 70 SEND "round10" TO DISPLAY
Line 71 RECEIVE arrow19 FROM (REAL) KEYBOARD
Line 72 RECEIVE arrow20 FROM (REAL) KEYBOARD
Line 73 SET roundTotal10 TO arrow19 + arrow20
Line 74 SET totalScore TO totalScore + roundTotal10
Line 75 < display all ten round totals >
Line 76 SEND "TotalScore: " & totalScore TO DISPLAY

The code below is written to store the names of a maximum of 40 competitors in an archery competition.

…
Line 80 SET stop TO TRUE
Line 81 SET count TO 0
Line 82 WHILE NOT(stop) AND count <= 40 DO
Line 83 RECEIVE nextPerson FROM (STRING) KEYBOARD
Line 84 IF nextPerson = "NO" THEN
Line 85 SET stop TO FALSE
Line 86 ELSE
Line 87 <store name entered>
Line 88 SET count TO count + 1
Line 89 END IF
Line 90 END WHILE
Line 91 SEND "Total archers " & count TO DISPLAY
…

Describe why this code will not function as expected.

12
1 mark

A competition was run to suggest names for a new bridge. The 20 most popular bridge names have been identified and stored.

State the predefined function that should be used to select any one of the 20 stored bridge names as the winner.

13
1 mark

The program code below protects the rechargeable battery in an electric toothbrush.

…
Line 67 SET brushstop TO FALSE
Line 68 WHILE brushstop = FALSE DO
Line 69 SET battery TO <percentage of power left>
Line 70 SET temperature TO <temperature of battery>
Line 71 IF battery < 3 OR temperature > 45 THEN
Line 72 SET brushstop TO TRUE
Line 73 END IF
Line 74 END WHILE
Line 75 <switch toothbrush off>
…

State the logical operator in this code.

14
3 marks

A campsite booking system is being developed to calculate the cost of a stay at £35 per night, per person. The cost will be displayed to the user before they confirm the booking. The system will ask users for their name, number of people, arrival date and length of stay.

The code below calculates the cost of the stay.

…
Line 7 RECEIVE name FROM (STRING) KEYBOARD
Line 8 RECEIVE numberOfPeople FROM (INTEGER) KEYBOARD
Line 9 RECEIVE arrival FROM (STRING) KEYBOARD
Line 10 RECEIVE nightsStaying FROM (INTEGER) KEYBOARD
Line 11 <calculate cost of stay>
Line 12 SEND "The cost of your stay is £" & cost TO DISPLAY
…

Using a programming language of your choice, write the code for Line 11.

15a
1 mark

An event company organises children’s parties. They would like a program to help calculate the costs of parties. Part of the structure diagram design is shown below.

Flowchart for a party cost program with decision points for cake, number of guests, and venue fee, including a loop for children's dietary needs.

State the type of loop shown in the design above.

15b
2 marks

The design is tested using the following inputs:

  • 12 adults

  • 16 children

  • cake required - Yes.

(i) State the venue cost.

[1]

(ii) State the total cost of the party.

[1]

16a
2 marks

Edge Races are developing a program to process information on races with multiple stages.

The first race has four stages as shown below.

Race one

Stage

Distance (km)

1

19.9

2

6.5

3

35.2

4

20.0

Diagram showing a four-stage path from start to finish; stages are labelled sequentially with connecting lines. Finish is marked by a square.

The design for part of the program is shown below.

6.1 Loop 4 times
6.2 Get valid distance for stage

The following lines of code are written to input the distance for each stage of ‘Race one’.

…
Line 6 FOR stage FROM 0 TO 3 DO
Line 7 RECEIVE distance FROM KEYBOARD
…

Changes should be made to the code above to ensure that any number of stages could be processed.

Describe two changes that would be required.

16b
4 marks

The average stage distance for each race is calculated and stored in the variable avgDistance.

(i) Using a programming language of your choice, write the code to store the average to 1 decimal place.

[2]

(ii) The average stage distance should be displayed, as shown below for ‘Race one’.

The average is 20.4 km

Using a programming language of your choice and the variable avgDistance, write the code to produce the output above.

[2]

17
1 mark

WordSmart is a computer game where players are shown a definition of a word and are asked to enter the word being described.

If the correct word is entered, a point is awarded for each letter of the word as shown below.

Word

Points

DEMOCRACY

9

SERVER

6

State the predefined function used to count the number of characters in the player’s correct answer.

18
2 marks

A window replacement company employs a programmer to write a program that calculates how much it will cost to fit triple glazed windows.

Small windows cost £299.99 and large windows cost £499.99 to replace.

The algorithm to calculate the cost is shown below.

  1. Set totalCost to 0

  2. Ask for number of rooms

  3. Loop through the number of rooms

  4. Ask for the number of small windows

  5. Ask for the number of large windows

  6. Calculate and update totalCost

  7. End loop

  8. Display totalCost

The total cost should be output as shown below.

The cost is £14999.50 for the windows

Using a programming language of your choice and the variable name totalCost, write the code to produce the output above.

19
3 marks

A 9-hole golf course is introducing an app to replace paper scorecards. The total score is the number of times the player hits the ball to complete all 9 holes.

Below is an example of a paper scorecard that a player has filled in.

Golf scorecard for Sanya Abioye dated 25/5/2023. Scores: Hole 1-3, 2-4, 3-9, 4-4, 5-6, 6-6, 7-4, 8-5, 9-5. Total score: 46.

Each hole has a target score known as the par value.

If a score entered is three or more above the par value for that hole it is adjusted to the par value plus two. This is shown below for holes 3 and 6.

Golf scorecard showing holes 1-9, par values, scores, and final scores with a total score of 42. Score adjustments made on holes 3, 4, and 7.

Part of the code for the app is shown below.

…
Line 10 REPEAT 9 TIMES
Line 11 RECEIVE par FROM (INTEGER) KEYBOARD
Line 12 RECEIVE score FROM (INTEGER) KEYBOARD
Line 13 < calculate final score for each hole >
Line 14 < update totalScore >
Line 15 END REPEAT
Line 16 SEND totalScore TO DISPLAY
…

Using a programming language of your choice, write the code for line 13.

20a
2 marks

A communications company uses a program to calculate a customer’s average data usage over a 12-month period.

An example of a customer’s monthly data usage in gigabytes (GB) is shown below.

...

Feb

March

April

May

June

July

Aug

...

50

41

30.8

35.7

32,7

23

19

A customer’s average monthly data usage is displayed using the following code.

SEND aveData TO DISPLAY

The above code displays the following value to a customer.

23.3123

Using a programming language of your choice, re-write this code so the customer’s average monthly data usage will display as the following.

23.3

20b
3 marks

The following code checks if the customer receives both their mobile data and broadband from the company before offering them a discount.

(i) Identify the logical operator in the above code.

[1]

(ii) During translation the program stops and produces an error at line 48.

State the type of error that the programmer has made and how the error can be corrected.

[2]

21
3 marks

Tarvit High School is trialling a voting system to decide their representative for the pupil council. A programmer creates a voting app to allow pupils to cast their vote. The winner is displayed once voting is closed.

Tarvit High School 2023 Pupil Council vote: Candidates listed, voting option C highlighted. Current votes: Hendry 22, Murphy 13, Kowalski 19, Green 21.

After voting is closed three candidates have received the same number of votes. Their names are stored in a data structure called winners.

The app uses a predefined function to pick one candidate from winners.

Using a programming language of your choice, write the code that will display the name of the winning candidate.

22
2 marks

The following code is used to input and display the points in a game.

…
Line 5 RECEIVE teamName FROM KEYBOARD
Line 6 RECEIVE totalPoints FROM KEYBOARD
Line 7 <display output>
…

When ‘Scotland’ and ‘27’ are input the following output is produced by the program.

Scotland scored 27 points

Using a programming language of your choice write Line 7 of the program.

23
1 mark

A train company is designing a program to handle passenger complaints. Part of the design is shown below.

Flowchart for complaint handling: assess if delay, review complaint, display outcome, check passenger satisfaction, if satisfied, close case.

State which type of loop is used in this design.

24a
1 mark

The code below is being tested to ensure it produces the correct output.

…
Line 3 RECEIVE score FROM KEYBOARD
Line 4 IF score = 5 OR score = 10 OR score = 15 THEN
Line 5 IF NOT(score >= 3 AND score <= 12) THEN
Line 6 SEND "Success" TO DISPLAY
Line 7 END IF
Line 8 END IF

State the score that should be input in Line 3 to display ‘Success’.

24b
1 mark

State one logical operator used in the code above.