Question 7 options: reverses the order of the elements in q randomizes the order
ID: 3558907 • Letter: Q
Question
Question 7 options:
reverses the order of the elements in q
randomizes the order of the elements in q
does nothing to the order of the elements in q
it is not possible to say what the effect is
Suppose that an intermixed sequence of push and pop operations are performed on a LIFO stack. The pushes push the numbers 0 through 9 in order; the pops print out the return value. Which of the following output sequence(s) could occur? Select all that are possible.
Question 8 options:
4 6 8 7 5 3 2 9 0 1
0 2 4 1 6 7 5 9 8 3
0 1 5 6 4 3 7 9 2 8
6 5 4 3 2 1 0 7 8 9
1 2 5 4 3 6 0 9 8 7
Suppose that an intermixed sequence of enqueue and dequeue operations are performed on a FIFO queue. The enqueues add the numbers 0 through 9 in order; the dequeues print out the return value. Which of the following output sequence(s) could occur? Select all that are possible.
Question 9 options:
7 4 2 0 1 9 3 6 5 8
0 3 5 9 1 8 4 7 2 6
0 1 2 3 4 5 6 7 8 9
3 2 4 8 7 9 1 5 6 0
1 2 0 5 6 4 7 9 3 8
Assume that linked lists of int are implemented with the following Node class.
class Node {
int item;
Node next;
}
Consider the following function, whose intention is to sum the elements of a list starting at Node x.
int sum (Node x) {
if (x == null)
return 0;
} else {
/*code*/
}
}
Which of the following replacements for /*code*/ returns the required value for sum?
I int rest = sum (x.next);
return rest + x.item;
II x = x.next;
int rest = sum (x);
return rest + x.item;
III int i = x.item;
x = x.next;
return sum (x) + i;
Question 10 options:
II and III only
III only
I and III only
II only
I and II only
I only
I,II,andIII
Recall that for any string s, s.substring(index) returns the substring
of s starting at position index and extending to the end of string
s. If index equals s.length(), the empty string is returned. If index
is greater than s.length(), a StringIndexOutOfBoundException is
thrown.
Consider the following code fragment, which uses a Queue.
public static void main (String[] args) {
Queue q = new Queue ();
q.enqueue ("one");
q.enqueue ("two");
q.enqueue ("four");
q.enqueue ("six");
String s = "";
int i = 0;
while (!q.isEmpty()) {
s = s + q.dequeue().substring(i);
i++;
}
StdOut.print (s);
}
What string is output as a result of executing this main function?
Question 11 options:
onwourx
soo
sixouro
There would be no output. A StringIndexOutOfBoundException would be thrown.
onewour
Consider the following code segment.
Queue q1 = new Queue ();
Queue q2 = new Queue ();
Stack> s = new Stack> ();
q1.enqueue ("all");
q1.enqueue ("bee");
q2.enqueue ("pig");
q2.enqueue ("que");
s.push (q1);
s.push (q2);
q1.dequeue ();
q2.dequeue ();
while (!s.isEmpty ())
StdOut.print (s.pop () + " ");
What output will be produced as a result of executing this code segment?
Question 12 options:
pig que all bee
bee que
que bee
all bee pig que
pig all
Bottom of Form
reverses the order of the elements in q
randomizes the order of the elements in q
does nothing to the order of the elements in q
it is not possible to say what the effect is
Explanation / Answer
7)
while (!q.isEmpty())
stack.push(q.dequeue());
The above code dequeues all the elements in the queue one by one and pushes then into the stack.
while (!stack.isEmpty())
q.enqueue(stack.pop());
Since the stack is a last in first out (LIFO) data structures , the above code pops the items one by one and enques them into the queue. Now the queue contains the items in reverse order;
Example:
q=[1,2,3,4,5], s= empty;
s=[12,3,4,5] , q=empty
s=empty, q=[5,4,3,2,1]
Therefore the correct option is “reverses the order of the elements in q”
8)
The output “ 4 6 8 7 5 3 2 9 0 1 “ could occur because the following sequence of pushes and pops gives the sequence.
push(0);push(1);push(2)push(3);push(4);pop();push(5);push(6);pop();push(7);
push(8);pop();pop();pop();pop();push(9);pop();pop();pop().
The output “ 0 1 5 6 4 3 7 9 2 8“ could occur because the following sequence of pushes and pops gives the sequence.
Push(0);pop();push(1);pop();push(2);push(3);push(4);push(5);pop();push(6);pop();
pop();pop();push(7);pop();pop();push(8);push(9);pop();pop();Push(8);
The output “ 6 5 4 3 2 1 0 7 8 9“ could occur because the following sequence of pushes and pops gives the sequence.
push(0);push(1);push(2);push(3);push(4);push(5);push(6);pop();pop();pop();pop();
pop();pop();pop();push(7);pop();push(8);pop();push(9);pop();
The output “ 1 2 5 4 3 6 0 9 8 7“ could occur because the following sequence of pushes and pops gives the sequence.
Push(0);push(1);pop();push(2);pop();push(3);push(4);push(5);pop();pop();pop();push(6);
pop();pop();push(7);push(8);push(9);pop();pop();pop();
Therefore the correct options are: “4 6 8 7 5 3 2 9 0 1, 0 1 5 6 4 3 7 9 2 8, 6 5 4 3 2 1 0 7 8 9, 1 2 5 4 3 6 0 9 8 7 ”.
9)
The output “ 0 1 2 3 4 5 6 7 8 9“ could occur because the following sequence of enques and dequeues gives the sequence.
Enque(0);enque(1);enque(2);enque(3);enque(4);enque(5);enque(6);enque(7);enque(8);
enque(9);dequeue();dequeue();dequeue();dequeue();dequeue();dequeue();dequeue();
dequeue();dequeue();dequeue();
Hence, the correct option is: “0 1 2 3 4 5 6 7 8 9”.
10)
II) x = x.next;//moves to next
int rest = sum (x);
return rest + x.item;
The above code gives the sum of all items except the first node.
I) int rest = sum (x.next); //gives the sum of items of all nodes from x.next
return rest + x.item; //returns the sum of item of current node and the rest of the nodes
The above code gives the recursively calls the function sum with next node and returns the total sum.
III) int i = x.item; //current node item
x = x.next; // move to next node
return sum (x) + i; //return sum of item of next node and item of current node
The above code also recursively calls the function sum on next node and returns the total sum.
Hence, the correct option is “I and III only”
11)
Since in the first iteration i=0 and first element in the queue is “one”, q.dequeue().substring(0); gives the string “one”.
Therefore s=”one”
Since in the second iteration i=1 and first element in the queue is “two”, q.dequeue().substring(1); gives the string “wo”.
Therefore s=”onewo”
Since in the third iteration i=2 and first element in the queue is “four”, q.dequeue().substring(2); gives the string “ur”.
Therefore s=”onewour”
Since in the third iteration i=3 and first element in the queue is “six”, q.dequeue().substring(3); does not result any string.
Therefore s=”onewour”
Since the queue is empty, the loop is exited and prints the string”onewour”.
Therefore the correct option is “onewour”.
12)
q1.dequeue (); removes the string “all” from the queue q1
q2.dequeue (); removes the string “pig” from the queue q2
in the first iteration of while loop , s.pop() pops q2 and prints que.
In the second iteration of while loop, s.pop() pops q1 and prints bee.
Note that s.push (q1); and s.push (q2); pushes only the references for the two queues. But not the values.
Therefore the correct option is “quebee”
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.