. | |
What is the value of this postfix expression?
4 5 + 2 * |
|
D. The infix equivalent is (4 + 5) * 2 = 18 |
. | |
When evaluating a postfix expression using the stack-based algorithm, what does it mean when all of the input has been processed and the stack contains exactly one value? |
|
no error |
. | |
When checking for balanced parentheses using the stack-based algorithm, if the current input symbol is a right paren, the correct action is ... |
|
D |
. (1 pt. each) | |
Which one of these is the best? Which one is worst? Write B or W next to your choices. ("Best" implies asymptotically smallest in questions like these). |
|
. (1 pt. each) | |
Fill in the blanks in this definition (from the
slides):
Definition: Let T and f be functions of the natural numbers. T(n) is in _______ if there are constants c and n0 such that T(n) < cf(n) for all _______. |
|
O(f(n) or just O(f)
n >= n0 or n > n0 |
. (1 pt. each) | |
Study this method:static int mystery (int input) { if (input < 0) { return -mystery(-input); } if (input <= 1) { return 0; } return 1 + mystery(input/3); } |
What is the value of mystery(10) ? ______
What is the value of mystery(-4) ? ______
|
mystery(10) = 1 + mystery(3) = 1 + (1 +
mystery(1)) = 1 + 1 + 0 = 2
mystery(-4) = -mystery(4) = -(1 + mystery(1)) = -(1 + 0) = -1 |
. (bonus question -- 1 free pt.) | |
True or False: according to the syllabus, the lowest midterm will be dropped. So if you oversleep on Friday, it's no big deal. | |
. (not graded) | |
Suppose you are asked to implement the following method
recursively (no loops):/** Return true if and only the array is non-null, non-empty, and contains only positive numbers. */ static boolean isAllPos(double nums[]); Think about implementing this method. Then either A. if you feel that isAllPos can be implemented recursively without a helper method, and without any library classes or methods, give full code for implementing isAllPos OR B. explain why isAllPos cannot be implemented without a helper method.
|
|
isAllPos is not suitable for a direct
recursive implementation. There is nothing in the parameters which
allows one to keep track of how much of the array has been checked.
A suitable helper method might be this one:
/** return true iff the all the values in the array, from the position given to the end, are positive. */ static boolean isPos(double[] nums, int position); With this helper, you could implement isPos as follows (you did not have to give this code as part of your answer, however!) { if (nums == null || nums.length == 0) return false; return isPos(nums, 0); } |