Reverse a Word using Stack – Data Structure Questions and Answers

Computer Science & Engineering Data Structures & Algorithms

This set of Data Structures & Algorithms Multiple Choice Questions & Answers (MCQs) focuses on “Reverse a Word using Stack”.

1. How many stacks are required for reversing a word algorithm?
A) one
B) two
C) three
D) four

Explanation: When reversing a word with stack, only one stack is needed. Push and pop operations are performed in the stack.

2. What will be result if the given stack is popped?

A) pat
B) tap
C) atp
D) apt

Explanation: The term ‘pat’ is scribbled on the stack. The term ‘tap’ is obtained by popping the characters of the stack one by one.

3. What will be output if the following sequence of operations are executed?

Push(a,s);
Push(b,s);
Pop(b);
Push(c,s);

A) abc
B) b
C) ac
D) acb

Explanation: The stack’s element ‘b’ is popped out. As a result, the output of the following operations will be ‘b.’

4. What are the set of functions that are to be executed to get the following output?
cat

A) push(c, s); push(a, s); push(t, s);
pop(s); pop(s); pop(s);
B) push(c,s); pop(s); push(a,s); pop(s);push(t,s);pop(s);
C) pop(c ); pop(a); pop(t);
D) push(c,s); push(a,s); pop(t);

Explanation: The characters ‘c, ‘a,’ and ‘t’ are inserted into the stack during push procedure and popped immediately after push.

5. How will your stack look like if the word ‘java’ is pushed?

A)

data-structures-questions-answers-reverse-word-stack-q10a

B)

data-structures-questions-answers-reverse-word-stack-q10b

C)

data-structures-questions-answers-reverse-word-stack-q10c

D)

data-structures-questions-answers-reverse-word-stack-q10d



Explanation: When you push a character, it stays at the top of the list. Since the stack follows the LIFO principle, the term appears in reverse order when popping.

6. Find the error (if any) in the following code snippet for pop operation.

void pop() //removing an element from a stack
{
     printf(“%s”, stack[top++]);
}

A) run time error
b) compile time error
c) pop operation is performed, but top moved in wrong direction
d) pop operation is performed properly

Explanation: The printf(“ percent s”, stack[top++]) statement performs a pop, but top is incremented, which is incorrect. To properly pop an operand and retain stack, the statement stack[top++] should be replaced with stack[top–].

7. What will be the output of the following program?

main()  
{  
   char str[]="san foundry";  
   int len = strlen(str);  
   int i;  
 
   for(i=0;i<len;i++)  
        push(str[i]);  // pushes an element into stack
 
   for(i=0;i<len;i++)  
      pop();  //pops an element from the stack
}

A) sanfoundry
B) san foundry
C) yrdnuof nas
D) foundry nas

Explanation:

The string ‘san foundry’ is first placed into the stack one by one.
The performance will be ‘yrdnuof nas’ when it is popped.

8. Reversing a word using stack can be used to find if the given word is a palindrome or not.
A) True
B) False
Explanation: This stack application can also be used to determine whether a given word is a palindrome, so if the reversed word is the same as the first, the given word is a palindrome.

9. Which is the most appropriate data structure for reversing a word?
A) queue
B) stack
C) tree
D) graph

Explanation: Since stack follows the LIFO theory, it is the best data structure for reversing a term.

10. Operations required for reversing a word or a string using stack are push() and pop().
A) True
B) False

Explanation: The push operation adds a character to the stack, while the pop operation removes the stack’s end.

11. What is the time complexity of reversing a word using stack algorithm?
A) O (N log N)
B) O (N2)
C) O (N)
D) O (M log N)

Explanation: Mathematically, the time complexity of reversing a stack is O (N), where N is the data.

12. What will be the word obtained if the word “abbcabb” is reversed using a stack?
A) bbabbca
B) abbcabb
C) bbacbba
D) bbacabb

Explanation: On the stack, the string “abbcabb” is moved. If you pop the characters one by one, you’ll get the word bbacbba.

Push and pop are the two key operations of a stack, which is first in first out. Data is inserted into it via push, and data is retrieved via pop. To reverse an array using stack, first push all elements into the stack with the push() method, then retrieve them into another array with the pop() method. A stack overload occurs when too much memory is allocated to the call stack. Stack overflow may also apply to: Stack Overflow, a computer programming question-and-answer website.

Leave a Reply

Your email address will not be published. Required fields are marked *