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 bNOT a AND b
Was this exam question helpful?
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
Choose your answer
Was this exam question helpful?
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
Choose your answer
Was this exam question helpful?
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
Choose your answer
Was this exam question helpful?
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)
Choose your answer
Was this exam question helpful?
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
Choose your answer
Was this exam question helpful?
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)
Choose your answer
Was this exam question helpful?
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
Choose your answer
Was this exam question helpful?
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
Choose your answer
Was this exam question helpful?
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
Choose your answer
Was this exam question helpful?
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
Choose your answer
Was this exam question helpful?
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
Choose your answer
Was this exam question helpful?
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
Choose your answer
Was this exam question helpful?
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
Choose your answer
Was this exam question helpful?
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
Choose your answer
Was this exam question helpful?
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
Choose your answer
Was this exam question helpful?