



















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















































Question 1. Which notation is used to formally describe the syntax of a programming language? A) Regular Expressions B) Backus-Naur Form (BNF) C) Lambda Calculus D) Hoare Logic Answer: B Explanation: BNF (or its extension EBNF) provides a formal way to specify the grammar of a language, defining how strings of symbols form valid programs. Question 2. In the Chomsky hierarchy, which class of languages can be recognized by a deterministic push-down automaton? A) Regular B) Context-free C) Context-sensitive D) Recursively enumerable Answer: B Explanation: Deterministic push-down automata recognize deterministic context-free languages, a proper superset of regular languages. Question 3. The “dangling else” problem is an example of which grammatical issue? A) Left recursion B) Ambiguity C) Non-terminating derivation
D) Unreachable production Answer: B Explanation: The grammar allows two different parse trees for a nested if-else statement, making it ambiguous. Question 4. Which of the following is a characteristic of small-step operational semantics? A) It evaluates whole expressions in one rule. B) It describes execution as a sequence of atomic transitions. C) It maps programs directly to mathematical functions. D) It only applies to functional languages. Answer: B Explanation: Small-step semantics (reduction semantics) models execution as a series of individual computation steps. Question 5. In big-step (natural) semantics, the judgment “⟨e, σ⟩ ⇓ v” means: A) Expression e reduces to value v in one step. B) Under store σ, expression e evaluates to value v after possibly many steps. C) e diverges (does not terminate). D) e is syntactically invalid. Answer: B Explanation: Big-step semantics relates an expression directly to its final value, abstracting intermediate steps.
D) Boolean values. Answer: B Explanation: Integer literals denote elements of the integer domain ℤ. Question 9. What property ensures that the meaning of a composite expression can be built from the meanings of its parts? A) Confluence B) Determinism C) Compositionality D) Termination Answer: C Explanation: Compositionality is a core principle of denotational semantics, guaranteeing modular reasoning. Question 10. Which theorem is commonly used to define the semantics of recursive functions via fixpoints? A) Gödel’s Incompleteness Theorem B) Knaster-Tarski Fixpoint Theorem C) Curry-Howard Isomorphism D) Church-Rosser Property Answer: B Explanation: The Knaster-Tarski theorem guarantees the existence of least fixpoints in complete lattices, used for recursion.
**Question 11. Continuations are primarily used to model which kind of control flow? ** A) Sequential execution only. B) Local variable scoping. C) Non-local jumps such as exceptions and goto. D) Type inference. Answer: C Explanation: Continuations capture “the rest of the computation,” allowing representation of jumps, exceptions, and coroutines. Question 12. The progress theorem for a type system states that: A) Types never change during execution. B) A well-typed program never gets stuck; it either is a value or can take a step. C) Every program can be assigned a type. D) Types are preserved under substitution. Answer: B Explanation: Progress guarantees that well-typed programs are either values or can make a reduction step. Question 13. The preservation theorem (type safety) asserts that: A) Evaluation always terminates. B) Types are invariant under program reduction. C) All variables are initialized before use. D) The type checker runs in polynomial time. Answer: B
C. Unification algorithm D. Quick-sort Answer: C Explanation: The Hindley-Milner algorithm performs type inference by repeatedly unifying type equations. Question 17. A finite state automaton (FSA) is primarily used in which compiler phase? A. Code generation B. Lexical analysis (scanning) C. Register allocation D. Loop optimization Answer: B Explanation: FSAs recognize token patterns defined by regular expressions during lexical analysis. Question 18. Which of the following regular expression constructs cannot be directly expressed in a deterministic finite automaton without additional states? A. Concatenation B. Kleene star (*) C. Union (|) D. Look-ahead assertions Answer: D Explanation: Look-ahead requires memory of upcoming input, which DFAs cannot provide without augmenting the automaton.
Question 19. An LL(1) parser is a type of: A. Bottom-up parser B. Top-down parser that uses one token of lookahead C. Parser that constructs LR tables D. Parser that works only on left-recursive grammars Answer: B Explanation: LL(1) parsers read input Left-to-right, produce a Leftmost derivation, and need a single lookahead token. Question 20. Which parsing technique is most suitable for handling left-recursive grammars without transformation? A. Recursive descent (LL) B. LR parsing (bottom-up) C. Packrat parsing D. Predictive parsing with backtracking Answer: B Explanation: LR parsers naturally handle left recursion, whereas LL parsers require grammar transformation. Question 21. In shift-reduce parsing, a “shift” action does what? A. Reduces a handle on the stack. B. Pushes the next input token onto the stack. C. Pops the stack completely. D. Accepts the input.
C. Perform constant folding. D. Manage heap allocation at runtime. Answer: B Explanation: Symbol tables map identifiers to information such as type, scope level, and storage location. Question 25. Three-address code (TAC) typically represents an instruction in the form: A. op dest, src1, src B. label: instruction C. push value D. call function Answer: A Explanation: TAC uses at most three operands (two sources and a destination) to simplify later optimization and translation. Question 26. In SSA (Static Single Assignment) form, each variable is assigned: A. Exactly once, and every use refers to that unique definition. B. Multiple times, but only within a single basic block. C. Only in the entry block. D. Only after loop unrolling. Answer: A Explanation: SSA requires each variable to have a single definition point, facilitating data-flow analyses.
Question 27. Which optimization eliminates computations that produce the same value more than once? A. Dead code elimination B. Loop unrolling C. Common subexpression elimination (CSE) D. Constant propagation Answer: C Explanation: CSE detects identical expressions whose values are already computed and reuses the previous result. Question 28. Dead code elimination removes: A. Code that never executes (unreachable) or whose results are never used. B. All recursive calls. C. All loops. D. Only comments. Answer: A Explanation: Dead code is code that has no effect on observable program behavior and can be safely removed. Question 29. A loop invariant is: A. A variable that never changes throughout the program. B. A condition that holds true before and after each iteration of a loop. C. The final value of a loop counter. D. The termination condition of a loop. Answer: B
B. Local variables C. Global symbol table D. Dynamic link (pointer to previous activation record) Answer: C Explanation: The global symbol table is not part of an activation record; it resides elsewhere. Question 33. Caller-save registers are: A. Preserved by the callee across a function call. B. Saved by the caller before invoking a function. C. Never used in modern ABIs. D. Only used for floating-point values. Answer: B Explanation: Caller-save registers must be saved by the calling function if it wants to preserve their values across the call. Question 34. Which garbage-collection algorithm works by periodically stopping the world and tracing reachable objects? A. Reference counting B. Mark-and-sweep C. Copying collection D. Generational GC only Answer: B Explanation: Mark-and-sweep pauses execution, marks reachable objects, then sweeps unmarked ones.
Question 35. Reference counting fails to reclaim memory in the presence of: A. Large objects B. Cyclic data structures C. Immutable strings D. Stack-allocated variables Answer: B Explanation: Cycles keep each object’s reference count > 0, preventing reclamation despite being unreachable from roots. Question 36. Which of the following is a regular language? A. { a^n b^n | n ≥ 0 } B. { ww | w ∈ {0,1}* } C. { a^* b } D. { a^n b^m c^{n+m} | n,m ≥ 0 } Answer: C Explanation: a* b can be recognized by a finite automaton; the others require memory beyond regular capabilities. Question 37. In a context-free grammar, left recursion can be eliminated by: A. Adding more terminals. B. Converting to right recursion using a new non-terminal. C. Removing all productions. D. Adding semantic actions.
B. int → int C. bool → bool D. α → β Answer: A Explanation: The identity function works for any type α, yielding the polymorphic type α → α. Question 41. Which property distinguishes a context-sensitive language from a context-free language? A. Its grammar may have productions where the left side contains more than one non-terminal. B. It can be recognized by a finite automaton. C. It always has ambiguous grammars. D. It cannot be parsed by any algorithm. Answer: A Explanation: Context-sensitive grammars allow productions of the form αAβ → αγβ, where the context around A matters. Question 42. The “first” set of a non-terminal in LL parsing is used to: A. Determine the follow set. B. Predict which production to use based on the next input token. C. Compute the parse tree depth. D. Generate code for loops. Answer: B
Explanation: The first set contains terminals that can appear at the beginning of strings derived from the non-terminal, guiding predictive parsing. Question 43. In LR parsing, a “reduce” action replaces a handle on the stack with: A. The next input token. B. A non-terminal representing the left-hand side of a production. C. The start symbol. D. The entire stack contents. Answer: B Explanation: Reduce applies a grammar production by popping the handle (right-hand side) and pushing the corresponding non-terminal. Question 44. Which of the following is a typical advantage of using SSA form for optimization? A. It eliminates the need for register allocation. B. It makes data-flow analysis simpler because each variable has a single definition. C. It reduces code size automatically. D. It guarantees termination of loops. Answer: B Explanation: SSA’s single-assignment property simplifies reaching-definition and other data-flow analyses. Question 45. The “goto” statement in an imperative language can be modeled using: A. Continuations in denotational semantics.
Explanation: Constant folding is a code-optimization pass that evaluates constant expressions; it does not rely directly on the symbol table. Question 48. In a typical compiler pipeline, after parsing and before code generation, which transformation is usually performed? A. Lexical analysis B. Semantic analysis and intermediate representation construction C. Binary linking D. Source-level debugging Answer: B Explanation: Semantic analysis checks types and builds an IR (e.g., TAC) that is later used for optimization and code generation. Question 49. Which of the following statements about regular expressions and finite automata is true? A. Every context-free language can be expressed as a regular expression. B. For every regular expression there exists an equivalent deterministic finite automaton (DFA). C. DFAs can recognize languages that require unlimited memory. D. Regular expressions cannot describe identifiers. Answer: B Explanation: Regular expressions and DFAs are equivalent in expressive power for regular languages. Question 50. The “follow” set of a non-terminal is needed in LL parsing to: A. Detect left recursion.
B. Decide when to apply a reduction. C. Determine which tokens can appear immediately after the non-terminal in some derivation. D. Compute the height of the parse tree. Answer: C Explanation: Follow sets help the parser know when a non-terminal’s derivation is complete. Question 51. In the context of abstract machines, what does the “store” map? A. Tokens to lexical categories. B. Variable identifiers to memory locations (addresses). C. Machine instructions to opcodes. D. Functions to their call graphs. Answer: B Explanation: The store (or heap) maps locations to values, while the environment maps identifiers to locations. Question 52. Which of the following best describes a “type error” detected by static analysis? A. A runtime null-pointer dereference. B. An attempt to add an integer and a string in a statically typed language. C. A segmentation fault caused by illegal memory access. D. A deadlock in concurrent execution. Answer: B Explanation: Static type checking catches mismatched operations like adding incompatible types before execution.