Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

Which of the following is TRUE?(a) Both P and Q are true(b) P is true and Q is false(c) P is false and Q is true(d) Both P and Q are falseThe question was asked during an internship interview.I'd like to ask this question from Top-Down Parsing in portion Topdown Parsing of Compiler

Answer»

The correct ANSWER is (C) P is false and Q is true

Easiest explanation: Ambiguity can be SEEN inregular GRAMMAR

S → aA/a

A → aA/ε

In above grammar, STRING ‘a’ has two leftmost

derivations.

S → aA

S → a

S->a (using A->ε).

2.

In a bottom-up evaluation of a syntax directed definition its inherited attributes can do which of the following?(a) Always evaluated(b) Can beevaluatedif the definition is L attributed(c) Can beevaluatedif the definition has synthesized attributes(d) Never be evaluatedI got this question in semester exam.My enquiry is from Top-Down Parsing topic in chapter Topdown Parsing of Compiler

Answer»

Right answer is (b) Can beevaluatedif the definition is L attributed

For EXPLANATION I would SAY: A Syntax Directed Definition (SDD) is called S Attributed if it has only synthesized attributes. Also the L-Attributed Definitions contain both synthesized and INHERITED attributes but do not NEED to build a dependency graph to evaluate them.

3.

The process of assigning load addresses to the various parts of the program and adjusting the code and data in the program to reflect the assigned addresses is called?(a) Assembly(b) Parsing(c) Relocation(d) Symbol resoluteThe question was asked by my school principal while I was bunking the class.Origin of the question is Top-Down Parsing in chapter Topdown Parsing of Compiler

Answer»

Correct option is (c) Relocation

To explain: Relocation is the PROCESS of replacing symbolic references or names of libraries with actual USABLE addresses in memory before running a PROGRAM. Linker performs it during compilation.

4.

Which of the following derivations does a top-down parser use while parsing an input string?(a) Leftmost derivation(b) Leftmost derivationin reverse(c) Rightmost derivation(d) Rightmost derivation in reverseI had been asked this question during an interview.This question is from Top-Down Parsing topic in portion Topdown Parsing of Compiler

Answer»

Right OPTION is (a) Leftmost DERIVATION

The best explanation: In top down parser takes INPUT from Left to right CONSTRUCTING leftmost derivation of the SENTENCE.

5.

Which of the following describes a handle (as applicable to LR-parsing) appropriately?(a) Position wherenext reduce orshiftoperation will occur(b) The next step has use of Non-terminal for reduction(c) Used for reduction in a coming-upstep along with a position in the sentential form where the next shift or reduce operation will occur(d) Used in the next step for reduction along with a position in the sentential form where the right hand side of the production may be foundThe question was asked in exam.Query is from Top-Down Parsing in chapter Topdown Parsing of Compiler

Answer»

Correct ANSWER is (d) Used in the next step for reduction along with a POSITION in the sentential FORM where the right HAND side of the production MAY be found

For explanation I would say: the next step in LR parsing shall have a Reduction.

6.

Which of the following suffices to convert an arbitrary CFG to an LL(1) grammar?(a) Removing left Recursive alone(b) Factoring the grammar alone(c) Along with removing left recursion we also perform the factoring of the grammar(d) None of the mentionedI have been asked this question in examination.Query is from Top-Down Parsing topic in chapter Topdown Parsing of Compiler

Answer»

Right OPTION is (d) None of the mentioned

To explain: REMOVING LEFT recursion and factoring the grammar do not suffice to convert an ARBITRARY CFG to LL(1) grammar.

7.

Which of the following statements is false?(a) Unambiguous grammar has both kind of derivations(b) An LL(1) parser is a top-down parser(c) LALR is more powerful than SLR(d) Ambiguous grammar can’t be LR(k)This question was posed to me by my college director while I was bunking the class.The above asked question is from Predictive Top-Down Parsing topic in section Topdown Parsing of Compiler

Answer»

The correct OPTION is (a) Unambiguous grammar has both kind of derivations

Easiest EXPLANATION: If a grammar has more than one leftmost (or RIGHTMOST) derivation the grammar is AMBIGUOUS.

8.

Which of the following statements is false?(a) Left as well as right most derivations can be in Unambiguous grammar(b) An LL (1) parser is a top-down parser(c) LALR is more powerful than SLR(d) Ambiguous grammar can’t be LR (k)I had been asked this question in exam.The question is from Top-Down Parsing in section Topdown Parsing of Compiler

Answer»

The correct ANSWER is (a) Left as well as right most derivations can be in Unambiguous grammar

To ELABORATE: If a grammar has more than ONE leftmost (or rightmost) derivation the grammar is AMBIGUOUS. Sometimes in unambiguous grammar the rightmost derivation and leftmost derivations may DIFFER.

9.

In the context of abstract-syntax-tree and control-flow-graph. Which one of the following is true?(a) In both AST and CFG if node N2 be the successor of node N1(b) For any input program, neither AST nor CFG will contain a cycle(c) The max number of successors of a node in an AST and a CFG depends on the input program(d) None of the mentionedI got this question in exam.This question is from Predictive Top-Down Parsing topic in chapter Topdown Parsing of Compiler

Answer»

Right ANSWER is (c) The MAX number of successors of a node in an AST and a CFG DEPENDS on the INPUT program

For explanation I would SAY: Successors depends on input .

10.

Compute E.value for the root of the parse tree for the expression:2 # 3 & 5 # 6 &4.(a) 200(b) 180(c) 160(d) 40The question was posed to me at a job interview.This question is from Top-Down Parsing topic in section Topdown Parsing of Compiler

Answer»

Correct ANSWER is (c) 160

The best explanation: Higher precedence operator will never produce an expression with operator with LOWER precedence.&># in terms of precedence ORDER.

11.

Which of the following pairs is the most powerful?(a) SLR, LALR(b) Canonical LR ,LALR(c) SLR canonical LR(d) LALR canonical LRI had been asked this question at a job interview.Enquiry is from Predictive Top-Down Parsing in chapter Topdown Parsing of Compiler

Answer»

Right ANSWER is (c) SLR canonical LR

To elaborate: parser ALGORITHM is simple.

12.

Consider a program P that consists of two source modules M1(contains reference to a function defined in M2) and M2 contained in two different files.(a) Edit time(b) Compile time(c) Link time(d) Load timeI had been asked this question in my homework.Query is from Top-Down Parsing topic in section Topdown Parsing of Compiler

Answer»

The correct choice is (c) Link time

To EXPLAIN I would say: Compiler transforms source code into the MACHINE language which is in binary.

Kinds of OBJECT codes:

i. Defined symbols, which allow it to be called by other modules,

ii. Undefined symbols, which call the other modules where these symbols are defined, and

iii. Symbols which are used internally withinobject FILE for relocation.

13.

Which one of the following is true at any valid state in shift-reduce parsing?(a) At the bottom we find the prefixes(b) None of the mentioned(c) Stack contains only viable prefixes(d) Stack consists of viable prefixesI got this question at a job interview.Enquiry is from Predictive Top-Down Parsing topic in division Topdown Parsing of Compiler

Answer» RIGHT answer is (C) Stack CONTAINS only viable prefixes

To elaborate: The prefixes on the stack of a shift-reduce parser are CALLED viable prefixes.
14.

Which one of the following is a top-down parser?(a) Recursive descent parser(b) Operator precedence parser(c) An LR(k) parser(d) An LALR(k) parserThe question was asked during an interview.My question is taken from Top-Down Parsing in section Topdown Parsing of Compiler

Answer» RIGHT option is (a) Recursive descent parser

The best explanation: Recursive Descent also known as TOP down PARSING also known to be LL(1).

 Consider the following two statements:

P: Every regular grammar is LL(1)

Q: Regular is LR(1) grammar.
15.

Assume that the SLR parser for a grammar G has n1 states and the LALR parser for G has n2 states.(a) n1 is necessarily less than n2(b) n1 is necessarily equal to n2(c) n1 is necessarily greater than n2(d) none of the mentionedI got this question in unit test.The origin of the question is Top-Down Parsing topic in division Topdown Parsing of Compiler

Answer»

Right choice is (b) n1 is necessarily EQUAL to n2

Best EXPLANATION: SLR PARSER has less range of context free languages than LALR but still both n1 & n2 are same for SLR & LALR respectively.

16.

The grammar A → AA | (A) | e is not suitable for predictive-parsing because the grammar is?(a) Ambiguous(b) Left recursive(c) Right recursive(d) An operator grammarThis question was posed to me during an interview.The origin of the question is Top-Down Parsing topic in division Topdown Parsing of Compiler

Answer»

Correct CHOICE is (B) Left recursive

For explanation I WOULD say: A ::= A a

| bis the left recursive language.

17.

Which of the following suffices to convert an arbitrary CFG to an LL(1) grammar?(a) Removing left recursion only(b) Factoring the grammar alone(c) Factoring & left recursion removal(d) None of the mentionedThe question was posed to me in an online quiz.This intriguing question originated from Top-Down Parsing in section Topdown Parsing of Compiler

Answer» RIGHT OPTION is (d) NONE of the mentioned

The explanation: Factoring as well as left recursion removal do not SUFFICE to CONVERT an arbitrary CFG to LL(1) grammar.