Paper 2 Algorithmic Representation Drills

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

Detailed answers are in Paper 2 Algorithmic Representation Answers.

Revise the topic hub first:

Questions

Question 1: Pseudocode to Python

Convert this pseudocode into a Python function grade(score).

IF score >= 70 THEN
    result <- "A"
ELSE IF score >= 50 THEN
    result <- "Pass"
ELSE
    result <- "Fail"
ENDIF
RETURN result

Test:

print(grade(82))
print(grade(55))
print(grade(40))

Expected output:

A
Pass
Fail

[6]

Question 2: Sentinel Loop

Write a function sum_until_stop(values) that represents this sentinel-controlled algorithm:

  • read values from the list in order;
  • stop when the value is -1;
  • return the sum of values before -1;
  • ignore any values after -1.

Test:

print(sum_until_stop([4, 7, -1, 100]))

Expected output:

11

[6]

Question 3: Decision Table Function

A user may access a resource only when:

logged_in AND paid_fee AND not_suspended

Write a function access_action(logged_in, paid_fee, not_suspended) that returns "ALLOW" or "DENY".

Test:

print(access_action(True, True, True))
print(access_action(True, True, False))
print(access_action(False, True, True))

Expected output:

ALLOW
DENY
DENY

[8]

Question 4: Trace by Print

Write a function trace_total() that follows this algorithm and prints the state after each iteration:

count <- 1
total <- 0
WHILE count <= 3
    total <- total + count
    OUTPUT count, total
    count <- count + 1
ENDWHILE

Expected output:

1 1
2 3
3 6

[4]

Question 5: Modular Program

Write three functions:

  • get_marks() returns [60, 75, 45];
  • calculate_average(marks) returns the average mark;
  • display_average(average) returns "Average: 60.0" for the test data.

Then write a short main program that calls the functions in sequence and prints the display string.

Expected output:

Average: 60.0

[8]

Question 6: Flowchart Implementation

Implement the following flowchart description as a function count_even(numbers):

Start
count <- 0
FOR each number in numbers
    Decision: number MOD 2 = 0?
    Yes: count <- count + 1
    No: do not change count
NEXT number
Output count
End

Test:

print(count_even([2, 5, 8, 9, 10]))

Expected output:

3

[7]

Question 7: Validation Function

Write and test a function valid_mark(mark) that returns True only when mark is an integer between 0 and 100 inclusive.

Test:

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

Expected output:

False
True
True
False
False

[5]

Question 8: Pseudocode Bug Fix

This flawed condition rejects valid boundary marks:

IF mark > 0 AND mark < 100 THEN
    valid <- True
ELSE
    valid <- False
ENDIF

Write a corrected Python function fixed_valid_mark(mark) and test it with 0, 50, and 100.

Expected output:

True
True
True

[6]

Question 9: Decision Table Tests

Reuse access_action(logged_in, paid_fee, not_suspended) from Question 3.

Write a function decision_table_tests() that returns a list of result strings for these eight cases:

[
    (True, True, True),
    (True, True, False),
    (True, False, True),
    (True, False, False),
    (False, True, True),
    (False, True, False),
    (False, False, True),
    (False, False, False)
]

Expected output:

['ALLOW', 'DENY', 'DENY', 'DENY', 'DENY', 'DENY', 'DENY', 'DENY']

[5]

Question 10: Program Skeleton

Create a Python skeleton for a quiz program with these functions:

  • load_questions()
  • ask_questions(questions)
  • display_score(score)
  • main()

For this skeleton, use placeholders so that running main() prints:

Score: 0

[4]

Review Checklist

After attempting these questions, check whether you can:

  • translate pseudocode selection and loops into Python;
  • implement a decision-table rule as Boolean logic;
  • print trace evidence without changing the algorithm;
  • split code into named modules with clear responsibilities;
  • test boundary cases and all major actions.