Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q1. Which of the following is an operation provided by the Java Stack class? a.

ID: 3847579 • Letter: Q

Question

Q1. Which of the following is an operation provided by the Java Stack class?
   a. peek()
   b. pushItem
   c. destroy(stack)
   d. delete(item)

Q2. StackClass s = new StackClass(50);
int x, y, z;

s.push(new IntElement(3));
s.push(new IntElement(7));
x = s.pop();
s.push(new IntElement(5));
y = s.pop();
z = s.pop();

Given the sequence of operations above, what is the value of z?
   a. 0
   b. 3
   c. 5
   d. 7

Q3. The operation push removes the bottom element from the stack.
   a. true
   b. false

Q4. If the stack is full, then push will throw a StackOverflowException.
   a. true
   b. false

Q5. To traverse a linked list, first set current = first;
Next, set up a loop, using the following test: while(current.link != NULL)
   a. true
   b. false

Q6. In a linear representation of a stack, which of the following values points to the top item in the stack?
   a. stackTop
   b. stackTop - 1
   c. 0
   d. -1

Q7. In a linked list implementation of a stack, the reference variable to the top of the stack is set to NULL.
   a. true
   b. false

Q8. A stack is also called a LIFO data structure.
   a. true
   b. false

Q9. The operation top removes the top element of the stack.
   a. true
   b. false

Q10. An array is a random access data structure; a stack is not.
   a. true
   b. false

Explanation / Answer

Q1) The answer for Q1 is Option A) peek() because remaining options are not related to the Stack class.

Q2) The answer for Q2 needs steps to be explained if you can see below:

StackClass s = new StackClass(50);
int x, y, z;

s.push(new IntElement(3));
s.push(new IntElement(7));
x = s.pop();
s.push(new IntElement(5));
y = s.pop();
z = s.pop();

First we are creating an object for the StackClass and then we are inserting elements into the stack using push(). Here we have to always remember one main key point that stack follows LIFO mechanism which means Last-In-First-Out mechanism.So the element which is on the top of the stack is the element that get removed first always.

so in the steps:

s.push(new IntElement(3));
s.push(new IntElement(7));

we are inserting the elements 3 and 7 as 7 remaining at the top of the stack.

then in the next step we are removing the element 7 using pop() on the object reference of StackClass and storing it to the variable x.

so the element 3 remains in the stack.

in the step again we are inserting one element 5 using push() now the elements in the stack are 5 and 3 with 5 on the top of the stack.

In the further steps

y = s.pop();
z = s.pop();

we are now removing elements in the stack one by one in which 5 gets removed first followed by 3 making the stack empty and storing the values to y and z instance variables.

Q2)The answer for the question 3 is option B

Q3)The answer for this question is option B)false as the push() inserts elements into stack and doesnt remove them from the stack.

Q4)The answer for this queestion is option A)true because there will be no room for any new element when the stack is full and this makes the push() throw StackOverFlowException.

Q5)The answer for this question is true as we are validating/checking for the current element for not null.

Q6)The answer for this question is -1 for the top item if the stack is empty and stacktop-1 if the stack is full.

Q7)In a linked list implementation of a stack, the reference variable to the top of the stack is set to NULL is true.It is worth it to set S[top] to null. The reference to the popped object is returned to the caller who can do anything to that object. When the caller is done with that object, then it no longer needs to be referenced by either the caller or the Stack. As long as no other object references it, then it is eligible for garbage collection.

Q8)A stack is also called a LIFO data structure which is true as the elements that are inserted last get removed first from the stack.

Q9)The answer for this is false as there is no such method in stack class.

Q10)The answer for this is false as stack is also a random access data structure as it implements RandomAccess Interface.