Unit 3: Algorithms and Programming

Showing 68 of 68 questions

Q1
MULTIPLE_CHOICEEasy

What is the value of x after the following code segment is executed? x โ† 5 x โ† x + 3 x โ† x * 2

Q2
MULTIPLE_CHOICEEasy

What is the output of the following code segment? a โ† 10 b โ† 20 a โ† b DISPLAY(a)

Q3
MULTIPLE_CHOICEMedium

What are the values of a and b after the following code segment? a โ† 3 b โ† 7 temp โ† a a โ† b b โ† temp

Q4
MULTIPLE_CHOICEMedium

What is displayed by this code segment? nums โ† [4, 7, 2, 9, 1] max โ† nums[1] FOR EACH num IN nums { IF (num > max) { max โ† num } } DISPLAY(max)

Q5
MULTIPLE_CHOICEMedium

What is displayed after executing this code? result โ† 0 FOR i โ† 1 TO 5 { result โ† result + i } DISPLAY(result)

Q6
MULTIPLE_CHOICEHard

A list contains n elements. A linear search is used to find a specific value. What is the maximum number of comparisons needed?

Q7
MULTIPLE_CHOICEHard

A sorted list contains 1024 elements. Using a binary search, what is the maximum number of comparisons needed to find a specific value?

Q8
MULTIPLE_CHOICEMedium

Which of the following is a key requirement for binary search to work correctly?

Q9
MULTIPLE_CHOICEMedium

What is displayed by the following code? PROCEDURE double(x) { RETURN x * 2 } result โ† double(double(3)) DISPLAY(result)

Q10
MULTIPLE_CHOICEHard

Which algorithm has a reasonable (polynomial) running time?

Q11
MULTIPLE_CHOICEMedium

What is displayed by the following code? list โ† [1, 2, 3, 4, 5] FOR EACH item IN list { IF (item MOD 2 = 0) { DISPLAY(item) } }

Q12
MULTIPLE_CHOICEHard

What is the output of the following code? list โ† [3, 1, 4, 1, 5] n โ† LENGTH(list) FOR i โ† 1 TO n - 1 { IF (list[i] > list[i + 1]) { temp โ† list[i] list[i] โ† list[i + 1] list[i + 1] โ† temp } } DISPLAY(list)

Q13
MULTIPLE_CHOICEMedium

What is displayed by the following code? PROCEDURE mystery(a, b) { IF (a > b) { RETURN a } ELSE { RETURN b } } DISPLAY(mystery(mystery(3, 5), mystery(4, 2)))

Q14
MULTIPLE_CHOICEHard

An algorithm is designed to work on a list of n items. Which of the following running times would be considered "unreasonable" for large values of n?

Q15
MULTIPLE_CHOICEMedium

What is displayed after the following code? list โ† [10, 20, 30] APPEND(list, 40) REMOVE(list, 2) DISPLAY(list)

Q16
MULTIPLE_CHOICEHard

A robot is in a grid and needs to reach a goal. The robot can move forward, turn left, and turn right. Which of the following is NOT a valid approach to solve this problem?

Q17
MULTIPLE_CHOICEMedium

What does the following procedure compute? PROCEDURE mystery(n) { count โ† 0 REPEAT UNTIL (n = 0) { n โ† n / 2 (integer division) count โ† count + 1 } RETURN count }

Q18
MULTIPLE_CHOICEHard

A programmer writes two versions of a search algorithm. Version A takes 2n steps, and Version B takes nยฒ steps. For what values of n does Version A perform fewer steps?

Q19
MULTIPLE_CHOICEHard

What is displayed by the following code? list โ† [5, 3, 8, 1, 9, 2] result โ† list[1] FOR EACH item IN list { IF (item < result) { result โ† item } } DISPLAY(result)

Q20
MULTIPLE_CHOICEMedium

Which of the following is true about the efficiency of algorithms?

Q21
MULTIPLE_CHOICEEasy

What is a "heuristic" approach to solving a problem?

Q22
MULTIPLE_CHOICEHard

What is displayed by the following code? count โ† 0 FOR i โ† 1 TO 4 { FOR j โ† 1 TO 3 { count โ† count + 1 } } DISPLAY(count)

Q23
MULTIPLE_CHOICEMedium

What is a simulation?

Q24
MULTIPLE_CHOICEMedium

What is displayed after the following code? x โ† 10 IF (x > 5) { x โ† x - 3 } IF (x > 5) { x โ† x - 3 } DISPLAY(x)

Q25
MULTIPLE_CHOICEMedium

If a = true and b = false, what is the value of (a AND b) OR (NOT b)?

Q26
MULTIPLE_CHOICEMedium

What is a primary benefit of using procedures (functions) in programming?

Q27
MULTIPLE_CHOICEMedium

In the AP CSP pseudocode, what does list[3] refer to if list = [10, 20, 30, 40]?

Q28
MULTIPLE_CHOICEEasy

What is the value of 17 MOD 5?

Q29
MULTIPLE_CHOICEMedium

In the AP CSP robot pseudocode, a robot is at position (1,1) facing right on a 4ร—4 grid. After executing MOVE_FORWARD() twice and ROTATE_LEFT() once, the robot is:

Q30
MULTIPLE_CHOICEMedium

What is displayed?\nx โ† 15\nIF (x > 20) { DISPLAY("high") }\nELSE { IF (x > 10) { DISPLAY("medium") } ELSE { DISPLAY("low") } }

Q31
MULTIPLE_CHOICEMedium

What does this procedure return when called as mystery(5)?\nPROCEDURE mystery(n) { result โ† 0 REPEAT n TIMES { result โ† result + n } RETURN result }

Q32
MULTIPLE_CHOICEMedium

What is displayed after this code runs?\nlist โ† [3, 7, 1, 9, 4]\nREMOVE(list, 2)\nAPPEND(list, 5)\nDISPLAY(LENGTH(list))

Q33
MULTIPLE_CHOICEMedium

In AP CSP pseudocode, RANDOM(1, 6) generates:

Q34
MULTIPLE_CHOICEMedium

An algorithm that checks every element in a list of n items to find a target has a time complexity described as:

Q35
MULTIPLE_CHOICEMedium

Binary search requires that the data be:

Q36
MULTIPLE_CHOICEMedium

To swap the values of variables a and b, which sequence works correctly?

Q37
MULTIPLE_CHOICEHard

An undecidable problem is one for which:

Q38
MULTIPLE_CHOICEHard

A heuristic approach to problem-solving:

Q39
MULTIPLE_CHOICEMedium

Simulations are useful because they:

Q40
MULTIPLE_CHOICEMedium

What is displayed?\nnums โ† [2, 4, 6, 8]\nsum โ† 0\nFOR EACH num IN nums { sum โ† sum + num }\nDISPLAY(sum)

Q41
MULTIPLE_CHOICEEasy

What is displayed?\nfirst โ† "Hello"\nsecond โ† "World"\nDISPLAY(CONCAT(first, " ", second))

Q42
MULTIPLE_CHOICEHard

How many times is DISPLAY called?\nREPEAT 3 TIMES { REPEAT 4 TIMES { DISPLAY("*") } }

Q43
MULTIPLE_CHOICEHard

An algorithm that takes 2^n steps for an input of size n runs in:

Q44
MULTIPLE_CHOICEMedium

What is displayed?\nPROCEDURE triple(x) { RETURN x * 3 }\nresult โ† triple(triple(2))\nDISPLAY(result)

Q45
MULTIPLE_CHOICEEasy

Which best describes how computing enables citizen science?

Q46
MULTIPLE_CHOICEHard

Which code correctly finds the minimum value in a list?\nnums โ† [5, 3, 8, 1, 6]

Q47
MULTIPLE_CHOICEMedium

Abstraction in programming allows developers to:

Q48
MULTIPLE_CHOICEMedium

After the following code, what is list?\nlist โ† [1, 2, 3]\nINSERT(list, 2, 10)\nAPPEND(list, 20)

Q49
MULTIPLE_CHOICEEasy

After executing:\na โ† 5\nb โ† a\na โ† 10\nWhat is the value of b?

Q50
MULTIPLE_CHOICEMedium

What is displayed?\ncount โ† 0\nlist โ† [3, 7, 2, 8, 5, 1]\nFOR EACH item IN list { IF (item > 4) { count โ† count + 1 } }\nDISPLAY(count)

Q51
MULTIPLE_CHOICEHard

A robot starts at position (1,1) facing right in a 5ร—5 grid. What does this code do?\nREPEAT UNTIL (NOT CAN_MOVE(forward)) { MOVE_FORWARD() }

Q52
MULTIPLE_CHOICEEasy

What is displayed?\ni โ† 1\nREPEAT 4 TIMES { DISPLAY(i) i โ† i + 2 }

Q53
MULTIPLE_CHOICEHard

What does this code produce?\noriginal โ† [10, 25, 5, 30, 15]\nfiltered โ† []\nFOR EACH val IN original { IF (val โ‰ฅ 15) { APPEND(filtered, val) } }\nDISPLAY(filtered)

Q54
MULTIPLE_CHOICEHard

Which expression is logically equivalent to NOT (a AND b)?

Q55
MULTIPLE_CHOICEEasy

What is displayed?\nx โ† 1\nREPEAT 5 TIMES { x โ† x * 2 }\nDISPLAY(x)

Q56
MULTIPLE_CHOICEMedium

What is the difference between RETURN and DISPLAY in a procedure?

Q57
MULTIPLE_CHOICEHard

What is displayed?\nx โ† 64\ncount โ† 0\nREPEAT UNTIL (x < 2) { x โ† x / 2 count โ† count + 1 }\nDISPLAY(count)

Q58
MULTIPLE_CHOICEMedium

What is displayed?\nage โ† 16\nhasPermit โ† true\nIF (age โ‰ฅ 16 AND hasPermit) { DISPLAY("Can drive") }\nELSE { DISPLAY("Cannot drive") }

Q59
MULTIPLE_CHOICEMedium

Consider the following pseudocode: result โ† 0 FOR EACH item IN [3, 7, 2, 9, 4] { IF (item > 5) { result โ† result + item } } DISPLAY(result) What is displayed?

Q60
MULTIPLE_CHOICEHard

A programmer writes a procedure to search a sorted list: PROCEDURE search(list, target) { low โ† 1 high โ† LENGTH(list) REPEAT UNTIL (low > high) { mid โ† (low + high) / 2 IF (list[mid] = target) { RETURN mid } ELSE IF (list[mid] < target) { low โ† mid + 1 } ELSE { high โ† mid - 1 } } RETURN -1 } The list has 1024 elements. What is the maximum number of comparisons needed to find a target or determine it is not in the list?

Q61
MULTIPLE_CHOICEMedium

Consider the following pseudocode: x โ† 1 REPEAT 4 TIMES { x โ† x * 2 x โ† x + 1 } DISPLAY(x) What is displayed?

Q62
MULTIPLE_CHOICEHard

Two algorithms solve the same problem. Algorithm A runs in nยฒ steps and Algorithm B runs in n ร— logโ‚‚(n) steps, where n is the input size. For n = 8, Algorithm A takes 64 steps. For what approximate input size n would Algorithm A take 100 times longer than Algorithm B?

Q63
MULTIPLE_CHOICEHard

The following procedure is intended to return true if a list contains any duplicate values. \nPROCEDURE hasDuplicate(list) { FOR EACH item1 IN list { FOR EACH item2 IN list { IF (item1 = item2) { RETURN true } } } RETURN false } \nWhat is wrong with this procedure?

Q64
MULTIPLE_CHOICEHard

A list of 1000 names is sorted alphabetically. Using binary search, what is the maximum number of comparisons needed to determine that a name is NOT in the list?

Q65
MULTIPLE_CHOICEMedium

Consider the following code segment. \nresult โ† 0\nFOR EACH num IN [3, 7, 2, 8, 5] { IF (num > 4) { result โ† result + num } }\nDISPLAY(result) \nWhat is displayed?

Q66
MULTIPLE_CHOICEHard

An algorithm has a running time that doubles each time the input size increases by 1. If the algorithm takes 2 seconds for an input of size 10, approximately how long does it take for an input of size 20?

Q67
MULTIPLE_CHOICEMedium

A robot is in the bottom-left corner of a 4ร—4 grid and needs to reach the top-right corner. The robot can only move right or up. How many different paths can the robot take?

Q68
MULTIPLE_CHOICEHard

A programmer writes two versions of a search algorithm. Version A uses linear search and Version B uses binary search on a sorted list. For which list size will the performance difference be MOST noticeable?

Advertisement