The following function counts down starting at Y. What number does the countdown return if it is called with Y= 10?
A. -1
B. 0
C. 1
D. 10
do-while loops are considered post-test loops because they always execute the do-section before checking whether a specific condition is met. do-while loops are used when you always want the body of the loop to execute at least once, regardless of whether a condition is met.
To answer this, lets step through this function as follows:
Y = 10
Enter the do-while loop
Y = 10 - 1 = 9
We then check the condition, is Y > 0? In this case, 9 is greater than zero so we loop back to the do-statement.
Y = 9 - 1 = 8
8 is greater than zero so we loop back to the do-statement.
Y = 8 - 1 = 7
7 is greater than zero so we loop back to the do-statement.
Y = 7 - 1 = 6
6 is greater than zero so we loop back to the do-statement.
Y = 6 - 1 = 5
5 is greater than zero so we loop back to the do-statement.
Y = 5 - 1 = 4
4 is greater than zero so we loop back to the do-statement.
Y = 4 - 1 = 3
3 is greater than zero so we loop back to the do-statement.
Y = 3 - 1 = 2
2 is greater than zero so we loop back to the do-statement.
Y = 2 - 1 = 1
1 is greater than zero so we loop back to the do-statement.
Y = 1 - 1 = 0
0 is not greater than zero so we break out of the do-while loop and return Y. Since Y = 0, our answer is B.
Given a complete binary search tree consisting of 15 elements, the height of the tree is:
A. 15
B. 8
C. 7
D. 3
The following pseudocode, which has a worst-case timing of $$O(n^2)$$, is what type of classical sort algorithm?
A. Bubble sort
B. Insertion sort
C. Merge sort
D. Quick sort
The final value of Q in the following flowchart is most nearly:
A. 0
B. 1
C. 3
D. 5