Logical-Reasoning
October 12, 2023Nielit Scientist-B CS 22-07-2017
October 12, 2023Programming
Question 2 |
A program attempts to generate as many permutations as possible of the string, ‘abcd’ by pushing the characters a, b, c, d in the same order onto a stack, but it may pop off the top character at any time. Which one of the following strings CANNOT be generated using this program?
abcd | |
dcba | |
abad | |
cabd |
Question 2 Explanation:
A) push ‘a’ and pop ‘a’, push ‘b’ and pop ‘b’, push ‘c’ and pop ‘c’, and finally push ‘d’ and pop ‘d’. Sequence of popped elements will come to abcd.
B) First push abcd, and after that pop one by one. Sequence of popped elements will come to dcba.
C) push abc, and after that pop one by one. Sequence of popped elements will come to cba. Now push ‘d’ and pop ‘d’, final sequence comes to cbad.
D) This sequence is not possible because ‘a’ cannot be popped before ‘b’ anyhow.
B) First push abcd, and after that pop one by one. Sequence of popped elements will come to dcba.
C) push abc, and after that pop one by one. Sequence of popped elements will come to cba. Now push ‘d’ and pop ‘d’, final sequence comes to cbad.
D) This sequence is not possible because ‘a’ cannot be popped before ‘b’ anyhow.
Correct Answer: D
Question 2 Explanation:
A) push ‘a’ and pop ‘a’, push ‘b’ and pop ‘b’, push ‘c’ and pop ‘c’, and finally push ‘d’ and pop ‘d’. Sequence of popped elements will come to abcd.
B) First push abcd, and after that pop one by one. Sequence of popped elements will come to dcba.
C) push abc, and after that pop one by one. Sequence of popped elements will come to cba. Now push ‘d’ and pop ‘d’, final sequence comes to cbad.
D) This sequence is not possible because ‘a’ cannot be popped before ‘b’ anyhow.
B) First push abcd, and after that pop one by one. Sequence of popped elements will come to dcba.
C) push abc, and after that pop one by one. Sequence of popped elements will come to cba. Now push ‘d’ and pop ‘d’, final sequence comes to cbad.
D) This sequence is not possible because ‘a’ cannot be popped before ‘b’ anyhow.
Subscribe
Login
0 Comments