LMU ☀️ CMSI 3801
LANGUAGES AND AUTOMATA I
Exam 1 Answers
  1. Atomic values include symbols, characters, and one-dimensional (scalar) numbers. Complex numbers aren’t really atomic. What kind of value best represents them?

    A record (where the two fields are floats)

  2. What is the difference between a character and a grapheme? (I am looking for a very specific technical answer here, not something vague, nor just an example.)

    A character is a unit of textual information; a grapheme is a unit of writing

  3. What do you call the unique number attached to a character?

    A code point

  4. Give the theoretical (language-independent) notation for a reference to a record of two properties, the first of which has the label name with value "Alice" and the second of which has the label location whose value is a tuple over the values -8 and 33.831, respectively.

    &(name: "Alice", location: (-8, 33.831))

  5. What is the difference between a literal and a variable?

    A literal directly represents a value; a variable stores a value

  6. Which of the following statements are true about the similarities and differences between generators in JavaScript and Python? Circle the statements that are true.

    JavaScript generators always produce objects, even when they have finished
    Python generators always produce dictionaries, even when they have finished
    JavaScript generators throw an exception when exhausted
    Python generators raise an exception when exhausted

  7. What is the difference between an expression and a statement?

    An expression evaluates to a value; a statement performs an action

  8. JavaScript does not have kwargs like Python does. What is the closest equivalent?

    A function having a single parameter which is an object, expressed with an object pattern and called with an object expression

  9. Rewrite the JavaScript expression x || y as a conditional expression.

    x ? x : y

  10. When might the expressions a+(b-c) and (a+b)-c produce different results?

    Lots of ways! (1) Overflow: for the Swift type Int8, 100+(50-30) is 120 but (100+50)-30 crashes. (2) Roundoff: 0.1+(0.2-0.1) is 0.2 but (0.1+0.2)-0.1 is 0.20000000000000004. (3) Types: In JS, "5"+(1-8) is "5-7" but ("5"+1)-8 is 43.

  11. Why can it be said that Python’s match statement is reasonable but JavaScript’s switch statement is an abomination?

    JavaScript switch cases fall through

  12. What is the equivalent of Lua’s for i := 1,10,3 do print(i) end in JavaScript and TypeScript?
  13. for (let i = 1; i <= 10; i += 3) { console.log(i); }

  14. Which of the following expressions merges the two dictionaries d1 and d2 in Python? (By merge we mean a shallow merge, i.e., returns a new dictionary with shallow copies.) Circle the best answer.

    d1 | d2{*d1, *d2}{**d1, **d2}d1.merge(d2){...d1, ...d2}

  15. Which of the following is falsy in Python but truthy in JavaScript? Circle the best answer.

    -0""[]     A function without a return statement

  16. Define, in type-theoretic notation, a type for drawing instructions, where an instruction can be (1) lift the pen up, (2) put the pen down, (3) move forward a given number of millimeters, (4) turn counterclockwise a given number of radians, or (5) change the current color to a new color represented by red, green, and blue values that are integer values between 0 and 255, inclusive.

    $\dfrac{}{\textsf{penUp}\!: \textsf{Instruction}}$
    $\dfrac{}{\textsf{penDown}\!: \textsf{Instruction}}$
    $\dfrac{d\!: \textsf{Float64}}{\textsf{forwardmm}\,d\!: \textsf{Instruction}}$
    $\dfrac{r\!: \textsf{Float64}}{\textsf{turnCounterClockwise}\,r\!: \textsf{Instruction}}$
    $\dfrac{r\!: \textsf{UInt8}\quad g\!: \textsf{UInt8} \quad b\!: \textsf{UInt8}}{\textsf{setColor}\,r\,g\,b\!: \textsf{Instruction}}$
  17. How do you denote, in Type Theory, the type of functions from pairs of natural number lists to a result that is either a boolean or a 64-bit integer?

    $(\textsf{Nat}^* \times \textsf{Nat}^*) \to (\textsf{Bool} \mid \textsf{Int64})$

  18. Define a function, using the type-theoretic definition-by-cases, of a function called thirdElement that accepts a list of elements of type $t$, that somehow returns its third element if it exists. (This question is assessing your ability to define this function well, given that the argument may not have a third element.)

    $\textsf{thirdElement}\!: t* \to t\textsf{?}$
    $\textsf{thirdElement}\,[\,] = \textsf{none}$
    $\textsf{thirdElement}\,(x \textsf{::} [\,]) = \textsf{none}$
    $\textsf{thirdElement}\,(x \textsf{::} y \textsf{::} [\,]) = \textsf{none}$
    $\textsf{thirdElement}\,(x \textsf{::} y \textsf{::} z \textsf{::} w) = \textsf{some}\;z$

  19. What best describes JavaScript’s type system? Circle the best answer.

    Static+Strong   Static+Weak   Dynamic+Strong   Dynamic+Weak

  20. How does TypeScript natively represent optionals?
  21. With the type T|undefined for some type T

  22. Which of the following causes a TypeError in JavaScript? Circle the best answer.

    Accessing a property of an object that does not exist

    Invoking a class name as a function without the new operator

    Invoking a number as a function

    Using null as an index into an array