Abstract Syntax Tree Multiple Choice Questions and Answers (MCQs)

This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses on “Abstract Syntax Tree”.

1. Which tree traversal technique is used to evaluate the expression represented by syntax tree?
a) Pre-order traversal
b) In-order traversal
c) Post-order traversal
d) Breadth first traversal
View Answer

Answer: c
Explanation: Post-order traversal is used to navigate the syntax tree in order to evaluate the expression represented by the tree. It starts from the root node and visits the children of each node from left to right recursively. It is a special case of depth first traversal.

2. Which pattern is used to evaluate abstract syntax tree?
a) Visitor pattern
b) Singleton Pattern
c) Proxy pattern
d) Adapter pattern
View Answer

Answer: a
Explanation: Visitor pattern allows you to add methods to classes of different types without alteration to those classes. It also allows you to define external classes that can extend other classes. Visitor pattern is used to evaluate abstract syntax tree.

3. Abstract syntax tree is an input to which phase of compiler?
a) Lexical analysis
b) Syntax Analysis
c) Semantic Analysis
d) Intermediate code generation
View Answer

Answer: c
Explanation: An abstract syntax tree is the output of the parser, the second phase i.e. syntax analysis and it forms an input to semantic analysis phase of the compiler. To determine semantic correctness of declarations and statements written in program, semantic analysis is performed.
advertisement
advertisement

4. Which of the following expression can be represented by the given syntax tree?
Find the expression from the given syntax tree
a) 7 + (3 * 5)
b) 7 * (3 + 5)
c) (7 * 3) + 5
d) (7 + 3) * 5
View Answer

Answer: a
Explanation: Step-1: Evaluating the left sub-tree: Since, no operation is performed the value is 7.
Step-2: Evaluating the right sub-tree: (3 * 5)
Step-3: Applying the root operation: 7 + (3 * 5)

5. In an abstract syntax tree, each internal node represents an operand and each leaf node represents an operator.
a) True
b) False
View Answer

Answer: b
Explanation: For an expression represented by an abstract syntax tree, each internal node represents an operator. The operands of the operator are represented by the children of the node. A grammar rule can’t be represented by an internal node in syntax tree.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

6. Consider the given syntax tree. Assume that division has the lowest precedence than addition, subtraction and multiplication. Addition and subtraction have the same precedence but more than multiplication. Which of the following expression can be given by the following syntax tree?
Determine the expression from the given syntax tree
a) [5 / (p + q) * (q – r)]
b) [5 * (p + q) / (q – r)]
c) [5 + (p + q) * (q – r)]
d) [5 / (p + q) + (q – r)]
View Answer

Answer: a
Explanation: The root represents the operator /. The subtrees of the root represent the subexpressions 5, (p + q) and (q – r). The grouping of (p + q) and (q – r) as an operand reflects the left to right evaluation of the operators at the same precedence level. Since +, – have the same precedence and * has higher precedence p + q * q – r is equivalent to (p + q) * (q – r). The correct expression is [5 / (p + q) * (q – r)].

7. Syntax trees are comparatively denser than parse trees.
a) True
b) False
View Answer

Answer: a
Explanation: Syntax trees are called abstract syntax trees because they do not represent all the precise information from the real syntax of the program. Syntax trees are dense in comparison with parse tree, for constructing the same language.
advertisement

8. Assume +, -, *, / are usual arithmetic operator, + has highest precedence and right associative and -, *, / have equal precedence and left associative. What is the output of the expression 23 * 3 – 6 + 11 – 2 * 8 / 4?
a) 110
b) 100
c) 130
d) 120
View Answer

Answer: b
Explanation: Parenthesizing the given expression we get (((((23 * 3) – (6 + 11)) – 2 ) * 8) / 4).
Equivalent postfix expression is: 23 3 * 6 11 + – 2 – 8 * 4 /
Evaluation can be done as follows-
• Start reading the expression from left to right, and perform the following steps-
1.If operand comes, push into the stack.
2.If operator comes, pop the topmost 2 elements from the stack and perform the operation between them.
3.Whatever result we get, push it again into the stack.
By following the above procedure, we get-
Find the output of the expression 23 * 3 – 6 + 11 – 2 * 8 / 4 from the given diagram
Hence, the output of the given expression is 100.

9. Which of the following is an application of abstract syntax tree?
a) Static code analyzing
b) Implement indexing in databases
c) Used for dynamic memory allocation
d) To represent networks of communication
View Answer

Answer: a
Explanation: Abstract syntax trees are used to analyze static code whereas B-trees are used to implement indexing in the database. Dynamic memory allocation is the application of linked list and graph is used to represent networks of communication.
advertisement

10. Which parsing approach is followed by syntax tree?
a) Top-down parsing
b) Bottom-up parsing
c) Predictive parsing
d) Shift reduce parsing
View Answer

Answer: b
Explanation: Abstract Syntax Tree follows bottom-up parsing, it is the process of reducing the input string to start symbol. The syntax tree is constructed starting from the non-terminals and ends on the start symbol. To reach the start symbol, we apply production rules to find a rightmost derivation of string in reverse manner.

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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). 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!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.