Data Structure Questions and Answers – Reverse a Word using Stack

«
»

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

1. Reversing a word using stack can be used to find if the given word is a palindrome or not.
a) True
b) False
View Answer

Answer: a
Explanation: This application of stack can also be used to find if the given word is a palindrome because, if the reversed is same as that of the original word, the given word is a palindrome.
advertisement

2. Which is the most appropriate data structure for reversing a word?
a) queue
b) stack
c) tree
d) graph
View Answer

Answer: b
Explanation: Stack is the most appropriate data structure for reversing a word because stack follows LIFO principle.

3. Operations required for reversing a word or a string using stack are push() and pop().
a) True
b) False
View Answer

Answer: a
Explanation: Push operation inserts a character into the stack and pop operation pops the top of the stack.
advertisement
advertisement

4. 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)
View Answer

Answer: c
Explanation: The time complexity of reversing a stack is mathematically found to be O (N) where N is the input.

5. What will be the word obtained if the word “abbcabb” is reversed using a stack?
a) bbabbca
b) abbcabb
c) bbacbba
d) bbacabb
View Answer

Answer: c
Explanation: The string “abbcabb” is pushed on to the stack. If the characters are popped one by one, the word obtained will be bbacbba.
advertisement

6. How many stacks are required for reversing a word algorithm?
a) one
b) two
c) three
d) four
View Answer

Answer: a
Explanation: Only 1 stack is required for reversing a word using stack. In that stack, push and pop operations are carried out.

7. What will be result if the given stack is popped?
data-structures-questions-answers-reverse-word-stack-q7
a) pat
b) tap
c) atp
d) apt
View Answer

Answer: b
Explanation: The word ‘pat’ is pushed on to the stack. When the characters of the stack are popped one by one, the word ‘tap’ is obtained.
advertisement

8. 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
View Answer

Answer: b
Explanation: The element ‘b’ is popped out of the stack. Hence the output of the following sequence of operations will be ‘b’.
advertisement

9. 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);
View Answer

Answer: b
Explanation: During push operation, the characters ‘c’, ’a’, ’t’ are inserted into the stack and popped immediately after push.

10. 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
View Answer

Answer: a
Explanation: When a character is pushed, it stays on the top of the stack. While popping, the word occurs in reverse order since stack follows LIFO principle.

11. 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
View Answer

Answer: c
Explanation: The statement printf(“%s”, stack[top++]) does a pop, but top gets incremented which is not correct. The statement stack[top++] should be replaced with stack[top–] in order to pop an operand and maintain stack properly.

12. 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
View Answer

Answer: c
Explanation: First, the string ‘san foundry’ is pushed one by one into the stack.
When it is popped, the output will be as ‘yrdnuof nas’.

Sanfoundry Global Education & Learning Series – Data Structure.

To practice all areas of Data Structure, here is complete set of 1000+ Multiple Choice Questions and Answers.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter