BSCS Programming Languages and Compilers 2 — Questions and Answers
Question 1: What is tail recursion optimization?
- Replacing loops with recursive calls
- Converting a recursive call in tail position into an iterative loop to avoid stack growth (Correct answer)
- Optimizing the last line of a function
- Eliminating unused variables at the end of a function
Correct answer: Converting a recursive call in tail position into an iterative loop to avoid stack growth
Tail call optimization converts a tail-recursive function into an iterative process, preventing stack overflow for deep recursion.
Question 2: What does BNF (Backus-Naur Form) represent?
- A runtime memory model
- A notation for expressing context-free grammars (Correct answer)
- A binary data encoding format
- A network protocol specification
Correct answer: A notation for expressing context-free grammars
BNF is a metalanguage notation used to formally specify the syntax of programming languages using production rules.
Question 3: What is the purpose of intermediate code in a compiler?
- To speed up lexical analysis
- To provide a machine-independent representation between source and target code (Correct answer)
- To store the symbol table
- To manage memory allocation at runtime
Correct answer: To provide a machine-independent representation between source and target code
Intermediate code (e.g., three-address code or IR) decouples the front end from the back end, enabling portability and optimization.
Question 4: Which paradigm treats computation as the evaluation of mathematical functions and avoids changing state?
- Procedural programming
- Object-oriented programming
- Functional programming (Correct answer)
- Logic programming
Correct answer: Functional programming
Functional programming emphasizes pure functions, immutability, and avoidance of side effects, modeled after mathematical functions.
Question 5: What is type inference in programming languages?
- Casting one type to another explicitly
- The compiler automatically deduces the type of an expression (Correct answer)
- Using dynamic dispatch for method calls
- A runtime type-checking mechanism
Correct answer: The compiler automatically deduces the type of an expression
Type inference allows the compiler to deduce variable and expression types without explicit type annotations from the programmer.
Question 6: What is lexeme in the context of lexical analysis?
- A grammar production rule
- The actual sequence of characters in the source that matches a token pattern (Correct answer)
- A semantic error in the source code
- A type annotation in the source
Correct answer: The actual sequence of characters in the source that matches a token pattern
A lexeme is the actual string of characters matched by the scanner, while the token is the abstract category it belongs to.
What is tail recursion optimization?