reset password
Author Message
rabbott
Posts: 1649
Posted 13:00 Nov 20, 2013 |

You are right that question 4 had no right answers. The answers should have included an extra pair of parentheses. The intended correct answer was:

d) int val = (int) (Math.random() * 80);

A copy of the quiz with the correct answers indicated is attached below.

Following are some comments on some of the other questions with a significant number of wrong answers. 

5. One way to generate the first n positive even integers is to count to n and multiply each element by 2. That's what correct answer (b) does.

6. It's possible that Math.random() will generate 0, in which case x will be 0, and the loop will terminate. But one doesn't know when that might happen. So (c) is the correct answer.
 
7. The expression rand.nextInt(41) will generate random numbers from 0 to 40 (but not 41). Subtracting 20 from that will generate random numbers from -20 to 20. (d) is the right answer.
 
8. rand.nextDouble() will generate a random double from 0 to (but not including) 1.  
So 
rand.nextDouble() * 2 + 3 
will generate a random double from 0 to (but not including) 5. 
 
9. The loop condition    while (position < str.length() && (str.charAt(position) != 'e'))  will continue the loop as long as position is within the string and is not at an 'e'. That means it will stop if it ever reaches an 'e'.  (a) is the right answer.
 
10. All numbers that are not positive are ignored. When a positive number is seen it is added to sum and count is incremented. The code computes the average of the positive numbers. 
 
 
 

 

Attachments:
Last edited by rabbott at 17:52 Nov 20, 2013.