Balanced Parenthesis Multiple Choice Questions and Answers (MCQs)

Computer Science & Engineering Data Structures & Algorithms

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

1. Which of the following statement is incorrect with respect to balancing symbols algorithm?
A) {[()]}
B) ([ )]
C) {( )}
D) { [ ] }

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

2. 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

Explanation: It is moved onto the stack when an opening bracket/braces/parentheses is encountered. Throw an error if the corresponding end bracket/braces/parentheses cannot be found.

3. 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

Explanation: If the corresponding end bracket/braces/parentheses cannot be found, an error is thrown because they do not fit.

4. 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

Explanation: The stack is popped when the corresponding end bracket/braces/parentheses are encountered. It is moved onto the stack when an opening bracket/braces/parentheses is encountered.

5. An error is reported when the stack is not empty at the end.
A) True
B) False

Explanation: The presence of elements at the end of the stack indicates that the given string of parentheses is unbalanced.

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

Explanation: The provided statement is incorrect in terms of symbol matching because the last bracket does not match the opening braces.

7. How many passes does the balancing symbols algorithm makes through the input?
A) one
B) two
C) three
D) four

Explanation: Since the balancing symbols algorithm is linear, it only allows one pass through the input.

8. 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]}

Explanation: Since the last close brace is absent from the argument, ((A+B) + (C+D) is invalid.

9. 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)

Explanation: Mathematically, the time complexity of the balancing parentheses algorithm is considered to be O. (N).

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

Explanation: Reversing stack reverses a stack while balancing symbols ensures that the pair of parentheses are correctly nested.

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

Explanation: The compiler reads every string from left to right rather than right to left.

12. Which is the most appropriate data structure for applying balancing of symbols algorithm?
A) stack
B) queue
C) tree
D) graph

Explanation: Since stack follows the LIFO principle, it is the best data structure for the balancing symbols algorithm (Last In First Out).

13. 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

Explanation: The stack-based balancing symbols algorithm only balances parentheses, brackets, and braces, not any other symbols.

A balanced String has the same number of opening and closing matched brackets on both ends. In other words, balanced strings are expressions that have corresponding opening and closing brackets. Each opening symbol has a corresponding closing symbol, and the pairs of parentheses are properly nested in balanced parentheses. If the current character is a beginning bracket (‘(‘, “, or ‘[‘), stack it. If the current character is a closing bracket (‘) or ” or ‘]’), pop from the stack; if the popped character is the corresponding starting bracket, all is well; otherwise, the brackets are unbalanced.

Leave a Reply

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