Question: java code q1- What is the output of the following operations if STK1 is a stack and QU1 is a queue and both STK1 and
java code
q1- What is the output of the following operations if STK1 is a stack and QU1 is a queue and both STK1 and Q are initially empty? STK1.push(X) STK1.push(N) QU1.add(M) STK1.push(A) QU1.add(STK1.pop( )) print (QU1.remove( )) QU1.add(G) STK1.push(QU1.remove( )) print(STK1.pop( )) print(STK1.pop( )) Show the output and after that draw your result
q2- You are given a Stack class and a Queue class. The following functions are available for use: public class Stack { public boolean empty(){}; public void push(int n){}; public int pop(){}; } public class Queue { public boolean empty(){}; public void enqueue(int n){}; public int dequeue(){}; } Write a method reverseQueue that takes a queue, myQueue, as an input and reverses the order of elements in myQueue. You are only allowed to use Stack and Queue objects in your method.
q3- Suppose that we have two stacks A and B of size 4, where stack b is initially empty and stack A is Full having the elements {x, y, z, t}in the same order, where the top element has data t. Note that when stack is Full we cant add any item to it. Draw the two stacks after executing each of the following pieces of code: (the two parts are separate) a) while(!A.isEmpty()){ B.push(A.peek()); B.push(A.pop()); } b) for(i=0; i<3; i++) B.push(A.peek());
q4-a) Write a method searchNum that takes an array x[] and an integer n, and returns true if element n is a number found in array x[] and false if not, using Iterative approach. b) Write a method searchNum that takes an array x[], the size of the array size, and an integer n, and returns true if element n is found in array x[] and false if not, using Recursive approach.
q5_ Consider the following method declaration: public void fun(int i) { if (i > 1) { fun(i / 2); fun(i / 2); } System.out.print("*"); } a) How many asterisks are printed by the method call fun(5) ? b) Draw only the decomposition.
q6- What is the output of the following program if n=5? Implements and Show all details and evidence for the output. public static int doubleResult(int n) int total=0; { if (n == 0) return 0; else total = 2 + doubleResult(n-1); System.out.println(total); return total; }
q7- Given the following program: 1. public static int quadIt(int n) 2. { 3. if(1 == n) return 1; 4. else return 4*quadIt(n-1); 5. } a) Which line is the Base case? b) What will be the value of quadIt(4)? Show how you calculated it.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
