Paper 2 Data Representation Drills
These are original Paper 2-style practice questions. They use exact function names, input values, and expected output evidence.
Detailed answers are in Paper 2 Data Representation Answers.
Revise the topic hub first:
Questions
Question 1: Denary to Binary Function
Write denary_to_binary(n) that converts a non-negative denary integer to a binary string without using bin().
Test:
print(denary_to_binary(0))
print(denary_to_binary(13))
print(denary_to_binary(45))Expected output:
0
1101
101101[8]
Question 2: Binary to Denary Function
Write binary_to_denary(bits) that converts a binary string to denary without using int(bits, 2).
Test:
print(binary_to_denary("0"))
print(binary_to_denary("1101"))
print(binary_to_denary("10101100"))Expected output:
0
13
172[7]
Question 3: Denary to Hex Function
Write denary_to_hex(n) that converts a non-negative denary integer to uppercase hexadecimal without using hex().
Test:
print(denary_to_hex(0))
print(denary_to_hex(58))
print(denary_to_hex(214))Expected output:
0
3A
D6[9]
Question 4: Hex to Denary Function
Write hex_to_denary(hex_text) that converts uppercase hexadecimal to denary without using int(hex_text, 16).
Test:
print(hex_to_denary("0"))
print(hex_to_denary("3A"))
print(hex_to_denary("D6"))Expected output:
0
58
214[8]
Question 5: Validate Binary
Write valid_binary(bits) that returns True only if bits is non-empty and contains only 0 and 1.
Test:
print(valid_binary("1010"))
print(valid_binary(""))
print(valid_binary("1021"))Expected output:
True
False
False[5]
Question 6: Validate Hex
Write valid_hex(hex_text) that returns True only if hex_text is non-empty and every character is a valid hexadecimal digit. Lowercase letters should be accepted.
Test:
print(valid_hex("3A"))
print(valid_hex("ff"))
print(valid_hex(""))
print(valid_hex("G1"))Expected output:
True
True
False
False[6]
Question 7: ASCII Codes
Write char_codes(text) that returns a list of (character, code) pairs using ord().
Test:
print(char_codes("Az0"))Expected output:
[('A', 65), ('z', 122), ('0', 48)][5]
Question 8: Unicode Characters
Write unicode_summary(text) that returns (len(text), len(text.encode("utf-8"))).
Test:
print(unicode_summary("A€你"))Expected output:
(3, 7)[4]
Question 9: File Conversion
A text file numbers.txt contains:
5
13
45Write convert_file(filename) that reads denary numbers and returns a list of "denary -> binary" strings.
Test:
print(convert_file("numbers.txt"))Expected output:
['5 -> 101', '13 -> 1101', '45 -> 101101'][8]
Question 10: Menu Converter
Write convert_choice(choice, value) where:
- choice
"1"converts binary to denary; - choice
"2"converts denary to binary; - any other choice returns
"INVALID CHOICE".
Use your own conversion functions, not Python base-conversion shortcuts.
Test:
print(convert_choice("1", "1101"))
print(convert_choice("2", "13"))
print(convert_choice("9", "13"))Expected output:
13
1101
INVALID CHOICE[10]
Review Checklist
After attempting these questions, check whether you can:
- implement base conversion from the algorithm, not just from built-in shortcuts;
- handle zero as a special case;
- validate binary and hexadecimal strings;
- use character-code functions for ASCII/Unicode evidence;
- show exact output for file and menu-style tasks.