0% found this document useful (0 votes)
6 views13 pages

Trace Table Questions

The document contains a worksheet for a computer science class focused on pseudocode and string handling functions. It includes exercises on trace tables, function behavior, and pseudocode modifications for various scenarios. Additionally, it addresses the concept of adaptive maintenance in programming and requires students to analyze and correct pseudocode errors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

Trace Table Questions

The document contains a worksheet for a computer science class focused on pseudocode and string handling functions. It includes exercises on trace tables, function behavior, and pseudocode modifications for various scenarios. Additionally, it addresses the concept of adaptive maintenance in programming and requires students to analyze and correct pseudocode errors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

AS Computer Science

Trace Table Worksheet


Name:__________________ Date:______________
4 The following pseudocode is a string handling function.
For the built-in functions list, refer to the Appendix on page 16.
FUNCTION Clean(InString : STRING) RETURNS STRING
DECLARE NewString : STRING
DECLARE Index : INTEGER
DECLARE AfterSpace : BOOLEAN
DECLARE NextChar : CHAR
CONSTANT Space = ' '
AfterSpace FALSE
NewString ""

FOR Index 1 TO LENGTH(InString)


NextChar MID(InString, Index, 1)
IF AfterSpace = TRUE
THEN

B
IF NextChar <> Space
THEN
NewString NewString & NextChar

ENDIF
ELSE
AfterSpace FALSE
AI
H
NewString NewString & NextChar
IF NextChar = Space
THEN
ZO

AfterSpace TRUE
ENDIF
ENDIF
ENDFOR

RETURN NewString
S

ENDFUNCTION
C

© UCLES 2019 9608/21/M/J/19

Page 1 of 13
9

(a) (i) Complete the trace table by performing a dry run of the function when it is called as
follows:

Result Clean("X∇∇∇Y∇and∇∇Z")

The symbol '∇' represents a space character. Use this symbol to represent a space
character in the trace table.

Index AfterSpace NextChar NewString

B
AI
H
ZO
S
C

[6]

(ii) State the effect of the function Clean().

...........................................................................................................................................

..................................................................................................................................... [1]

© UCLES 2019 9608/21/M/J/19 [Turn over


Page 2 of 13
10

(iii) The pseudocode is changed so that the variable AfterSpace is initialised to TRUE.

Explain what will happen if the function is called as follows:

Result Clean("∇∇X∇∇∇Y∇and∇∇Z")

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

..................................................................................................................................... [2]

(b) The following pseudocode declares and initialises an array.

DECLARE Code : ARRAY[1:100] OF STRING


DECLARE Index : INTEGER

B
FOR Index 1 TO 100
Code[Index] ""
ENDFOR

The design of the program is changed as follows:


AI
H
the array needs to be two dimensional, with 500 rows and 4 columns
• the elements of the array need to be initialised to the string "Empty"
ZO

Re-write the pseudocode to implement the new design.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................
S

...................................................................................................................................................
C

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

............................................................................................................................................. [4]

(c) State the term used for changes that are made to a program in response to a specification
change.

............................................................................................................................................. [1]

© UCLES 2019 9608/21/M/J/19

Page 3 of 13
8

4 The following is pseudocode for a string handling function.

For the built-in functions list, refer to the Appendix on page 16.

FUNCTION Search(InString : STRING) RETURNS INTEGER

DECLARE NewString : STRING


DECLARE Index : INTEGER
DECLARE NextChar : CHAR
DECLARE Selected : INTEGER
DECLARE NewValue : INTEGER

NewString '0'
Selected 0

FOR Index 1 TO LENGTH(InString)

NextChar MID(InString, Index, 1)


IF NextChar < '0' OR NextChar > '9'

B
THEN
NewValue STRING_TO_NUM(NewString)
IF NewValue > Selected
THEN

ENDIF
Selected NewValue AI
H
NewString '0'
ELSE
NewString NewString & NextChar
ZO

ENDIF

ENDFOR

RETURN Selected

ENDFUNCTION
S
C

© UCLES 2019 9608/22/M/J/19

Page 4 of 13
9

(a) (i) The following assignment calls the Search() function:

Result Search("12∇34∇5∇∇39")

Complete the following trace table by performing a dry run of this function call.

The symbol '∇' represents a space character. Use this symbol to represent a space
character in the trace table.

Index NextChar Selected NewValue NewString

B
AI
H
ZO
S
C

[5]

(ii) State the value returned by the function when it is called as shown in part (a)(i).

....................................... [1]

© UCLES 2019 9608/22/M/J/19 [Turn over


Page 5 of 13
4

(b) The following pseudocode algorithm has been developed to check whether a string contains
a valid password.

To be a valid password, a string must:

• be longer than 6 characters


• contain at least one lower case letter
• contain at least one upper case letter
• contain at least one non-alphabetic character.

10 FUNCTION Check(InString : STRING) RETURNS BOOLEAN


11
12 DECLARE Index : INTEGER
13 DECLARE StrLen : INTEGER
14 DECLARE NumUpper, NumLower : INTEGER
15 DECLARE NumNonAlpha : INTEGER
16 DECLARE NextChar : CHAR
17
18 NumUpper 0

B
19 NumLower 0
20 NumNonAlpha 0
21
22
23
24
StrLen LENGTH(InString)
IF StrLen < 7
THEN
AI
H
25 RETURN FALSE
26 ELSE
27 FOR Index 1 TO StrLen
ZO

28 NextChar MID(InString, Index, 1)


29 IF NextChar >= 'a' AND NextChar <= 'z'
30 THEN
31 NumLower NumLower + 1
32 ELSE
33 IF NextChar > 'A' AND NextChar <= 'Z'
34 THEN
S

35 NumUpper NumUpper + 1
36 ELSE
C

37 NumNonAlpha NumNonAlpha + 1
38 ENDIF
39 ENDIF
40 ENDFOR
41 ENDIF
42
43 IF (NumUpper >= 1) AND (NumLower >= 1) AND (NumNonAlpha >= 1)
44 THEN
45 RETURN TRUE
46 ELSE
47 RETURN FALSE
48 ENDIF
49
50 ENDFUNCTION

Refer to the Appendix on page 21 for a list of built-in pseudocode functions and operators.

© UCLES 2020 9608/21/M/J/20

Page 6 of 13
5

The pseudocode does not work under all circumstances.

A dry run performed on the function Check(), with the string "crAsh99", produced the
following trace table.

The string is a valid password, but the pseudocode would return the value FALSE.

Trace
StrLen Index NextChar NumUpper NumLower NumNonAlpha
table row
1 7 0 0 0
2 1 'c'
3 1
4 2 'r'
5 2
6 3 'A'
7 1

B
8 4 's'
9 3
10
11
5 'h' AI 4
H
12 6 '9'
13 2
ZO

14 7 '9'
15 3

(i) Describe how the completed trace table may be used to identify the error in the
pseudocode. In your answer, refer to the trace table row number(s).
S

...........................................................................................................................................
C

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

..................................................................................................................................... [2]

(ii) State the pseudocode line number that has to be changed to correct the error and write
the correct pseudocode for the complete line.

Line number ......................................................................................................................

Correct pseudocode ..........................................................................................................

...........................................................................................................................................
[2]

© UCLES 2020 9608/21/M/J/20 [Turn over


Page 7 of 13
6

(iii) Rewrite lines 29 to 39 of the original pseudocode using a CASE structure.

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

..................................................................................................................................... [4]

B
AI
H
ZO
S
C

© UCLES 2020 9608/21/M/J/20

Page 8 of 13
8

(b) The following pseudocode algorithm has been developed to check whether a string contains
a valid password.

To be a valid password, a string must:

• be longer than five characters


• contain at least one numeric digit
• contain at least one upper case letter
• contain at least one other character (not a numeric digit or an upper case letter).

10 FUNCTION Check(InString : STRING) RETURNS BOOLEAN


11
12 DECLARE Index : INTEGER
13 DECLARE StrLen : INTEGER
14 DECLARE NumUpper, NumDigit : INTEGER
15 DECLARE NextChar : CHAR
16 DECLARE NumOther : INTEGER
17
18 NumUpper 0

B
19 NumDigit 0
20
21 StrLen LENGTH(InString)
22
23
24
IF StrLen < 6
THEN
RETURN FALSE
AI
H
25 ELSE
26 FOR Index 1 TO StrLen - 1
27 // loop for each character
ZO

28 NextChar MID(InString, Index, 1)


29 IF NextChar >= '0' AND NextChar <= '9'
30 THEN
31 NumDigit NumDigit + 1 // count digits
32 ELSE
33 IF NextChar >= 'A' AND NextChar <= 'Z'
34 THEN
S

35 NumUpper NumUpper + 1 // count upper case


36 ENDIF
C

37 ENDIF
38 ENDFOR
39 ENDIF
40
41 NumOther StrLen – (NumDigit – NumUpper)
42 IF NumDigit >= 1 AND NumUpper >= 1 AND NumOther >= 1
43 THEN
44 RETURN TRUE
45 ELSE
46 RETURN FALSE
47 ENDIF
48
49 ENDFUNCTION

© UCLES 2020 9608/23/M/J/20

Page 9 of 13
9

The pseudocode does not work under all circumstances.

The function was dry run with the string "1234AP" and the following trace table was
produced. The string is an invalid password, but the pseudocode returned the value
TRUE.

Trace
StrLen Index NextChar NumUpper NumDigit NumOther
table row
1 6 0 0
2 1 '1'
3 1
4 2 '2'
5 2
6 3 '3'
7 3

B
8 4 '4'
9
10
11
5 'A' AI 1
4
H
12 3
ZO

(i) The pseudocode algorithm contains two errors.

State how the given trace table indicates the existence of each error.

Error 1 ...............................................................................................................................
S

...........................................................................................................................................

...........................................................................................................................................
C

...........................................................................................................................................

Error 2 ...............................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................
[2]

© UCLES 2020 9608/23/M/J/20 [Turn over


Page 10 of 13
10

(ii) Give the line number of each error in the pseudocode algorithm and write the modified
pseudocode to correct each error.

Line number for error 1 .....................................................................................................

Correct pseudocode ..........................................................................................................

...........................................................................................................................................

Line number for error 2 .....................................................................................................

Correct pseudocode ..........................................................................................................

...........................................................................................................................................
[2]

(c) The term adaptive maintenance refers to amendments that are made in response to
changes to the program specification. These changes usually affect the program algorithm.

B
Name one other part of the design that can change as a result of adaptive maintenance.

............................................................................................................................................. [1]

AI
H
ZO
S
C

© UCLES 2020 9608/23/M/J/20

Page 11 of 13
10

4 Study the following pseudocode. Line numbers are for reference only.

10 FUNCTION Convert(Name : STRING) RETURNS STRING


11
12 DECLARE Flag: BOOLEAN
13 DECLARE Index : INTEGER
14 DECLARE ThisChar : CHAR
15 DECLARE NewName : STRING
16
17 CONSTANT SPACECHAR = ' '
18
19 Flag TRUE
20 Index 1
21 NewName "" // formatted name string
22
23 WHILE Index <= LENGTH(Name)
24 ThisChar MID(Name, Index, 1)
25 IF Flag = TRUE THEN
26 NewName NewName & UCASE(ThisChar)

B
27 IF ThisChar <> SPACECHAR THEN
28 Flag FALSE
29 ENDIF
30
31
32
ELSE
NewName
ENDIF
NewName & ThisChar AI
H
33 IF ThisChar = SPACECHAR THEN
34 Flag TRUE
35 ENDIF
ZO

36 Index Index + 1
37 ENDWHILE
38
39 RETURN NewName
40
41 ENDFUNCTION
S
C

© UCLES 2021 9618/21/M/J/21

Page 12 of 13
11

(a) Complete the trace table below by dry running the function when it is called as follows:

Result Convert("∇in∇a∇∇Cup")

Note: The symbol '∇' has been used to represent a space character.
Use this symbol for any space characters in the trace table.

The first row has been completed for you.

Name Flag Index NewName ThisChar

"∇in∇a∇∇Cup"

B
AI
H
ZO
S
C

[5]

© UCLES 2021 9618/21/M/J/21 [Turn over


Page 13 of 13

You might also like