The following code segment is a count-controlled loop going from 1 through 5. At
ID: 675381 • Letter: T
Question
The following code segment is a count-controlled loop going from 1 through 5. At each iteration, the loop counter is either printed or put on a queue depending on the boolean result returned by the method random. (Assume that random randomly returns either true or false.) At the end of the loop, the elements in the queue are removed and printed. Because of the logical properties of a queue, this code segment cannot print certain sequences of the values of the loop counter. You are given an output and asked to determine whether the code segment could generate the output.
for (count =1; count <= 5; count++)
{
if (random())
System.out.println(count);
else
queue.enqueue(count);
}
while (!queue.isEmpty())
{
number = queue.dequeue();
System.out.println(number);
}
The following output is possible: 1 2 3 4 5
i. True ii. False iii. Not enough info.
b. The following output is possible: 1 3 5 4 2
i. True ii. False iii. Not enough info.
c. b. The following output is possible: 1 3 5 2 4
i. True ii. False iii. Not enough info.
Explanation / Answer
The following output is possible: 1 2 3 4 5
iii. Not enough info.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.