Constructs (Cambridge (CIE) AS Computer Science): Exam Questions

Exam code: 9618

42 mins14 questions
11 mark

The final CASE condition (> 200) in the pseudocode example could be replaced with a keyword.

Give the keyword.

2a2 marks

One type of run-time error can cause a program to stop responding (‘freezing’).

Identify a particular type of programming construct that can generate this type of error and explain why it occurs.

Construct .......................................................................................................

Explanation.....................................................................................................

2b2 marks

The function Process() contains a selection construct using a CASE structure.

Write pseudocode using a single selection construct with the same functionality without using a CASE structure.

34 marks

Refer to the insert (opens in a new tab) for the list of pseudocode functions and operators.

The following table contains pseudocode examples.

Each example may contain statements that relate to one or more of the following:

  • selection

  • iteration (repetition)

  • subroutine (procedure or function).

Complete the table by placing one or more ticks ('') in each row.

Pseudocode example

Selection

Iteration

Subroutine

FOR Index ← 1 TO 3
IF Safe[Index] = TRUE THEN
Flap[Index] ← 0
ENDIF
NEXT Index

CASE OF Compound(3)

REPEAT UNTIL AllDone() = TRUE

WHILE Result[3] <> FALSE

42 marks

An algorithm will:

  1. prompt and input a sequence of 100 integer values, one at a time

  2. sum the positive integers

  3. output the result of the sum.

The algorithm requires the use of basic constructs. One of these is sequence.

Identify one other basic construct required by the algorithm and describe how it is used.

Construct....................................................................................................

Use..............................................................................................................

52 marks

A global 1D array of strings contains three elements which are assigned values as shown:

Data[1] leftwards arrow"aaaaaa"
Data[2] leftwards arrow"bbbbbb"
Data leftwards arrow[3] "cccccc"

Procedure Process() manipulates the values in the array.

The procedure is written in pseudocode as follows:

PROCEDURE Process(Format : STRING)
DECLARE Count, Index, L : INTEGER
DECLARE Result : STRING
DECLARE C : CHAR

Resultleftwards arrow "****"

FOR leftwards arrowCount 1 TO LENGTH(Format) STEP 2
Cleftwards arrowMID(Format, Count, 1)
Lleftwards arrowSTR_TO_NUM(MID(Format, Count + 1, 1))

Index (Count + 1) DIV 2

CASE OF C
'X' : leftwards arrowResult TO_UPPER(Data[Index])
'Y' : leftwards arrowResult TO_LOWER(Data[Index])
'Z' : leftwards arrowResult "**" & Data[Index]
ENDCASE

Data[Index] LEFT(Result, L)
NEXT Count

ENDPROCEDURE

The procedure is to be modified. If variable C is assigned a value other than 'X', 'Y' or 'Z', then procedure Error() is called and passed the value of variable C as a parameter.

This modification can be implemented by adding a single line of pseudocode.

(i) Write the single line of pseudocode.

[1]

(ii) State where this new line should be placed.

[1]

62 marks

A program is being designed in pseudocode.

The program contains a global 1D array Data of type string containing 200 elements.

The first element has the index value 1.

A procedure Process() is written to initialise the values in the array:

PROCEDURE Process(Label : STRING)
DECLARE Index : INTEGER
Index leftwards arrow0
INPUT Data[Index]
WHILE Index < 200
Index leftwards arrowIndex + 1
CASE OF (Index MOD 2)
0 : Data[Index] leftwards arrowTO_UPPER(Label)
1 : Data[Index] leftwards arrowTO_LOWER(Label)
OTHERWISE : OUTPUT "Alarm 1201"
ENDCASE
NEXT Index
OUTPUT "Completed " & Index & " times"
ENDPROCEDURE

The procedure contains a statement that is not needed.

Identify the pseudocode statement and explain why it is not needed.

Statement ..........................................................................................

Explanation .........................................................................................

72 marks

A program uses a global 1D array of type string and a text file.

An algorithm that forms part of the program is expressed as follows:

  • copy the first line from the file into the first element of the array

  • copy the second line from the file into the second element of the array

  • continue until all lines in the file have been copied into the array.

Sequence is one programming construct. Identify one other programming construct that will be required when the algorithm from part (a) is converted into pseudocode and explain its use.

Construct..........................................................................................................

Use....................................................................................................................

83 marks

A record structure is declared to hold data relating to components being produced in a factory:

TYPE Component
DECLARE Item_ID : STRING
DECLARE Reject : BOOLEAN
DECLARE Weight : REAL
ENDTYPE

The factory normally produces a batch (or set) of 1000 components at a time. A global array is declared to store 1000 records for a batch:

DECLARE Batch : ARRAY [1:1000] OF Component

Two global variables contain the minimum and maximum acceptable weight for each component. The values represent an inclusive range and are declared as:

DECLARE Min, Max : REAL

A program uses a variable ThisIndex as the array index to access a record.

Write a pseudocode clause to check whether or not the weight of an individual component is within the acceptable range.

92 marks

A program is being designed in pseudocode.

The program contains the following declaration:

DECLARE Data : ARRAY[1:1000] OF STRING

A procedure ArrayInitialise() is written to initialise the values in the array:

PROCEDURE ArrayInitialise(Label : STRING)
DECLARE Index : INTEGER
Indexleftwards arrow1
WHILE Index <= 1000
CASE OF (Index MOD 2)
0 : Data[Index]leftwards arrow FormatA(Label)
Index leftwards arrowIndex + 1
1 : Data[Index]leftwards arrow FormatB(Label)
Index leftwards arrowIndex + 1
ENDCASE
ENDWHILE
ENDPROCEDURE

Functions FormatA() and FormatB() apply fixed format case changes to the parameter string.

The design of the procedure does not use the most appropriate loop construct.

Suggest a more appropriate construct that could be used and explain your choice.

Construct ........................................................................................................

Explanation ......................................................................................................

10a4 marks

Refer to the insert (opens in a new tab)for the list of pseudocode functions and operators.

The following pseudocode represents part of the algorithm for a program:

CASE OF ThisValue
< 30 : Level leftwards arrow"Low" // less than 30
Check leftwards arrow 1
< 20 : Level leftwards arrow"Very Low" // less than 20
Check leftwards arrow ThisValue / 2
30 TO 40 : Level leftwards arrow"Medium" // between 30 and 40
Check leftwards arrow ThisValue / 3
Data[ThisValue] leftwards arrow Data[ThisValue] + 1
> 40 : Level leftwards arrow "High"
ENDCASE

Complete the table by writing the answer for each row:

Answer

The value assigned to Level when ThisValue is 40

The value assigned to Check when ThisValue is 36

The value assigned to Level when ThisValue is 18

The number of elements in array Data that may be incremented

10b3 marks

The pseudocode contains four assignments to variable Level. One of these assignments will never be performed.

Identify this assignment and explain why this is the case.

10c1 mark

The following line is added immediately before the ENDCASE statement:

OTHERWISE : Level leftwards arrow "Undefined"

State why this assignment is never performed.

112 marks

Sequence is one programming construct.

Identify two other programming constructs that will be required when the algorithm is converted into pseudocode.

Construct 1 ..........................................................................................................

Construct 2 .........................................................................................................

122 marks

An algorithm will:

  1. input a sequence of integer values, one at a time

  2. ignore all values until the value 27 is input, then sum the remaining values in the sequence

  3. stop summing values when the value 0 is input and then output the sum of the values.

The solution to the algorithm includes iteration.

Give the name of a suitable loop structure that could be used.

Justify your answer.

Name .....................................................................................................................

Justification ............................................................................................................

132 marks

A program contains six modules:

Pseudocode module header

PROCEDURE Module-A()

PROCEDURE Module-X(T1 : INTEGER, S2 : REAL)

PROCEDURE Reset(BYREF Code : INTEGER)

FUNCTION Restore(OldCode : INTEGER) RETURNS BOOLEAN

FUNCTION Module-Y(RA : INTEGER, RB : BOOLEAN) RETURNS BOOLEAN

FUNCTION Module-Z(SA : INTEGER) RETURNS INTEGER

Module-X() calls Reset()
Module-Y() calls Restore()

Flowchart with Module-A at the top, splitting into three branches with rectangles, arrows, circles, and a diamond.

Explain the meaning of the diamond symbol as used in the diagram in part (a).

146 marks

A global 1D array of integers contains four elements, which are assigned values as shown:

Mix[1] leftwards arrow 4
Mix[2] leftwards arrow 2
Mix[3] leftwards arrow 3
Mix[4] leftwards arrow 5

A procedure Process() manipulates the values in the array.

The procedure is written in pseudocode as follows:

PROCEDURE Process(Start : INTEGER)
DECLARE Value, Index, Total : INTEGER

Index leftwards arrow Start
Total leftwards arrow 0

WHILE Total < 20
Value leftwards arrow Mix[Index]
Total leftwards arrowTotal + Value

IF Index < 4 THEN
Mix[Index] leftwards arrow Mix[Index] + Mix[Index+1]
ELSE
Mix[Index] leftwards arrow Mix[Index] + Mix[1]
ENDIF
Index leftwards arrow (Value MOD 4) + 1

ENDWHILE

Mix[1] leftwards arrow Total * Index

ENDPROCEDURE

Complete the trace table on the opposite page by dry running the procedure when it is called as follows:

CALL Process(2)

Index

Value

Total

Mix[1]

Mix[2]

Mix[3]

Mix[4]