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.

    1. Explain static vs. dynamic.
    2. Give an example of when these two words might be used.
    1. Explain syntax and semantics.
    2. How is the syntax of a programming language typically specified?
    1. What do we mean by a language?
    2. When we are talking about a language, what do we mean by alphabet?
    3. True or false: a programming language is a kind of language.
  1. Consider the following grammar, which has start symbol S.

    S   →   XY
    X   →   Xab
      |   ab
    Y   →   c
    1. Based on this grammar, write a leftmost derivation for the string ababc
    2. Draw a parse tree for ababc based on this grammar.
    3. Based on this grammar, write a rightmost derivation for the string ababc
    4. What language is generated by this grammar?
    5. Write a regular expression that generates the same language as this grammar.
    6. Draw the diagram of a DFA that recognizes the language that this grammar generates.
    7. True or false: this grammar is regular.
    8. True or false: the language generated by this grammar is a regular language.
    9. True or false: this grammar is context-free.
  2. True or false.
    1. Every language is generated by some grammar.
    2. Every regular language is recognized by some DFA.
    3. Every context-free language is regular.
    1. True or false: A grammar is ambiguous if some string in the language it generates has multiple derivations.
    2. True or false: Ambiguity is a desirable property in a grammar.
    3. Consider the grammar with start symbol S and one production: S → SS | a. Is this grammar ambiguous? How do you know?
    1. Consider the language consisting of the letter a followed by zero or more letters, each of which is either b or c. Write a BNF grammar that generates this language.
    2. True or false: It is common for modern programming languages to have their syntax specified by BNF, EBNF, or some similar standard.
    3. True or false: In grammars, typically brackets surround optional sections, and braces surround optional, repeatable sections.
    4. True or false: The dangling “else” problem is a common example of ambiguity in grammars specifying PL syntax.
  3. What is a programming language?
  4. True or false.
    1. A programming language that is executed by converting it to a byte code and then interpreting that, is not actually a compiled language; a true compiler always targets machine code.
    2. An interpreter is software that takes code in some programming language and executes it.
    3. An AST is a common example of an IR used in compilation.
    4. In Just-in-Time (JIT) compilation, code is compiled just before writing it to an executable file.
  5. True or false: Lua is an example of a PL whose existence is largely due to restrictive trade laws.
  6. Describe Lua’s type system.
    1. Static or dynamic?
    2. Mostly manifest or mostly implicit?
    3. Fixed set of types or extensible?
    4. Types apply to variables?
    1. True or false: Lua is oriented primarily toward imperative programming.
    2. What keyword—if any—introduces a Lua function?
    3. True or false: Lua has first-class functions.
    4. True or false: Lua allows for lambda functions.
    5. True or false: By default, Lua does lazy evaluation.
    6. True or false: Lua determines the suitability of function arguments using duck typing.
  7. Explain truthy and falsy.
    1. Lua includes only one building block for data structures. What is it?
    2. In Lua, the term array does not refer to the same thing it generally refers to in other programming languages. What is a Lua array?
    1. True or false. Ordinary Lua variables declared inside a function are always local variables.
    2. What keyword or function do we use to import a module in Lua?
    1. What is a Lua metatable?
    2. Give two examples of what Lua metatables are used for.
    3. The special functions in Lua metatables all have names beginning with what?
    1. Explain what the Lua colon operator does.
    2. How is the Lua colon operator helpful when doing object-oriented programming Lua?
    1. What is a closure?
    2. In PLs with closures, we can use a closure as a replacement for what? Be specific.
    1. What is a coroutine?
    2. What word is normally used for passing a value out of a coroutine in a way that allows for other values to be so passed in the future?
    3. True or false: the concept of a coroutine is specific to Lua.
    4. Lua makes coroutines available through what standard-library module?
    1. What type of value is a Lua iterator? Hint. “Type” is a technical term.
    2. What Lua flow-of-control construct makes use of iterators?

Answers

    1. Dynamic refers to things that happen at runtime, that is, when a program is executing. Static refers to things that happen before that.
    2. 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.
    1. Syntax refers to the (correct) structure of code. Semantics refers to the meaning of code.
    2. The syntax of a programming language is usually specified, at least in part, using a grammar.
    1. A (formal) language is a set of strings.
    2. A language’s alphabet is the set of all characters that can be used in strings in the language.
    3. False. A programming language is not a (formal) language.
    1. Leftmost derivation:

      S
      XY
      XabY
      ababY
      ababc

    2. The parse tree is shown below.
      Image not displayed: Parse tree for string ababc
    3. Rightmost derivation:

      S
      XY
      Xc
      Xabc
      ababc

    4. The language generated by this grammar is the language consisting of all strings formed by one or more repetitions of ab followed by c.
    5. Regular expression: /ab(ab)*c/ OR /(ab)+c/.
    6. Here is the diagram of a DFA that recognizes the proper language.
      Image not displayed: DFA diagram
      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.
    7. False. This is not a regular grammar.
    8. True. Since it is generated by a regular expression (and recognized by a DFA), the language generated by this grammar is a regular language.
    9. True. This is a context-free grammar.
    1. False. There are languages that are not generated by a grammar.
    2. True. Every regular language is recognized by some DFA.
    3. False. Rather, every regular language is context-free.
    1. False. Rather, a grammar is ambiguous if some string in the language it generates has multiple parse trees.
    2. False. Ambiguity is an undesirable property in a grammar.
    3. 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.
      Image not displayed: Two parse trees for string aaa
    1. BNF grammar:
      <string>   ::=  "a" | "a" <other>
      <other>   ::=  <bc-char> | <bc-char> <other>
      <bc-char>   ::=  "b" | "c"

      Other answers are possible.

    2. True. It is common for modern programming languages to have their syntax specified by BNF, EBNF, or some similar standard.
    3. True. In grammars, typically brackets surround optional sections, and braces surround optional, repeatable sections.
    4. True. The dangling “else” problem is a common example of ambiguity in grammars specifying PL syntax.
  1. A programming language is a notation for specifying computations.
    1. False. A compiler translates code from one PL to another. It is not required to target machine code.
    2. True. An interpreter is software that takes code in some programming language and executes it.
    3. True. An Abstract Syntax Tree is a common example of an Intermediate Representation used in compilation.
    4. False. In Just-in-Time (JIT) compilation, code is compiled at runtime.
  2. True. Lua is an example of a PL whose existence is largely due to restrictive trade laws.
    1. Lua has dynamic typing.
    2. Lua’s typing is largely implicit.
    3. Lua has a fixed set of types.
    4. Lua types do not apply to variables, but only to the values they take on.
    1. True. Lua is oriented primarily toward imperative programming.
    2. A Lua function is introduced by the keyword function.
    3. True. Lua has first-class functions.
    4. True. Lua allows for lambda functions.
    5. False. Lua defaults to eager evaluation.
    6. True. Lua determines the suitability of function arguments using duck typing.
  3. 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.
    1. Lua’s building block for data structures is the table: a hash table that may include both keys and associated values of differing types.
    2. In Lua, the term array means a table (hash table) whose keys are consecutive integers from 1 to the table size.
    1. False. Ordinary Lua variables declared inside a function default to being global variables.
    2. In Lua, we import a module using the standard-library function require.
    1. A Lua metatable is a table that has a special relationship with some other table. For example, table mt could be the metatable for table t. In certain circumstances, functions in the metatable (here, mt) are called to implement operations involving the table (here, t).
    2. 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.
    3. The special functions in Lua metatables all have names beginning with two underscores (__).
    1. 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 call t:ff(a,b) is the same as t.ff(t,a,b).
    2. 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.
    1. A closure is a function that carries with it a reference to or copy of (part of) the environment in which it was created.
    2. 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.
    1. A coroutine is a function that can temporarily give up control and later be resumed.
    2. The word is yield. A coroutine can yield a value and later be resumed.
    3. False. Coroutines are available in a number of programming languages other than Lua, including Go, Python, and C++.
    4. Lua makes coroutines available through its standard-library coroutine module.
    1. A Lua iterator has type function.
    2. Lua iterators are used by its for ... in construct.