What is the output of the following code: s.push( 10 ); Stack s = new Stack(); s
ID: 3687655 • Letter: W
Question
What is the output of the following code:
s.push( 10 );
Stack s = new Stack();
s.push( 6 );
s.push( 12 );
s.push( 61);
s.pop();
s.push( s.top() );
s.push( s.top() );
for(int i = 0; i <3; i++)
s.pop();
System.out.println( “Output: “+s.top() );
_______________________________________________________________________________________________________________________________________________
Assume array-based implementation of a stack with array size is set to 6. Give the content of the array referred to by stackArray and the value of myTop in the stack s after the code segment is executed, or indicates why an error occurs. If an error occurs, you must show the content of the array and myTop before the error.
0 1 2 3 4 5 6
Explanation / Answer
Indexes of the array:
Inititally, s.top points to -1.
s.push( 10 ); //This can lead to error, if s is not initialized previously. As per the give piece of code, this should lead to an error.
Stack s = new Stack(); //Now a new Stack object s is intantiated.
s.push( 6 ); //Pushes an element 6 onto the stack, which means, 6 will be added to the array at position 0, and top will be updated to 0.
s.push( 12 ); //Pushes an element 12 onto the stack, which means, 12 will be added to the array at position 1, and top will be updated to 1.
s.push( 61); //Pushes an element 61 onto the stack, which means, 61 will be added to the array at position 2, and top will be updated to 2.
s.pop(); //The top most element in the stack will be popped out.Which means, the element at index 2 in the array will be deleted, and top is now pointing to 1 again.
s.push( s.top() ); //Retrieves the top element in the stack, and the same will be pushed onto the stack. Note that the top element will not be deleted, it will be just retrieved witht the statmenet s.top(). So, 12 will be retrieved, and ofcourse it will be pushed again onto the stack. So, 12 will be inserted again into the index 2 in the array, and top will be updated to 2.
s.push( s.top() ); //Again, Retrieves the top element in the stack, and the same will be pushed onto the stack. Note that the top element will not be deleted, it will be just retrieved with the statmenet s.top(). So, 12 will be retrieved, and ofcourse it will be pushed again onto the stack. So, 12 will be inserted again into the index 3 in the array, and top will be updated to 3.
for(int i = 0; i <3; i++)
s.pop(); //Executes this statement for 3 times, for values i = 0, 1, and 2. Therefore, the top 3 elements in the stack will be popped out. Note that, its not accessing, its popping. So, the top 3 elements in the stack, i.e., elements at positions 3, 2, and 1 will be popped out, and now top will be pointing to 0.
System.out.println( “Output: “+s.top() ); //Prints the top element in the stack. Therefore, the only element in the stack, i.e., 6 will be printed, onto the screen.
0 1 2 3 4 5 6Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.