Cambridge A Level Computer Science 9618 — 2022 May/June Paper 2 · Variant 2
9618/22/M/J/22 · 8 questions · 75 marks · ≈84 min
The question paper and its mark scheme, free to read here and free to download. This is Cambridge’s own paper, exactly as it was sat.
Question paper20 pages




















Mark scheme14 pages
Answers below. Sit the paper first if you are practising.














Questions as text
Q1 · A programmer is testing a program using an Integrated Development Environment (IDE)
1 (a) A programmer is testing a program using an Integrated Development Environment (IDE). The programmer wants the program to stop when it reaches a specific instruction or program statement in order to check the value assigned to a variable. Give the technical term for the position at which the program stops. ............................................................................................................................................. [1] (b) The following table lists some activities from the program development life cycle. Complete the table by writing the life cycle stage for each activity. Activity Life cycle stage An identifier table is produced. Syntax errors can occur. The developer discusses the program requirements with the customer. A trace table is produced. [4] (c) An identifier table includes the names of identifiers used. State two other pieces of information that the identifier table should contain. 1 ................................................................................................................................................ 2 ................................................................................................................................................ [2] (d) The pseudocode statements in the following table may contain errors. State the error in each case or write 'NO ERROR' if the statement contains no error. You can assume that none of the variables referenced are of an incorrect type. Statement Error Status TRUE AND FALSE IF LENGTH("Password") < "10" THEN Code LCASE("Electrical") Result IS_NUM(-27.3) [4]
Mark scheme: Question Answer Marks 1(a) Correct answer only: 1 Breakpoint 1(b) One mark per row 4 Activity Life cycle stage An identifier table is produced. Design Syntax errors can occur. Coding The developer discusses the program requirements with the Analysis customer. A trace table is produced. Testing 1(c) One mark per bullet point to Max 2 2 A description of what the identifier is used for / the purpose of the identifier The data type of the identifier The number of elements of an array // the length of a string An example data value Value of any constants used The scope of the variable (local or global) 1(d) One mark per row 4 Statement Error Status TRUE AND FALSE NO ERROR IF LENGTH("Password") < "10" shouldn’t be a string // must be an "10" THEN integer Code Parameter must be a char // cannot be a LCASE("Electrical") string Alternative: LCASE should be TO_LOWER Result IS_NUM(-27.3) Parameter must be a string / char // cannot be a number
Q2 · An algorithm is described as follows: 1
2 An algorithm is described as follows: 1. Input an integer value. 2. Jump to step 6 if the value is less than zero. 3. Call the function IsPrime() using the integer value as a parameter. 4. Keep a count of the number of times function IsPrime() returns TRUE. 5. Repeat from step 1. 6. Output the value of the count with a suitable message. Draw a program flowchart to represent the algorithm. START END [4]
Mark scheme: 2 4 One mark per point: 1 Initialise Count before loop AND Input NextNum in a loop 2 Loop until NextNum < 0 AND OUTPUT statement including Count plus a message 3 Use of IsPrime(NextNum)as a function (must return a value) 4 Check return value AND increment Count if appropriate
Q3 · The module headers for five modules in a program are defined in pseudocode as follows…
3 (a) The module headers for five modules in a program are defined in pseudocode as follows: Pseudocode module header FUNCTION Mod_V(S2 : INTEGER) RETURNS BOOLEAN PROCEDURE Mod_W(P4 : INTEGER) PROCEDURE Mod_X(T4 : INTEGER, BYREF P3 : REAL) PROCEDURE Mod_Y(W3 : REAL, Z8 : INTEGER) FUNCTION Mod_Z(F3 : REAL) RETURNS INTEGER An additional module Head() repeatedly calls three of the modules in sequence. A structure chart has been partially completed. (i) Complete the structure chart to include the information given about the six modules. Do not label the parameters and do not write the module names. A B C D E F [3] (ii) Complete the table using the information in part 3(a) by writing each module name to replace the labels A to F. Label Module name A B C D E F [3] (b) The structure chart represents part of a complex problem. The process of decomposition is used to break down the complex problem into sub-problems. Describe three benefits of this approach. 1 ................................................................................................................................................ ................................................................................................................................................... 2 ................................................................................................................................................ ................................................................................................................................................... 3 ................................................................................................................................................ ................................................................................................................................................... [3]
Mark scheme: 3(a)(i) One mark per red annotation 3 3(a)(ii) 3 Label Module name A Head B Mod_W C Mod_X D Mod_V E Mod-Z F Mod_Y Marks as follows: Two rows correct – one mark Four rows correct – two marks All rows correct – three marks 3(b) One mark per point: 3 Breaking a complex problem down makes it easier to understand / solve // smaller problems are easier to understand / solve Smaller problems are easier to program / test / maintain Sub-problems can be given to different teams / programmers with different expertise // can be solved separately
Q4 · A procedure LastLines() will: • take the name of a text file as a parameter • output the…
4 (a) A procedure LastLines() will: • take the name of a text file as a parameter • output the last three lines from that file, in the same order as they appear in the file. Note: • Use local variables LineX, LineY and LineZ to store the three lines from the file. • You may assume the file exists and contains at least three lines. Write pseudocode for the procedure LastLines(). ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ............................................................................................................................................. [6] (b) The algorithm in part (a) is to be amended. The calling program will pass the number of lines to be output as well as the name of the text file. The number of lines could be any value from 1 to 30. It can be assumed that the file contains at least the number of lines passed. Outline three changes that would be needed. 1 ................................................................................................................................................ ................................................................................................................................................... ................................................................................................................................................... 2 ................................................................................................................................................ ................................................................................................................................................... ................................................................................................................................................... 3 ................................................................................................................................................ ................................................................................................................................................... ................................................................................................................................................... [3]
Mark scheme: 4(a) PROCEDURE LastLines(ThisFile : STRING) 6 DECLARE ThisLine, LineX, LineY, LineZ : STRING OPENFILE ThisFile FOR READ LineY "" LineZ "" WHILE NOT EOF(ThisFile) READFILE Thisfile, ThisLine // read a line LineX LineY LineY LineZ LineZ ThisLine ENDWHILE CLOSEFILE ThisFile OUTPUT LineX OUTPUT LineY OUTPUT LineZ ENDPROCEDURE Marks as follows to Max 6: 1 Procedure heading (including parameter) and ending 2 Declaration of local variables for three lines AND File OPEN in READ mode AND CLOSE 3 Loop until EOF(ThisFile) 4 Read line from file... in a loop 5 Attempt at a shuffle… in a loop 6 Correctly shuffle LineX, LineY and LineZ in a loop 7 OUTPUT the three lines in correct sequence, following reasonable attempt 4(a) Alternative (using two loops): PROCEDURE LastLines(ThisFile : STRING) DECLARE ThisLine, LineX, LineY, LineZ : STRING DECLARE Count, Count2 : INTEGER Count 0 OPENFILE ThisFile FOR READ WHILE NOT EOF(ThisFile) READFILE Thisfile, ThisLine // read a line Count Count + 1 ENDWHILE CLOSEFILE ThisFile OPENFILE ThisFile FOR READ FOR Count2 1 TO Count - 3 READFILE Thisfile, ThisLine // read a line NEXT Count2 READFILE Thisfile, LineX READFILE Thisfile, LineY READFILE Thisfile, LineZ OUTPUT LineX OUTPUT LineY OUTPUT LineZ CLOSEFILE ThisFile ENDPROCEDURE Marks as follows to Max 6: 1 Procedure heading (including parameter) and ending 2 Declaration of local variables for three lines AND (at least one) File OPEN in READ mode AND CLOSE 3 Loop until EOF(ThisFile) 4 Read line from file and increment Count in a loop 5 Two separate loops, closing and re-opening the file between loops 6 Read Count - 3 lines from the file 7 OUTPUT the last three lines in correct sequence, following reasonable attempt 4(b) One mark per point to Max 3: 3 1 Change the procedure header to include a (numeric) parameter (as well as the filename) 2 Replace LineX, Y and Z with an array 3 Amend shuffle mechanism 4 Use new parameter to determine first line to output 5 Output the lines in a loop Alternative 'two loop' solution to Max 3: 1 Change the procedure header to include a numeric parameter (as well as the filename) 2 A loop to count the total number of lines in the file 3 Ref use of single variable rather than LineX, LineY and LineZ 4 Close and re-open the file 5 Use the new parameter value to determine first line to output 6 Output the lines in a loop
Q5 · Study the following pseudocode
5 Study the following pseudocode. Line numbers are for reference only. 10 PROCEDURE Encode() 11 DECLARE CountA, CountB, ThisNum : INTEGER 12 DECLARE ThisChar : CHAR 13 DECLARE Flag : BOOLEAN 14 CountA 0 15 CountB 10 16 Flag TRUE 17 INPUT ThisNum 18 WHILE ThisNum <> 0 19 ThisChar LEFT(NUM_TO_STR(ThisNum), 1) 20 IF Flag = TRUE THEN 21 CASE OF ThisChar 22 '1' : CountA CountA + 1 23 '2' : IF CountB < 10 THEN 24 CountA CountA + 1 25 ENDIF 26 '3' : CountB CountB - 1 27 '4' : CountB CountB - 1 28 Flag FALSE 29 OTHERWISE : OUTPUT "Ignored" 30 ENDCASE 31 ELSE 32 IF CountA > 2 THEN 33 Flag NOT Flag 34 OUTPUT "Flip" 35 ELSE 36 CountA 4 37 ENDIF 38 ENDIF 39 INPUT ThisNum 40 ENDWHILE 41 OUTPUT CountA 42 ENDPROCEDURE (a) Procedure Encode() contains a loop structure. Identify the type of loop and state the condition that ends the loop. Do not include pseudocode statements in your answer. Type .......................................................................................................................................... Condition .................................................................................................................................. ................................................................................................................................................... [2] (b) Complete the trace table below by dry running the procedure Encode() when the following values are input: 12, 24, 57, 43, 56, 22, 31, 32, 47, 99, 0 The first row is already complete. ThisNum ThisChar CountA CountB Flag OUTPUT 0 10 TRUE [6] (c) Procedure Encode() is part of a modular program. Integration testing is to be carried out on the program. Describe integration testing. ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ............................................................................................................................................. [2]
Mark scheme: 5(a) One mark for type and one mark for condition: 2 Independent marks Type: pre-condition Condition: when the value of ThisNum / the input value is equal to zero 5(b) 6 ThisNum ThisChar CountA CountB Flag OUTPUT 0 10 TRUE 12 '1' 1 24 '2' 57 '5' "Ignored" 43 '4' 9 FALSE '5' 56 4 22 '2' TRUE "Flip" '3' 31 8 32 '3' 7 47 '4' 6 FALSE 99 '9' TRUE "Flip" 0 4 Marks as follows: One mark per outlined group If no marks per group then mark by columns (columns 3 to 6) for max 4 5(c) One mark per point: 2 Modules that have already been tested individually are combined into a single (sub) program which is then tested as a whole
Q6 · A string represents a series of whole numbers, separated by commas
6 A string represents a series of whole numbers, separated by commas. For example: "12,13,451,22" Assume that: • the comma character ',' is used as a separator • the string contains only the characters '0' to '9' and the comma character ','. A procedure Parse will: • take the string as a parameter • extract each number in turn • calculate the total value and average value of all the numbers • output the total and average values with a suitable message. Write pseudocode for the procedure. PROCEDURE Parse(InString : STRING) .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... .......................................................................................................................................................... ENDPROCEDURE [7]
Mark scheme: 6 PROCEDURE Parse(InString : STRING) 7 DECLARE Count, Total, Index : INTEGER DECLARE Average : REAL DECLARE NumString : STRING DECLARE ThisChar : CHAR CONSTANT COMMA = ',' Count 0 Total 0 NumString "" FOR Index 1 to LENGTH(InString) ThisChar MID(InString, Index, 1) IF ThisChar = COMMA THEN Total Total + STR_TO_NUM(NumString) Count Count + 1 NumString "" ELSE NumString NumString & ThisChar // build the number string ENDIF NEXT Index // now process the final number Total Total + STR_TO_NUM(NumString) Count Count + 1 Average Total / Count OUTPUT "The total was ", Total, " and the average was ", Average ENDPROCEDURE Marks as follows: 1 Declare and initialise Count, Total and NumString 2 Loop for number of characters in InString 3 Extract a character and test for comma in a loop 4 If comma, convert NumString to integer and update Total and Count 5 and reset NumString 6 Otherwise append character to NumString 7 Calculate average AND final output statement(s) outside the loop
Q7 · A programming language has string functions equivalent to those given in the insert
7 A programming language has string functions equivalent to those given in the insert. The language includes a LEFT() and a RIGHT() function, but it does not have a MID() function. (a) Write pseudocode for an algorithm to implement your own version of the MID() function which will operate in the same way as that shown in the insert. Do not use the MID() function given in the insert, but you may use any of the other functions. Assume that the values passed to the function will be correct. ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ............................................................................................................................................. [4] (b) The values passed to your MID() function in part (a) need to be validated. Assume that the values are of the correct data type. State two checks that could be applied to the values passed to the function. 1 ................................................................................................................................................ ................................................................................................................................................... 2 ................................................................................................................................................ ................................................................................................................................................... [2]
Mark scheme: 7(a) FUNCTION MID(InString : STRING, Start, Num : INTEGER) 4 RETURNS STRING DECLARE MidString : STRING DECLARE InStringLen : INTEGER InStringLen LENGTH(InString) // solution for RIGHT() then LEFT() MidString RIGHT(InString, InStringLen – Start + 1) MidString LEFT(MidString, Num) // alternative solution for LEFT() then RIGHT() MidString LEFT(InString, Start + Num - 1) MidString RIGHT(MidString, Num) RETURN MidString ENDFUNCTION Marks as follows: 1 Function heading and ending including parameters and return type 2 Correct use of one substring functions 3 Correct use of both substring functions (in correct sequence) 4 Return substring after a reasonable attempt 7(b) One mark per point 2 Check that: Start and/or Num are >= 1 // positive Length of InString is "sufficient" for required operation
Q8 · A program allows a user to save passwords used to log in to websites
8 A program allows a user to save passwords used to log in to websites. A stored password is then inserted automatically when the user logs in to the corresponding website. A global 2D array Secret of type STRING stores the passwords together with the website domain name where they are used. Secret contains 1000 elements organised as 500 rows by 2 columns. Unused elements contain the empty string (""). These may occur anywhere in the array. An example of a part of the array is: Array element Value Secret[27, 1] "thiswebsite.com" Secret[27, 2] Secret[28, 1] "thatwebsite.com" Secret[28, 2] Note: • For security, the passwords are stored in an encrypted form, shown as "" in the example. • The passwords cannot be used without being decrypted. • You may assume that the encrypted form of a password will NOT be an empty string. The programmer has started to define program modules as follows: Module Description • Takes two parameters: ○ a string ○ a character Exists() • Performs a case-sensitive search for the character in the string • Returns TRUE if the character occurs in the string, otherwise returns FALSE • Takes a password as a parameter of type string Encrypt() • Returns the encrypted form of the password as a string • Takes an encrypted password as a parameter of type string Decrypt() • Returns the decrypted form of the password as a string Note: in a case-sensitive comparison, 'a' is not the same as 'A'. (a) Write pseudocode for the module Exists(). ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ............................................................................................................................................. [5] (b) A new module SearchDuplicates() will: • search for the first password that occurs more than once in the array and output a message each time a duplicate is found. For example, if the same password was used for the three websites ThisWebsite.com, website27.net and websiteZ99.org, then the following messages will be output: "Password for ThisWebsite.com also used for website27.net" "Password for ThisWebsite.com also used for websiteZ99.org" • end once all messages have been output. The module will output a message if no duplicates are found. For example: "No duplicate passwords found" Write efficient pseudocode for the module SearchDuplicates(). Encrypt() and Decrypt() functions have been written. Note: It is necessary to decrypt each password before checking its value. ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ................................................................................................................................................... ...................................................................................................................................................
Mark scheme: 8(a) FUNCTION Exists(ThisString : STRING, Search : CHAR) 5 RETURNS BOOLEAN DECLARE Found : BOOLEAN DECLARE Index : INTEGER Found FALSE Index 1 WHILE Found = FALSE AND Index <= LENGTH(ThisString) IF MID(ThisString, Index, 1) = Search THEN Found TRUE ELSE Index Index + 1 ENDIF ENDWHILE RETURN Found ENDFUNCTION Marks as follows (Conditional loop solution): 1 Conditional loop while character not found and not end of string 2 Extract a char in a loop 3 Compare with parameter without case conversion in a loop 4 If match found, set termination logic in a loop 5 Return BOOLEAN value ALTERNATIVE (Using Count-controlled loop): FOR Index 1 TO LENGTH(ThisString) IF MID(ThisString, Index, 1) = Search THEN RETURN TRUE ENDIF NEXT Index RETURN FALSE Marks as follows (Count-controlled loop variant): 1 Loop for length of ThisString (allow from 0 or 1) 2 Extract a char in a loop 3 Compare with parameter without case conversion in a loop 4 If match found, immediate RETURN of TRUE 5 Return FALSE after the loop // Return Boolean if no immediate RETURN 8(b) PROCEDURE SearchDuplicates() 8 DECLARE IndexA, IndexB : INTEGER DECLARE ThisPassword, ThisValue : STRING DECLARE Duplicates : BOOLEAN Duplicates FALSE IndexA 1 WHILE Duplicates = FALSE AND IndexA < 500 ThisValue Secret[IndexA, 2] IF ThisValue <> "" THEN ThisPassword Decrypt(ThisValue) FOR IndexB IndexA + 1 TO 500 // IF Secret[IndexB, 2] <> "" THEN IF Decrypt(Secret[IndexB, 2]) = ThisPassword THEN OUTPUT "Password for " & Secret[IndexA, 1] & "also used for " & Secret[IndexB, 1] Duplicates TRUE ENDIF ENDIF NEXT IndexB ENDIF IndexA IndexA + 1 ENDWHILE IF Duplicates = FALSE THEN OUTPUT "No duplicate passwords found" ENDIF ENDPROCEDURE Marks as follows to Max 8: 1. (Any) conditional loop... 2. ... from 1 to 499 while (attempt at) no duplicate 3. Skip unused password 4. Use Decrypt() and assign return value to ThisPassword 5. Inner loop from outer loop index + 1 to 500 searching for duplicates 6. Compare ThisPassword with subsequent passwords (after use of Decrypt()) 7. If match found, set outer loop termination 8. and attempt an Output message giving duplicate 9. Output 'No duplicate passwords found' message if no duplicates found after the loop 8(c) One mark for each point that is referenced: 6 1 Initialise password to empty string at the start and return (attempted) password at the end of the function 2 Two loops to generate 3 groups of 4 characters // One loop to generate 12 / 14 characters 3 Use of RandomChar()to generate a character in a loop 4 Reject character if Exists()returns TRUE, otherwise form string in a loop 5 (Attempt to) use hyphens to link three groups 6 Three groups of four characters generated correctly with hyphens and without duplication (completely working algorithm)
What was in this paper
The subtopics covered by these 8 questions, and how many questions each got. Open one in a new tab to see every Cambridge question on it.
What you needed in this session
Cambridge’s own grade thresholds for 2022 May/June, Paper 2 · Variant 2. A higher threshold means an easier paper — the bar moves with how the cohort did.