0% found this document useful (0 votes)
62 views

Assessment Test Algorithms

Uploaded by

vaghelisz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Assessment Test Algorithms

Uploaded by

vaghelisz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Name: Test on Date: 00/00/2020

Form: Computational Thinking Marks: / 68


Science

1. What value is assigned to the variable X by each of the following statements?

(a) X = 1 + 5/2

(b) X = (25 – 4) / 3

(c) X = 5 + 7 MOD 2

(d) X = ((27 + 5) /2) DIV 3

2. (a) Given that a = 7 and b = 6, state in each case whether x will be set to True or False.

(i) IF (a ≤ 7 AND b > 4) THEN


x  True
ELSE
x  False

(ii) IF (a > 7 OR b = 1) THEN


x  True
ELSE
x  False

(iii) IF (NOT (a > b) OR (b ≤ 7)) THEN


x  True
ELSE
x  False

(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” THEN
IF temperature > 20 THEN
picnic  “Yes”
ELSE
picnic  “No”
ENDIF
ENDIF
3. The following pseudocode uses definite iteration. It is intended to find and print the
largest number in an array numbers of ten positive integers.
max  numbers[0]
FOR n  1 to 9
IF max > numbers[n] THEN
max  numbers[n]
ENDIF
ENDFOR
OUTPUT (“Maximum is”, max)
(a) The pseudocode contains an error and does not work correctly. Correct the error.

(b) Write the corrected algorithm using a WHILE… ENDWHILE loop instead of a FOR…
ENDFOR loop

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.

FOR i  1 TO 20
Xcoord  RANDOM_INT(1, 400)
Ycoord  RANDOM_INT (1, 300)
size  RANDOM_INT (20, 70)
colourNumber  RANDOM_INT (1, 4)
IF colourNumber = 1 THEN
colour  “red”
ELSE IF colourNumber = 2 THEN
colour  “yellow”
ELSE IF colourNumber = 3 THEN
colour  “green”
ELSE
colour  “blue”
ENDIF
brushColour  colour
FillCircle(XCoord, YCoord, size)
ENDFOR
Delay 200 #delay a few seconds
Clear Screen

(a) What type of variable is brushColour?

(b) State what is meant by selection and iteration, using examples from Manjeri’s
program.

Selection:

Iteration:

(d) What does the statement colourNumber  RANDOM_INT (1, 4) do?

(c) 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?

(ii) What would the array contain?

(iii) Write a statement to assign a colour to the variable colour using the array.
(d) Give two reasons why it is important to use meaningful identifier names in
a program.

(e) Add statements to the program to make the routine repeat indefinitely.

5. The following subroutine returns a value to the main program

SUBROUTINE checkval (value, low, high)


IF value < low THEN
RETURN “Low”
ELSE IF value > high
RETURN “High”
ELSE
RETURN “OK”
ENDIF
ENDSUBROUTINE

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

(ii) State the data type of the return value

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

OUTPUT (“Please enter measurement:”)


measurement  USERINPUT

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.
(b) Give three reasons why programmers use subroutines.

6. A product type in a computer company is “HARDWARE”, “SOFTWARE” or “MANUAL”.

The following pseudocode algorithm is being written to validate the user input.

OUTPUT “Please enter product type:”


productType  USERINPUT
A IF

THEN

OUTPUT “valid product type entered”


ELSE
OUTPUT “product type invalid”
ENDIF

(a) Complete the missing IF statement at A.

(b) The following incomplete algorithm performs an alternative validation check on a


product code.
(i) Insert the missing lines at B and C.

OUTPUT “Please enter ProductID:”


productID  USERINPUT
validID  True
B

IF len(ProductID) <> 6 THEN


validID  False
OUTPUT “ProductID invalid - Please re-enter:”
productID  USERINPUT
ELSE
validID  True
ENDIF
C

(ii) What type of validation check is carried out by the algorithm?

7. A subroutine is given below.

SUBROUTINE calc (x, y)


WHILE x ≠ y
IF x > y THEN
x  x – y
ELSE
y  y – x
ENDIF
ENDWHILE
RETURN x
ENDSUBROUTINE

(a) Fill in the trace table below if the subroutines is called with the parameters 36, 30,
and state what value is returned to the calling program.

x y x≠y

36 30 True

Value returned to calling program: _______________


(b) What is the purpose of the program? (You may find it helpful to try calling the
subroutine with any two other values, for example 18 and 4).

8. The following is an incomplete algorithm for checking that a user has entered the
correct password. The password entered is to be checked against the password
“ABCdef123”

The user is given 3 attempts to get the password correct before being logged out.

passwordOK  False
count  1
password  input("Please enter password: ")
WHILE count < 3 and passwordOK = False

IF passwordOK = True THEN


OUTPUT("Welcome back")
ELSE
OUTPUT ("Logged out")
ENDIF

Complete the algorithm.

9. Emma is writing a program to encrypt text entered by a user. Only letters, spaces and
punctuation marks can be encrypted. Each letter in the text is replaced by the letter
three places further down the alphabet. A is replaced by D, b by e, … Y by B, z by c.
Spaces and punctuation marks are not changed.

Complete the test plan below by stating, for each input data, the expected outcome and
a reason for the test.
Expected
Input data Reason for test
outcome

HEY

XYZ123

I’m Zac

10. The pseudocode below shows an algorithm that is supposed to calculate the average of
a set of scores held in the array scores. The program contains two logic errors.

1. totalScore  0
2. score  [25, 17, -5, 28, 30]
3. FOR n  0 TO LEN (score)
4. totalScore  totalScore + score (n)
5. ENDFOR
6. averageScore  totalScore / LEN (score) – 1

Identify the two lines with logic errors in the above pseudocode, and write correct
versions of each so that the program will work as intended.

11. The following subroutine calculates a rail fare, based on the distance travelled. A
discount of 10% is given for off-peak travel.

SUBROUTINE RailFare(distance, day, time)


IF distance < 5
RailFare  3.00
ELSE
IF distance < 12
RailFare  4.00
ELSE
RailFare  5.00
ENDIF

IF (day = "Saturday") or (day = "Sunday"):


RailFare  0.9 * RailFare
ELSE
IF(time ≥ 10) and (time < 16) THEN
RailFare  0.9 * RailFare
ENDIF
RETURN RailFare
ENDSUBROUTINE
What does the subroutine return if it is called with each of the following statements:

(a) RailFare (4, “Monday”, 8.00)

(b) RailFare (15, “Saturday”, 14.00)

(c) RailFare (8, “Tuesday”, 18.00)

12. Indicate whether each of the following statements are true or false.

True or False?

All programs have to be converted to machine code


before they can be executed
A compiler translates a high-level language into machine
code
An interpreter converts a low-level language into
machine code
High-level languages are said to be “machine-
independent”

[Total 68 marks]

You might also like