Developing Algorithms (College Board AP® Computer Science Principles): Exam Questions

15 mins15 questions
1
1 mark

Which of the following Boolean expressions is logically equivalent to NOT (a AND b)?

  • (NOT a) OR (NOT b)

  • (NOT a) AND (NOT b)

  • a OR b

  • NOT a AND b

2
1 mark

A program searches a large sorted list for a target value. Which two changes would make the search more efficient? Select two answers.

  • Stop (exit the loop) as soon as the target is found

  • Use a binary search instead of a linear search

  • Add comments explaining each step

  • Rename the loop variable to be more descriptive

3
1 mark

Two students write different algorithms to find the largest value in a list. Algorithm A scans the list once, tracking the largest value seen so far. Algorithm B first sorts the list in descending order and then returns the first element. Which statement is correct?

  • Both produce the correct result, but A generally does less work

  • Only Algorithm B produces the correct result

  • The two algorithms can produce different maximum values for the same list

  • Algorithm A only works if the list is already sorted

4
1 mark

Consider the following code segment.

IF(score < 50)
{
   failed ← true
}
ELSE
{
   failed ← false
}

Which single assignment is logically equivalent to this code segment?

  • failed ← (score < 50)

  • failed ← (score > 50)

  • failed ← (score ≥ 50)

  • failed ← (score = 50)

5
1 mark

Which Boolean expression is logically equivalent to NOT (p OR q)?

  • (NOT p) OR (NOT q)

  • (NOT p) AND (NOT q)

  • p AND q

  • NOT p OR q

6
1 mark

Consider the following code segment.

IF(x > 0)
{
   IF(x < 10)
   {
      DISPLAY("yes")
   }
}

Which single Boolean expression, used in one IF statement, displays "yes" for exactly the same values of x?

  • (x > 0) OR (x < 10)

  • (x > 0) OR (x ≥ 10)

  • (x > 0) AND (x < 10)

  • NOT (x > 0)

7
1 mark

Two algorithms determine whether a list contains a target value. Algorithm A returns true as soon as it finds the first element equal to the target and then stops. Algorithm B counts every element equal to the target and then returns true if the count is greater than 0. Which statement is correct?

  • Only Algorithm A can return the correct result

  • Both return the correct result, but Algorithm A generally does less work

  • The two algorithms can disagree on whether the value is present

  • Algorithm B is always faster because it uses a counter

8
1 mark

Consider the two code segments below, where numbers is a list of values.

Segment 1:

sum ← 0
FOR EACH n IN numbers
{
   sum ← sum + n
}

Segment 2:

sum ← 0
FOR EACH n IN numbers
{
   sum ← n
}

Which statement about these segments is correct?

  • Both segments compute the total of all the numbers

  • Both segments always store the same value in sum

  • Segment 2 computes the total; Segment 1 stores only the last value

  • Segment 1 computes the total; Segment 2 stores only the last value

9
1 mark

A programmer needs to compute the average of only the even numbers in a list. Which combination of common algorithms accomplishes this?

  • A loop to iterate through the list, a divisibility check to keep even numbers, a running sum and count, then a division

  • A binary search followed by a sort of the list

  • A single division of the last element by the length of the list

  • A loop that displays each element without performing any calculation

10
1 mark

A linear search normally returns the position of the first element that equals a target. A programmer instead needs an algorithm that returns how many elements in the list equal the target. What is the most efficient way to obtain this?

  • Sort the list and then run a binary search

  • Modify the existing linear search so that, instead of stopping at the first match, it increments a counter for every match

  • Write a completely new algorithm from scratch that uses recursion

  • Run the linear search once for every element in the list

11
1 mark

Which of the following is a benefit of building a new program from existing, already-tested algorithms rather than writing every part from scratch?

  • It guarantees the program will run faster on every possible input

  • It removes any need to test the finished program

  • It reduces development time and makes errors easier to identify

  • It increases the total amount of new code that must be written

12
1 mark

Consider the following code segment, which searches the list values for the target t.

found ← false
i ← 1
REPEAT UNTIL(found = true OR i > LENGTH(values))
{
   IF(values[i] = t)
   {
      found ← true
   }
   i ← i + 1
}

The list is values ← [4, 9, 15, 22] and t ← 9. How many times is the condition values[i] = t evaluated before the loop stops?

  • 1

  • 4

  • 3

  • 2

13
1 mark

Two algorithms each decide whether a sorted list of 1,000 items contains a particular value. Algorithm A checks each item from the start until the value is found or the list ends. Algorithm B repeatedly halves the search range. Which statement best describes their efficiency on large sorted lists?

  • Algorithm B usually makes far fewer comparisons because it halves the range at each step

  • Algorithm A is more efficient because it never has to sort the list

  • Both algorithms always make exactly the same number of comparisons

  • Efficiency is irrelevant because both algorithms return the correct answer

14
1 mark

A programmer replaces Segment 1 with Segment 2, where data is a list of numbers.

Segment 1:

FOR EACH x IN data
{
   DISPLAY(x / LENGTH(data))
}

Segment 2:

n ← LENGTH(data)
FOR EACH x IN data
{
   DISPLAY(x / n)
}

Which statement about the two segments is correct?

  • Segment 2 produces different output from Segment 1

  • Segment 1 is more efficient than Segment 2

  • Both produce the same output, and Segment 2 avoids recomputing the length on every iteration

  • Segment 2 will not run because a stored value cannot be reused in a loop

15
1 mark

Consider two algorithms that both compute the sum of a list of numbers.

Algorithm A:

total ← 0
FOR EACH v IN nums
{
   total ← total + v
}

Algorithm B:

total ← 0
i ← 1
REPEAT UNTIL(i > LENGTH(nums))
{
   total ← total + nums[i]
   i ← i + 1
}

For the list nums ← [3, 7, 2], which statement is correct?

  • Only Algorithm A gives the correct sum of 12

  • The two algorithms give different sums

  • Algorithm B gives 12 but Algorithm A gives 0

  • Both algorithms give the same result, 12