JAVA
What values are stored in variables a and b in the code below?
public class StackQuestion {
public static void main(String[] args) {
Stack s = new Stack();
s.push(1);
s.push(2);
s.push(3);
s.pop();
s.pop();
s.push(4);
s.push(5);
s.pop();
s.pop();
int a = s.pop();
s.push(6);
int b = s.pop();
}
}
What numbers are stored in variable a and b when the code below executes?
public class QueueQuestion {
public static void main(String[] args) {
Queue s = new Queue();
s.enqueue(1);
s.enqueue(2);
s.enqueue(3);
s.dequeue();
s.dequeue();
s.enqueue(4);
s.enqueue(5);
s.dequeue();
s.dequeue();
int a = s.dequeue();
s.enqueue(6);
int b = s.dequeue();
}
}
1)
Solution)The values 1 and 6 are stored in the variables a and b respectively.
Explanation:
Stack is a LIFO data structure i.e Last In First Out, the elements that is inserted at last will be removed first from the stack,Please consider the images below for complete explanation:
2)
Solution)The values 5 and 6 are stored in the variables a and b respectively.
Queue is a FIFO data structure i.e First In First Out, the elements that is inserted at first will be removed first from the queue, Enqueue is the process of adding an element at the back of the Queue and the process of removing an element from the front of the Queue is called Dequeue.Please consider the images below for complete explanation
NOTE:.Please upvote if you liked my answer and comment if you need any modification or explanation.
Get Answers For Free
Most questions answered within 1 hours.