Paper 2 Validation Testing Drills

These are original Paper 2-style practice questions. They use exact function names, inputs, and expected output evidence.

Detailed answers are in Paper 2 Validation Testing Answers.

Revise the topic hub first:

Questions

Question 1: Range Validation

Write valid_mark(mark) that returns True if mark is from 0 to 100 inclusive.

Test:

print(valid_mark(-1))
print(valid_mark(0))
print(valid_mark(100))
print(valid_mark(101))

Expected output:

False
True
True
False

[5]

Question 2: Presence Validation

Write present(text) that returns True only if text contains at least one non-space character.

Test:

print(present("Amy"))
print(present(""))
print(present("   "))

Expected output:

True
False
False

[4]

Question 3: Format Validation

Write valid_code(code) for a membership code with exactly two uppercase letters followed by four digits.

Test:

print(valid_code("AB1234"))
print(valid_code("A12345"))
print(valid_code("Ab1234"))
print(valid_code("CD12E4"))

Expected output:

True
False
False
False

[8]

Question 4: Length Check

Write valid_password_length(password) that returns True when the password length is from 8 to 12 inclusive.

Test:

print(valid_password_length("abc1234"))
print(valid_password_length("abc12345"))
print(valid_password_length("abcdefghijkl"))
print(valid_password_length("abcdefghijklm"))

Expected output:

False
True
True
False

[4]

Question 5: Check Digit

A four-digit code is valid if:

final digit = (sum of first three digits) modulo 10

Write valid_check_digit(code) that returns False if the code is not exactly four digits.

Test:

print(valid_check_digit("2462"))
print(valid_check_digit("2463"))
print(valid_check_digit("24A2"))

Expected output:

True
False
False

[10]

Question 6: Test Cases

Write valid_age(age), then write run_age_tests() where valid ages are 13 to 18 inclusive. run_age_tests() should return a list of (age, result) pairs for:

[12, 13, 15, 18, 19]

Expected output:

[(12, False), (13, True), (15, True), (18, True), (19, False)]

[6]

Question 7: Debug Runtime Error

This original operation may crash for blank input:

mark = int(text)

Write safe_int(text) that returns the integer value for valid integer text, and returns None for blank or non-integer text.

Test:

print(safe_int("42"))
print(safe_int(""))
print(safe_int("   "))
print(safe_int("abc"))

Expected output:

42
None
None
None

[6]

Question 8: Debug Logic Error

This function is meant to pass marks of 50 or more, but wrongly fails 50:

def passed(mark):
    return mark > 50

Write the corrected function and test 49, 50, and 51.

Expected output:

False
True
True

[5]

Question 9: Assertions

Reuse valid_mark(mark) from Question 1; you do not need to redefine it. Write assert_valid_mark_tests() that uses assertions to test valid_mark(mark) for -1, 0, 100, and 101. If all assertions pass, return "ALL TESTS PASSED".

Expected output:

ALL TESTS PASSED

[4]

Question 10: Integrated Validation Loop

Write parse_mark(text) that returns an integer mark if text represents an integer from 0 to 100 inclusive, and returns None otherwise.

Then write read_valid_mark() with no parameters. It should repeatedly ask the user to enter a mark until parse_mark(text) returns a valid integer. For each invalid input, display a concise error message. When a valid mark is entered, return the accepted integer.

Test parse_mark(text) using:

print(parse_mark(""))
print(parse_mark("abc"))
print(parse_mark("-1"))
print(parse_mark("0"))
print(parse_mark("100"))
print(parse_mark("101"))

Expected output:

None
None
None
0
100
None

[8]

Review Checklist

After attempting these questions, check whether you can:

  • implement exact validation rules;
  • test normal, abnormal, and extreme cases;
  • prevent runtime crashes from invalid conversion;
  • correct boundary logic errors;
  • use assertions and validation-helper tests as evidence.