Unit 3: Algorithms and Programming
Showing 50 of 68 questions
What is the value of x after the following code segment is executed? x โ 5 x โ x + 3 x โ x * 2
What is the output of the following code segment? a โ 10 b โ 20 a โ b DISPLAY(a)
What are the values of a and b after the following code segment? a โ 3 b โ 7 temp โ a a โ b b โ temp
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)
What is displayed after executing this code? result โ 0 FOR i โ 1 TO 5 { result โ result + i } DISPLAY(result)
A list contains n elements. A linear search is used to find a specific value. What is the maximum number of comparisons needed?
A sorted list contains 1024 elements. Using a binary search, what is the maximum number of comparisons needed to find a specific value?
Which of the following is a key requirement for binary search to work correctly?
What is displayed by the following code? PROCEDURE double(x) { RETURN x * 2 } result โ double(double(3)) DISPLAY(result)
Which algorithm has a reasonable (polynomial) running time?
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) } }
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)
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)))
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?
What is displayed after the following code? list โ [10, 20, 30] APPEND(list, 40) REMOVE(list, 2) DISPLAY(list)
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?
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 }
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?
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)
Which of the following is true about the efficiency of algorithms?
What is a "heuristic" approach to solving a problem?
What is displayed by the following code? count โ 0 FOR i โ 1 TO 4 { FOR j โ 1 TO 3 { count โ count + 1 } } DISPLAY(count)
What is a simulation?
What is displayed after the following code? x โ 10 IF (x > 5) { x โ x - 3 } IF (x > 5) { x โ x - 3 } DISPLAY(x)
If a = true and b = false, what is the value of (a AND b) OR (NOT b)?
What is a primary benefit of using procedures (functions) in programming?
In the AP CSP pseudocode, what does list[3] refer to if list = [10, 20, 30, 40]?
What is the value of 17 MOD 5?
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:
What is displayed?\nx โ 15\nIF (x > 20) { DISPLAY("high") }\nELSE { IF (x > 10) { DISPLAY("medium") } ELSE { DISPLAY("low") } }
What does this procedure return when called as mystery(5)?\nPROCEDURE mystery(n) { result โ 0 REPEAT n TIMES { result โ result + n } RETURN result }
What is displayed after this code runs?\nlist โ [3, 7, 1, 9, 4]\nREMOVE(list, 2)\nAPPEND(list, 5)\nDISPLAY(LENGTH(list))
In AP CSP pseudocode, RANDOM(1, 6) generates:
An algorithm that checks every element in a list of n items to find a target has a time complexity described as:
Binary search requires that the data be:
To swap the values of variables a and b, which sequence works correctly?
An undecidable problem is one for which:
A heuristic approach to problem-solving:
Simulations are useful because they:
What is displayed?\nnums โ [2, 4, 6, 8]\nsum โ 0\nFOR EACH num IN nums { sum โ sum + num }\nDISPLAY(sum)
What is displayed?\nfirst โ "Hello"\nsecond โ "World"\nDISPLAY(CONCAT(first, " ", second))
How many times is DISPLAY called?\nREPEAT 3 TIMES { REPEAT 4 TIMES { DISPLAY("*") } }
An algorithm that takes 2^n steps for an input of size n runs in:
What is displayed?\nPROCEDURE triple(x) { RETURN x * 3 }\nresult โ triple(triple(2))\nDISPLAY(result)
Which best describes how computing enables citizen science?
Which code correctly finds the minimum value in a list?\nnums โ [5, 3, 8, 1, 6]
Abstraction in programming allows developers to:
After the following code, what is list?\nlist โ [1, 2, 3]\nINSERT(list, 2, 10)\nAPPEND(list, 20)
After executing:\na โ 5\nb โ a\na โ 10\nWhat is the value of b?
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)
Advertisement