Balanced Parenthesis Multiple Choice Questions and Answers (MCQs)

«
»

This set of Data Structures & Algorithms Multiple Choice Questions & Answers (MCQs) focuses on “Balanced Parenthesis”.

1. What is the time complexity of balancing parentheses algorithm?
a) O (N)
b) O (N log N)
c) O (M log N)
d) O (N2)
View Answer

Answer: a
Explanation: The time complexity of balancing parentheses algorithm is mathematically found to be O (N).
advertisement

2. Which application of stack is used to ensure that the pair of parentheses is properly nested?
a) Balancing symbols
b) Reversing a stack
c) Conversion of an infix to postfix expression
d) Conversion of an infix to prefix expression
View Answer

Answer: a
Explanation: Balancing symbols application ensures that the pair of parentheses are properly nested while reversing stack reverses a stack.

3. In balancing parentheses algorithm, the string is read from?
a) right to left
b) left to right
c) center to right
d) center to left
View Answer

Answer: b
Explanation: Any string is read by the compiler from left to right and not from right to left.
advertisement
advertisement

4. Which is the most appropriate data structure for applying balancing of symbols algorithm?
a) stack
b) queue
c) tree
d) graph
View Answer

Answer: a
Explanation: Stack is the most appropriate data structure for balancing symbols algorithm because stack follows LIFO principle (Last In First Out).

5. Which of the following does the balancing symbols algorithm include?
a) balancing double quotes
b) balancing single quotes
c) balancing operators and brackets
d) balancing parentheses, brackets and braces
View Answer

Answer: d
Explanation: The balancing symbols algorithm using stack only includes balancing parentheses, brackets and braces and not any other symbols.
advertisement

6. Which of the following statement is incorrect with respect to balancing symbols algorithm?
a) {[()]}
b) ([ )]
c) {( )}
d) { [ ] }
View Answer

Answer: b
Explanation: ([ )] is incorrect because’)’ occurs before the corresponding ‘]’ is encountered.

7. What should be done when an opening parentheses is read in a balancing symbols algorithm?
a) push it on to the stack
b) throw an error
c) ignore the parentheses
d) pop the stack
View Answer

Answer: a
Explanation: When an opening bracket/braces/parentheses is encountered, it is pushed on to the stack. When the corresponding end bracket/braces/parentheses is not found, throw an error.
advertisement

8. When the corresponding end bracket/braces/parentheses is not found, what happens?
a) The stack is popped
b) Ignore the parentheses
c) An error is reported
d) It is treated as an exception
View Answer

Answer: c
Explanation: When the corresponding end bracket/braces/parentheses is not found, throw an error since they don’t match.

9. If the corresponding end bracket/braces/parentheses is encountered, which of the following is done?
a) push it on to the stack
b) pop the stack
c) throw an error
d) treated as an exception
View Answer

Answer: b
Explanation: When the corresponding end bracket/braces/parentheses is encountered, the stack is popped. When an opening bracket/braces/parentheses is encountered, it is pushed on to the stack.
advertisement

10. An error is reported when the stack is not empty at the end.
a) True
b) False
View Answer

Answer: a
Explanation: When the stack contains elements at the end, it means that the given string of parentheses is not balanced.

11. Is the given statement ((A+B) + [C-D]] valid with respect to balancing of symbols?
a) True
b) False
View Answer

Answer: b
Explanation: The given statement is invalid with respect to balancing of symbols because the last bracket does not correspond to the opening braces.

12. How many passes does the balancing symbols algorithm makes through the input?
a) one
b) two
c) three
d) four
View Answer

Answer: a
Explanation: The balancing symbols algorithm makes only one pass through the input since it is linear.

13. Which of the following statement is invalid with respect to balancing symbols?
a) [(A+B) + (C-D)]
b) [{A+B}-{C-[D+E]}]
c) ((A+B) + (C+D)
d) {(A+B) + [C+D]}
View Answer

Answer: c
Explanation: ((A+B) + (C+D) is invalid because the last close brace is not found in the statement.

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