BSCS Programming Languages and Compilers 1 — Questions and Answers
Question 1: What is the first phase of a typical compiler?
- Parsing
- Code generation
- Lexical analysis (Correct answer)
- Semantic analysis
Correct answer: Lexical analysis
Lexical analysis (scanning) is the first phase, converting the character stream into a sequence of tokens.
Question 2: What does a context-free grammar (CFG) describe?
- The runtime behavior of a program
- The syntactic structure of a programming language (Correct answer)
- The memory layout of variables
- The type system of a language
Correct answer: The syntactic structure of a programming language
A CFG defines the valid syntactic structure of a language using production rules applied regardless of surrounding context.
Question 3: What is the difference between a compiled language and an interpreted language?
- Compiled languages are slower; interpreted languages are faster
- Compiled code is translated to machine code ahead of time; interpreted code is executed line by line at runtime (Correct answer)
- Compiled languages use more memory; interpreted languages do not
- Interpreted languages require an OS; compiled languages do not
Correct answer: Compiled code is translated to machine code ahead of time; interpreted code is executed line by line at runtime
Compiled languages like C++ translate the entire program to machine code before execution, while interpreters like Python execute source code line by line.
Question 4: What is dynamic typing in a programming language?
- Types are checked at compile time
- Types are determined and checked at runtime (Correct answer)
- All variables must be declared as integers
- The language uses only one data type
Correct answer: Types are determined and checked at runtime
In dynamically typed languages like Python, variable types are resolved at runtime rather than at compile time.
Question 5: What is a closure in programming?
- A function that terminates the program
- A function that captures variables from its enclosing scope (Correct answer)
- A class with private methods only
- A loop that runs a fixed number of times
Correct answer: A function that captures variables from its enclosing scope
A closure is a function bundled with the variables from its surrounding lexical scope, allowing it to access those variables after the scope ends.
Question 6: What does the symbol table store during compilation?
- Machine instructions
- Information about identifiers such as type, scope, and memory location (Correct answer)
- Runtime stack frames
- Intermediate code representations
Correct answer: Information about identifiers such as type, scope, and memory location
The symbol table tracks identifiers (variables, functions) and their attributes like type, scope, and location throughout compilation.
What is the first phase of a typical compiler?