Developing Algorithms MCQ Practice

Question 1 of 10

Algorithm M: IF (x > y) { max ← x } ELSE { max ← y }. Algorithm N: max ← x IF (y > max) { max ← y }. For x=4,y=9 which is true?

  1. A. Both set max to 9.
  2. B. Neither sets max to 9.
  3. C. Only M sets max to 9.
  4. D. Only N sets max to 9.

Question 2 of 10

List nums is [2,7,7,3]. Count matches to target 7:

count ← 0
FOR EACH value IN nums
{
  IF (value = target)
  {
    count ← count + 1
  }
}
DISPLAY (count)

Displayed when target is 7?

  1. A. 1
  2. B. 2
  3. C. 3
  4. D. 7

Question 3 of 10

Goal: order three-element list ascending by adjacent swaps. After swap of indexes 1 and 2 on [5,1,4] the list is [1,5,4]. Missing next step?

  1. A. Remove the middle element.
  2. B. Swap index 1 with index 3.
  3. C. Swap index 2 with index 3.
  4. D. Swap index 1 with index 2 again.

Question 4 of 10

A sums list values. B multiplies a product starting at 1 by each value. For [2,3,4]?

  1. A. A produces 24 and B produces 9.
  2. B. Both produce 24.
  3. C. Both produce 9.
  4. D. A produces 9 and B produces 24.

Question 5 of 10

Robot R faces east. Reach G. W = wall.

Row C1 C2 C3
R1 R G
R2 W W W

Which algorithm reaches G without a wall hit?

  1. A. REPEAT 2 TIMES { MOVE_FORWARD }
  2. B. ROTATE_RIGHT REPEAT 2 TIMES { MOVE_FORWARD }
  3. C. ROTATE_LEFT MOVE_FORWARD
  4. D. REPEAT 3 TIMES { MOVE_FORWARD }

Question 6 of 10

Correct first step to find the maximum in a nonempty list values?

  1. A. max ← 0 for every list, including all-negative lists.
  2. B. max ← values[1], then compare each later element to max.
  3. C. max ← LENGTH(values).
  4. D. max ← values[LENGTH(values)] without checking earlier elements.

Question 7 of 10

List [3,8,2]. P displays first value > 5. Q displays the maximum. Outputs?

  1. A. P: 2, Q: 8
  2. B. P: 3, Q: 8
  3. C. P: 8, Q: 8
  4. D. P: 8, Q: 3

Question 8 of 10

Missing line so count ends as number of matches:

count ← 0
i ← 1
REPEAT LENGTH(data) TIMES
{
  IF (data[i] = target)
  {
    __________________
  }
  i ← i + 1
}
  1. A. count ← target
  2. B. REMOVE(data, i)
  3. C. count ← i
  4. D. count ← count + 1

Question 9 of 10

List [9,4,6]. One left-to-right pass swaps each adjacent out-of-order pair (indexes 1-2 then 2-3 on the updated list). List after that pass?

  1. A. [4, 6, 9]
  2. B. [9, 6, 4]
  3. C. [6, 9, 4]
  4. D. [4, 9, 6]

Question 10 of 10

Best statement about developing algorithms from existing ones?

  1. A. A correct max pattern cannot help write a min pattern.
  2. B. Reusing a known correct counting pattern can reduce new testing when counting a different target.
  3. C. Two algorithms with the same loop must always produce the same output.
  4. D. Robots cannot reuse MOVE_FORWARD inside larger path algorithms.