What’s the output of the following code?
Queue<Integer> q = new Queue<Integer>();
q.enqueue(30);
q.dequeue();
q.enqueue(40);
q.enqueue(20);
q.dequeue();
q.enqueue(80);
System.out.print(q.dequeue());
20 Explanation: ------------- Queue<Integer> q = new Queue<Integer>(); q.enqueue(30); // queue is [30], with 30 at front q.dequeue(); // front=30 is removed, queue is []. q.enqueue(40); // queue is [40], with 40 at front q.enqueue(20); // queue is [40, 20], with 40 at front q.dequeue(); // front=40 is removed, queue is [20]. q.enqueue(80); // queue is [20, 80], with 20 at front System.out.print(q.dequeue()); // front=20 is removed and printed
What’s the output of the following code? Queue<Integer> q = new Queue<Integer>(); q.enqueue(30); q.dequeue(); q.enqueue(40); q.enqueue(20);...
4. The Singly.Linked List below represents a queue q where q.peeko = 90. Suppose the following operations are performed: q.dequeue(); q.enqueue(17); q.dequeue(); q.dequeue(); What is the output of q.peeko? Select 'No Answer' if the program results in an error. No answer 90 54 13 81 64 53
Collections (a) What are generic types and what is their importance in ADTs for collections? (b) How are the ADTS of stacks and queues similar? How does their behaviour differ? (c) What is the difference between a static and a dynamic data structure? Why are static structures such as arrays often used in the background for dynamic data structures (e.g., in Java's ArrayList)? What issues does this raise and how are they generally resolved? (d) Suppose the following fragment occurs...
Design and implement a class Q that uses Q.java as a code base. The queue ADT must use class LinkedList from Oracle's Java class library and its underlying data structure (i.e. every Q object has-a (contains) class LinkedList object. class Q is not allowed to extend class LinkedList. The methods that are to be implemented are documented in Q.java. Method comment blocks are used to document the functionality of the class Q instance methods. The output of your program must...
Complete the code below, test cases to use are at the bottom package Labs; public class ResizingQueue { private final int INITIAL_SIZE = 10; private int [] A = new int[INITIAL_SIZE]; // array to hold queue: front is always at Q[0] // and rear at A[next-1] int next = 0; // location of next available unused slot // interface methods public void enqueue(int key) { //TODO: push the key onto the back of the queue // YOUR CODE HERE! }...
Write a method called reverseFirstK that accepts an integer k and a queue of integers as parameters and reverses the order of the first k elements of the queue, leaving the other elements in the same relative order. For example, if a queue named q stores [10, 20 30, 40, 50, 60, 70, 80, 90], the call of reverseFirstK (4, q):should change the queue to store [40, 30 20, 10, 50, 60, 70, 80, 90]. If k is 0 or...
What is the output from running the following code snippet. void mystery(Queue<int>& q) { Stack<int> s; while (!s.isEmptyQueue()) { s.push(q.front()); q.deleteQueue(); } while (!s.isEmptyStack()) { q.addQueue(2 * s.top()); s.pop(); } } Queue q; q.addQueue(8); q.addQueue(4); q.addQueue(18); q.addQueue(7); q.addQueue(5); mystery(q); cout << "["; while (!.isEmptyQueue()) { cout << " " << q.front(); } cout << " ]" << endl;
1. What is output by the following code: ArrayList< Integer > a = new ArrayList< Integer >(); ArrayList b = a; a.add(new Integer(4)); b.add(new Integer(5)); a.add(new Integer(6)); a.add(new Integer(7)); System.out.println(b.size()); A)1 B)2 C)3 D)4 E)5 2. Assume the Student and Employee classes each extend the Person class. The Student class overrides the getMoney method in the Person class. Consider the following code: Person p1, p2, p3; int m1, m2, m3; p1 = new Person(); m1 = p1.getMoney(); // assignment 1...
What’s the output of the following code? num = 3 print('my lucky number is ' + num)
Consider the following circular array queue. Indicate what would be printed when executing the following code (assuming array is the variable containing the elements): count 5 front XHL se 2 rear 0 1 2 3 4 N int t = front; for (int q = 0; q < count; q++) { System.out.print(array(q) + t = (t + 1) % array.length; } ");
Consider the following Java method: public static ArrayList<Integer nums ArrayList<Integer values new ArrayList<Integer0; for(int i=10; i<30; i-i+3) if(i%) values.add(i); return values: What is returned after the method call nums()? a. [12] b. [13] c. [14] d. [15] e. [16] O Which of the following represents the final output of the code segment below? int k: int[]A; A new int[3]: fork-0; k<A.length;k++) A[k]-A.length-k; forſk-0; k<A.length-1; k++) A[k+1]-A[k) for(int i-0;i<A.length;i++) System.out.print(A[i]+" "); a. 222 O b. 333 c. 444 d. 555 O...