GCE O-Level · Computing (7155)

Algorithm Design: Practice Questions

4 multiple-choice questions marked as you go, and 5 written questions with worked solutions. All on Algorithm Design.

9 questions26 marksFree, no account
Question 1
1 mark

A programmer is dry-running an algorithm to find the maximum absolute difference between any two adjacent elements in the list \(L = [15, 22, 18, 30, 25]\).

\(max\_diff = 0\)
\(i = 0\)
while \(i < 4\):
   \(diff = L[i+1] - L[i]\)
   if \(diff < 0\):
      \(diff = -diff\)
   if \(diff > max\_diff\):
      \(max\_diff = diff\)
   \(i = i + 1\)

What is the final value of \(max\_diff\) recorded in the trace table after the loop terminates?

Question 2
1 mark

A student is writing an algorithm to calculate the average of all strictly positive numbers in a list L = [10, -5, 20, 0] without using built-in functions like sum().

total = 0
count = 0
for x in L:
  if x > 0:
    total += x
    count += 1
avg = total / count

What is the final value of avg?

Question 3
1 mark

A programmer needs to find the minimum value in a list L = [7, 3, 9, 2, 5] without using the built-in min() function. Which of the following code logic segments correctly identifies the minimum value?

Question 4
1 mark

A student is manually tracing an algorithm to count elements in a list that meet specific criteria. The algorithm is as follows:
1. Initialize \(count = 0\)
2. For each number \(n\) in the list \(L = [9, 15, 24, 33, 40, 45]\):
   a. If \(n > 10\) and \(n \% 3 == 0\):
      i. If \(n \% 2 != 0\):
         - \(count = count + 1\)

What is the final value of \(count\) after the algorithm has processed the entire list?

Question 5
3 marks

A string \(S = \text{"banana"}\) is provided. Explain the logic an algorithm would use to determine the total number of times the character \(\text{"a"}\) appears in \(S\) without using the built-in \(\text{count()}\) or \(\text{find()}\) methods.

Write your answer out first, then check it against the worked solution.

Question 6
5 marks

A student manually traces an algorithm designed to find the maximum value in the list \(L = [3, 8, 10, 6, 12, 15, 7]\). The variable max_val is initialized to the first element of the list, and then the algorithm iterates through the remaining elements, updating max_val whenever a larger number is encountered. How many times is the assignment statement for max_val executed in total, including the initialization step?

Write your answer out first, then check it against the worked solution.

Question 7
3 marks

A programmer uses the Euclidean distance formula \(d = \sqrt{x^2 + y^2}\) within a Python function to calculate the distance of a point from the origin \((0, 0)\). If the point is located at coordinates \((6, 8)\), what is the final value of \(d\)?

Write your answer out first, then check it against the worked solution.

Question 8
4 marks

A programmer needs to determine how many times a specific character \(c\) appears in a given string \(S\).

Outline the steps of an algorithm to achieve this without using Python's built-in functions such as count() or find(). In your explanation, specify the initialization of any necessary variables, the type of loop to be used, and the logic for updating the count.

Write your answer out first, then check it against the worked solution.

Question 9
7 marks

Consider a list of integers \(A = [12, -5, 8, 0, -3, 10]\). An algorithm is needed to calculate the average value of all positive integers (numbers greater than \(0\)) within the list.

Describe the algorithmic steps required to calculate this average without using built-in Python functions like sum() or len(). Include logic to handle the scenario where the list contains no positive integers to prevent a run-time error.

Write your answer out first, then check it against the worked solution.

* The content provided by thinka is generated by AI and may not always be accurate or up-to-date. Please use it as a supplementary resource and verify with official materials.

You've seen the model answer. Now get yours marked.

This page can show you how a good answer looks. It cannot tell you what your answer was missing. thinka marks your written work against the real mark scheme in about 15 seconds.

Want more questions like these? Get a fresh set on this topic, marked as you go.

Practise More