CS 331 Spring 2025 > Exam Review Problems, Set A
CS 331 Spring 2025
Exam Review Problems, Set A
This is the first set of exam review problems.
Problems
Review problems are given below. Answers are in the Answers section of this document. Do not turn these in.
Answers
- Dynamic refers to things that happen at runtime, that is, when a program is executing. Static refers to things that happen before that.
- One example would be static vs. dynamic typing. In static typing, types are determined and checked before a program runs. In dynamic typing, types are determined and checked while a program runs. Other answers are possible.
- Syntax refers to the (correct) structure of code. Semantics refers to the meaning of code.
- The syntax of a programming language is usually specified, at least in part, using a grammar.
- A (formal) language is a set of strings.
- A language’s alphabet is the set of all characters that can be used in strings in the language.
- False. A programming language is not a (formal) language.
-
- Leftmost derivation:
S
XY
XabY
ababY
ababc - The parse tree is shown below.
- Rightmost derivation:
S
XY
Xc
Xabc
ababc - The language generated by this grammar is the language consisting of all strings formed by one or more repetitions of ab followed by c.
- Regular expression: /ab(ab)*c/ OR /(ab)+c/.
- Here is the diagram of a DFA that recognizes the proper language.
As is common, there is a “dead” state at the bottom. Most of the logic of recognizing the language is in the row of four states at the top of the diagram.
- False. This is not a regular grammar.
- True. Since it is generated by a regular expression (and recognized by a DFA), the language generated by this grammar is a regular language.
- True. This is a context-free grammar.
- Leftmost derivation:
-
- False. There are languages that are not generated by a grammar.
- True. Every regular language is recognized by some DFA.
- False. Rather, every regular language is context-free.
-
- False. Rather, a grammar is ambiguous if some string in the language it generates has multiple parse trees.
- False. Ambiguity is an undesirable property in a grammar.
- This grammar is ambiguous, because the string aaa has multiple parse trees.
In both of them, S expands to SS.
In one of the trees, the first S then expands to SS;
in the other tree, it is the second S that is expanded.
The two parse trees are shown below.
-
- BNF grammar:
<string>::= "a" | "a" <other><other>::= <bc-char> | <bc-char> <other><bc-char>::= "b" | "c"Other answers are possible.
- True. It is common for modern programming languages to have their syntax specified by BNF, EBNF, or some similar standard.
- True. In grammars, typically brackets surround optional sections, and braces surround optional, repeatable sections.
- True. The dangling “else” problem is a common example of ambiguity in grammars specifying PL syntax.
- BNF grammar:
- A programming language is a notation for specifying computations.
-
- False. A compiler translates code from one PL to another. It is not required to target machine code.
- True. An interpreter is software that takes code in some programming language and executes it.
- True. An Abstract Syntax Tree is a common example of an Intermediate Representation used in compilation.
- False. In Just-in-Time (JIT) compilation, code is compiled at runtime.
- True. Lua is an example of a PL whose existence is largely due to restrictive trade laws.
-
- Lua has dynamic typing.
- Lua’s typing is largely implicit.
- Lua has a fixed set of types.
- Lua types do not apply to variables, but only to the values they take on.
-
- True. Lua is oriented primarily toward imperative programming.
- A Lua function is introduced by the keyword
function. - True. Lua has first-class functions.
- True. Lua allows for lambda functions.
- False. Lua defaults to eager evaluation.
- True. Lua determines the suitability of function arguments using duck typing.
- A value is truthy if it is treated as true in a Boolean context—in particular, when used as a condition in something like an if-statement or while-loop. A value if falsy if it is treated as false in such a context.
-
- Lua’s building block for data structures is the table: a hash table that may include both keys and associated values of differing types.
- In Lua, the term array means a table (hash table) whose keys are consecutive integers from 1 to the table size.
-
- False. Ordinary Lua variables declared inside a function default to being global variables.
- In Lua, we import a module using the standard-library function
require.
-
- A Lua metatable is a table that has a special relationship with some other table.
For example, table
mtcould be the metatable for tablet. In certain circumstances, functions in the metatable (here,mt) are called to implement operations involving the table (here,t). - Lua metatables are used to implement the equivalent of classes in Lua, when doing object-oriented programming. Lua tables are also used to implement operator overloading.
- The special functions in Lua metatables all have names beginning with two underscores (
__).
- A Lua metatable is a table that has a special relationship with some other table.
For example, table
-
- The Lua colon operator is much like the dot (
.) operator, except that, when colon is used, the value before the colon is inserted as a new first argument to the function after it. For example, the function callt:ff(a,b)is the same ast.ff(t,a,b). - The Lua colon operator is helpful when making the equivalent of member function calls. It gives a convenient syntax for letting a function know what object it is being called on.
- The Lua colon operator is much like the dot (
-
- A closure is a function that carries with it a reference to or copy of (part of) the environment in which it was created.
- In PLs with closures, we can use a closure as a replacement for an object that exists primarily so that a single member function can be called.
-
- A coroutine is a function that can temporarily give up control and later be resumed.
- The word is yield. A coroutine can yield a value and later be resumed.
- False. Coroutines are available in a number of programming languages other than Lua, including Go, Python, and C++.
- Lua makes coroutines available through its standard-library
coroutinemodule.
-
- A Lua iterator has type
function. - Lua iterators are used by its
for...inconstruct.
- A Lua iterator has type