Unit 3: Algorithms and Programming

Showing 20 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?

Advertisement