Paper 2 Data Representation Answers

These answers correspond to Paper 2 Data Representation Drills.

Verification note: every Python code block in this answer file has been executed locally.

Answer 1: Denary to Binary Function

Model answer:

def denary_to_binary(n):
    if n == 0:
        return "0"
 
    digits = ""
    while n > 0:
        remainder = n % 2
        digits = str(remainder) + digits
        n = n // 2
    return digits
 
 
print(denary_to_binary(0))
print(denary_to_binary(13))
print(denary_to_binary(45))

Expected output:

0
1101
101101

Mark points:

  • handles zero;
  • loops while the number is greater than zero;
  • calculates remainder modulo 2;
  • stores each remainder as a binary digit;
  • builds digits in reverse or reverses at the end;
  • updates the quotient using integer division by 2;
  • stops when quotient becomes zero;
  • returns the expected strings.

Common weak answer:

  • returning the remainders in the order they are generated, which reverses the binary value.

Answer 2: Binary to Denary Function

Model answer:

def binary_to_denary(bits):
    total = 0
    for bit in bits:
        total = total * 2 + int(bit)
    return total
 
 
print(binary_to_denary("0"))
print(binary_to_denary("1101"))
print(binary_to_denary("10101100"))

Expected output:

0
13
172

Mark points:

  • initializes total to zero;
  • loops through bits from left to right;
  • multiplies the running total by 2;
  • converts each bit to an integer;
  • adds the bit value;
  • handles "0";
  • produces all expected outputs.

Common weak answer:

  • adding the digit values only, so 1011 would incorrectly become 3.

Answer 3: Denary to Hex Function

Model answer:

def denary_to_hex(n):
    digits = "0123456789ABCDEF"
    if n == 0:
        return "0"
 
    result = ""
    while n > 0:
        remainder = n % 16
        result = digits[remainder] + result
        n = n // 16
    return result
 
 
print(denary_to_hex(0))
print(denary_to_hex(58))
print(denary_to_hex(214))

Expected output:

0
3A
D6

Mark points:

  • handles zero;
  • uses digit symbols 0 to F;
  • divides by 16 repeatedly;
  • uses remainder modulo 16;
  • maps remainders 10 to 15 to A to F;
  • builds digits in the correct order;
  • updates the quotient;
  • stops correctly;
  • produces all expected outputs.

Common weak answer:

  • returning decimal 10 as two characters instead of hexadecimal digit A.

Answer 4: Hex to Denary Function

Model answer:

def hex_to_denary(hex_text):
    digits = "0123456789ABCDEF"
    total = 0
    for ch in hex_text:
        value = digits.index(ch)
        total = total * 16 + value
    return total
 
 
print(hex_to_denary("0"))
print(hex_to_denary("3A"))
print(hex_to_denary("D6"))

Expected output:

0
58
214

Mark points:

  • maps each hex digit to a value from 0 to 15;
  • scans digits from left to right;
  • multiplies running total by 16;
  • adds current digit value;
  • handles A correctly as 10;
  • handles D correctly as 13;
  • returns integer results;
  • produces all expected outputs.

Common weak answer:

  • treating 3A as 3 + 10 = 13 instead of 3 * 16 + 10 = 58.

Answer 5: Validate Binary

Model answer:

def valid_binary(bits):
    if bits == "":
        return False
    for bit in bits:
        if bit != "0" and bit != "1":
            return False
    return True
 
 
print(valid_binary("1010"))
print(valid_binary(""))
print(valid_binary("1021"))

Expected output:

True
False
False

Mark points:

  • rejects empty string;
  • loops through each character;
  • accepts 0;
  • accepts 1;
  • rejects any other character.

Common weak answer:

  • checking whether the string is numeric, which would wrongly accept 1021.

Answer 6: Validate Hex

Model answer:

def valid_hex(hex_text):
    if hex_text == "":
        return False
 
    allowed = "0123456789ABCDEF"
    for ch in hex_text.upper():
        if ch not in allowed:
            return False
    return True
 
 
print(valid_hex("3A"))
print(valid_hex("ff"))
print(valid_hex(""))
print(valid_hex("G1"))

Expected output:

True
True
False
False

Mark points:

  • rejects empty string;
  • defines the allowed hexadecimal characters;
  • converts lowercase to uppercase or otherwise accepts lowercase;
  • loops through every character;
  • rejects characters outside 0 to 9 and A to F;
  • produces all expected outputs.

Common weak answer:

  • rejecting lowercase ff when the question says lowercase should be accepted.

Answer 7: ASCII Codes

Model answer:

def char_codes(text):
    pairs = []
    for ch in text:
        pairs.append((ch, ord(ch)))
    return pairs
 
 
print(char_codes("Az0"))

Expected output:

[('A', 65), ('z', 122), ('0', 48)]

Mark points:

  • loops through each character;
  • uses ord;
  • stores character and code as a pair;
  • preserves order;
  • returns the expected list.

Common weak answer:

  • using int(ch), which only works for digit characters and does not give the character code.

Answer 8: Unicode Characters

Model answer:

def unicode_summary(text):
    return (len(text), len(text.encode("utf-8")))
 
 
print(unicode_summary("A€你"))

Expected output:

(3, 7)

Mark points:

  • counts characters using len(text);
  • encodes as UTF-8 bytes;
  • counts encoded bytes;
  • returns (3, 7) for the given string.

Common weak answer:

  • assuming every character uses one byte. In UTF-8, A uses 1 byte, uses 3 bytes, and uses 3 bytes.

Answer 9: File Conversion

Model answer:

def denary_to_binary(n):
    if n == 0:
        return "0"
 
    digits = ""
    while n > 0:
        remainder = n % 2
        digits = str(remainder) + digits
        n = n // 2
    return digits
 
 
def convert_file(filename):
    results = []
    with open(filename, "r", encoding="utf-8") as f:
        for line in f:
            number = int(line.strip())
            results.append(str(number) + " -> " + denary_to_binary(number))
    return results
 
 
with open("numbers.txt", "w", encoding="utf-8") as f:
    f.write("5\n13\n45\n")
 
print(convert_file("numbers.txt"))

Expected output:

['5 -> 101', '13 -> 1101', '45 -> 101101']

Mark points:

  • opens the file;
  • loops through all lines;
  • strips whitespace;
  • converts each line to an integer;
  • calls the conversion function;
  • formats each result as requested;
  • appends each result to a list;
  • returns the expected list.

Common weak answer:

  • reading only the first line of the file.

Answer 10: Menu Converter

Model answer:

def denary_to_binary(n):
    n = int(n)
    if n == 0:
        return "0"
 
    digits = ""
    while n > 0:
        remainder = n % 2
        digits = str(remainder) + digits
        n = n // 2
    return digits
 
 
def binary_to_denary(bits):
    total = 0
    for bit in bits:
        total = total * 2 + int(bit)
    return str(total)
 
 
def convert_choice(choice, value):
    if choice == "1":
        return binary_to_denary(value)
    if choice == "2":
        return denary_to_binary(value)
    return "INVALID CHOICE"
 
 
print(convert_choice("1", "1101"))
print(convert_choice("2", "13"))
print(convert_choice("9", "13"))

Expected output:

13
1101
INVALID CHOICE

Mark points:

  • defines or calls a binary-to-denary conversion;
  • defines or calls a denary-to-binary conversion;
  • checks choice "1";
  • checks choice "2";
  • returns or prints the selected conversion result;
  • handles invalid choice;
  • converts value types where needed;
  • does not use Python base-conversion shortcuts;
  • produces the three expected outputs;
  • keeps the menu choice logic clear.

Common weak answer:

  • converting both choices in the same direction.