Which if any of the following code segments correctly implements the constructor
ID: 3817772 • Letter: W
Question
Which if any of the following code segments correctly implements the constructor for Stack? A) Stack:: Stack {} B) Stack:: Stack {} {{data = new int [MAXSIZE]; top = -1; int top = -1;}} C) Stack:: Stack {} {top = -1;} D) void stack:: Stack {} {top = 0;} E)None of the answers provided Which if any of the following code segments correctly implements the method Push? A) void Stack:: Push {int n} {if (top = = MAXSIZE-1} throw StackFull (); else {data [n] = top; top++;}} B) void Stack:: Push (int n) {if (IsFull ()) throw StackFull (); else {top++; data [top] = n;}} C) void Stack:: Push {int n} {if {z isEmpty ()} throw StackEmpty (); else {data [top] = n;}} D) void stack:: Push {int n} {if {top = = MAXSIZE-1} throw StackFull (); else {data [top] = n; top++;}} E) None of the answers provided Which if any of the following code segments correctly implements the method Available? A) void Stack:: Available () const {return MAXSIZE;} B) int Stack:: Available () const {returnExplanation / Answer
Ques 8: Constructor for stack
option(a)
when constructing a stack we need to know the size of the stack which here we are using to declare the size of stack using the line data = new int[MAXSIZE];
top should be -1 as initially there is no elements in the stack.
Ques 9: Push in Stack
option(b)
Push operation in Stack is used to enter elements in stack.
So first it needs to check whether the stack has enough space to enter the element in a stack which can be done by checking if top == MAXSIZE -1; but in option b the statement used is if ( IsFull() ) if stack is full then an exception is thrown else we we push the element in stack. So option c is eliminated option a,b,d,e are still to be checked.
In option a it is entering top at nth position which a stack doesnot do. So option a is incorrect.
In option b first the stack is updated and then it is pushing the elements in the stack. So option b is correct.
Since option b is correct we dont need to check option d but still I would explain it.
In option d first stack is entering element at previous top and then it is updating the top. If I need to push an element in an empty stack then it would put the element at position -1 and stack starts at 0 not -1. Hence incorrect.
option e works if all option are incorrect.
Hence option b is the correct answer.
Ques 10: Available in Stack
option (c)
Available() method should tell the availability of the stack which can only be checked using top.
So top is returned.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.