02 — Practice solutions

← Questions

These are independently written explanations, not an official marking scheme.

02A

1×256 + 12×16 + 2 = 450. Twelve bits: 0001 1100 0010. Hex is more compact and easier to transcribe, with a direct four-bit mapping per digit.

02B

77 = 64+8+4+1 → 01001101. U+1F631 = 1×65536 + 15×4096 + 6×256 + 3×16 + 1 = 128561. Unicode supports many writing systems and symbols such as emoji that ASCII cannot represent. Its byte representation depends on the encoding; UTF-8 is variable-length, so the universal two-byte claim is false.

02C

def hex_to_binary(text):
    digits = "0123456789ABCDEF"
    groups = ["0000", "0001", "0010", "0011",
              "0100", "0101", "0110", "0111",
              "1000", "1001", "1010", "1011",
              "1100", "1101", "1110", "1111"]
    result = ""
    for digit in text:
        result += groups[digits.index(digit)]
    return result

"0A" → "00001010"; "F0" → "11110000". Leading zeroes preserve the specified four-bit group for each digit. The contract supplies valid uppercase hexadecimal input.