8
0/5

A program encrypts a message using a Caesar-style alphabetic shift cipher.

The letters of the alphabet are shifted a set number of places. A positive shift moves the letters to the right (towards Z/z). A negative shift moves them to the left (towards A/a).

Spaces, punctuation, symbols, and numbers must not be encrypted.

When the end of the alphabet is reached with a shape shift, wrapping must occur so the alphabet acts as a loop. For example, a shift of +3 encodes 'z' to 'c', and a shift of -2 encodes 'a' to 'y'. Case is preserved throughout encryption.

The program must produce the encrypted ciphertext shown in the test table below:

Table 1: Caesar Cipher Test Cases

PlaintextShiftCiphertext
Hello World!3Khoor Zruog!
Zebra-99-2Xczpy-99
Code 12314Qcrs 123

Below is an incomplete Python implementation of this program. It contains five sections where you must choose between alternative lines of code.

def encrypt_message(plain_text, shift):
    cipher_text = ""
    for char in plain_text:
        # --- CHOICE 1 ---
        # Option A1: if char.isalpha():
        # Option A2: if char.isalnum():

            # --- CHOICE 2 ---
            # Option B1: start = ord('a') if char.islower() else ord('A')
            # Option B2: start = ord('z') if char.islower() else ord('Z')

            # --- CHOICE 3 ---
            # Option C1: position = ord(char) - start
            # Option C2: position = ord(char)

            # --- CHOICE 4 ---
            # Option D1: new_position = (position + shift) % 26
            # Option D2: new_position = (position + shift) % 52

            # --- CHOICE 5 ---
            # Option E1: cipher_text += chr(new_position + start)
            # Option E2: cipher_text += chr(new_position)
        else:
            cipher_text += char
    return cipher_text

Identify the correct option (1 or 2) for each of the five choices to make the program function correctly according to the specification.

[5]

Develop code Questions

Practise Edexcel GCSE Computer Science Develop code with exam-style questions for GCSE Computer Science. 57 questions covering Decomposition and abstraction to solve problems, Read, write, analyse and refine programs, Converting algorithms into programs, Techniques for readable, maintainable code, Identifying and correcting program errors, and Evaluating program fitness and efficiency, matched to the Edexcel GCSE Computer Science (1CP2) specification and written in Paper 1 and Paper 2 style. Every question includes a full worked solution and mark scheme, so you can see where marks are awarded rather than just whether you got the answer right.

PreviousNext

Develop code Questions

  1. GCSE
  2. /Computer Science
  3. /Develop code