4. A binary search algorithm is written (as in the modules, for example) which s
ID: 3842302 • Letter: 4
Question
4.
A binary search algorithm is written (as in the modules, for example) which searches a pre-sorted array for some user-defined value, clientData. If clientData is stored in the array, it returns its array position, and if not found, it returns -1 (again, just like in the modules). Assume the array to be searched has 100 data elements in it. (Check all that apply):
[NOTE: due to common off-by-one interpretations when counting such things, if your predicted answer is within one (+1 or -1) of a posted option below, you can assume your prediction and the choice you are looking at are equivalent and check that option.]
It will always return with an answer in 3 or fewer comparisons of data.
3.
Assume MyClass defines two nested exception classes, BadNumber and BadString. Also, assume that MyClass's method doSomething() throws these exceptions under certain error conditions, and returns without throwing anything when it decides no error has occurred. We call doSomething four times in a try/catch block:
Check the true statements (there will be more than one correct answer).
[Ignore whether spaces or ' ' are displayed - it is the words of the cout statements that we are interested in.]
It is possible that this block will be called without anything being displayed on the screen.
A. It might return to the client with an answer after only one comparison of data. B. It may require as many as 99 comparisons of data before it returns. C. It will always return with an answer in 7 or fewer comparisons of data. D.It will always return with an answer in 3 or fewer comparisons of data.
Explanation / Answer
1. c) it will always return with an answer of 7 or less comparisons
as max no. of comparison in worst case for sorted binary search is log2n here n= 100, so max search is 6.6 something or 7
2)correct choices are a,b,e as it may be possible that none of the calls throws exception and runs successfuly. so maybe nothing will be visible.
both error messages cannot be printed together as if it enters one catch statement, next statements are not executed. so c is wrong .
d is also wrong because if dosomething method is not throwing any error it will not enter catch block, so none of the errors can be displayed.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.