Oppimateriaalin copyright © 2007 Arto Wikla. Tämän oppimateriaalin käyttö on sallittu vain yksityishenkilöille opiskelutarkoituksissa. Materiaalin käyttö muihin tarkoituksiin, kuten kaupallisilla tai muilla kursseilla, on kielletty.

581362 Ohjelmointikielten periaatteet keväällä 2007: 3. harjoitukset 6.2.

  1. [Modified from Scott's Review Questions for Chapter 6, Vihavainen, Wikla] Answer the following questions concerning programming languages:

    1. What is the difference between a value model of variables and a reference model of variables? Why is this distinction important? Why is the distinction between mutable and immutable values important in the implementation of a language with a reference model of variables? Languages that employ a reference model of variables also tend to employ automatic garbage collection. Is this more than a coindidence? Explain.
    2. Why is it generally expensive to catch all uses of uninitialized variables at run time?
    3. Explain the notion of definite assignment in Java.
    4. What are the advantages of updating a variable with a combination assignment operator (+=, -=, etc.), rather than with a regular assignment in which the same variable appears on both the left- and right-hand side?
    5. Explain why it may sometimes be useful for a function to have side effects.
    6. Describe three different search strategies that might be employed in the implementation of a case statement, and the circumstances in which each would be desirable.
    7. Anna esimerkki algoritmista (tai tilanteesta), jossa "puolitoistakertainen luuppi" johtaa selkeämpään rakenteeseen kuin alku- tai loppuehtoinen toisto.
    8. Explain the difference between applicative and normal order evaluation of expressions. Under what circumstances is each desirable?

  2. [Modified from Scott 6.1, Vihavainen] We noted in Section 6.1.1 that most binary arithmetic operators a left-asociative in most programming languages. At the same time, we noted in Section 6.1.3 that most compilers are free to evaluate the operands of a binary operator in either order. Are these statements contradictory? Why or why not?

  3. [Scott 6.3] Translate the following expression into postfix and prefix notation. Do you need a special symbol for unary negation?
        [-b + sqrt(b * b - 4 * a * c)]/(2 * a)
    
  4. [Scott 6.4] In Lisp, most of the arithmetic operators are defined to take two or more arguments, rather than strictly one or two. Thus (* 2 3 4 5) evaluates to 120, and (- 16 9 4) evaluates to 3. Show that parentheses are necessary to disambiguate arithmetic expressions in Lisp (in other words give an example of an expression whose meaning is unclear when parentheses are removed.)
    In Section 6.1.1 we claimed that issues of precedence and associativity do not arise with prefix and postfix notation. Reword this claim to make explicit the hidden assumption in the original claim.

  5. [Scott 6.38] Explain the different approaches to arithmetic overflow adopted by Pascal, C Java, C# and Common Lisp, as described in Section 6.1.4 (p. 251). Speculate as to the differences in language design goals that might have caused the designers to adopt the approaches they did.

  6. [Modified from Scott's Review Questions for Chapter 7, Vihavainen, Wikla] Answer the following questions concerning programming languages:

    1. What purpose(s) do types serve in a programming language? What does it mean for a language to be strongly typed? Statically typed? What prevents, say C or C++, from being strongly typed? Name two important programming languages that are strongly but dynamically typed.
    2. What does it mean for the type system of a language to be orthogonal? Give an example of an orthogonal feature of a type system. Give an example of a non-orthogonal feature of a type system.
    3. Explain the differences between the denotational, constructive, and abstraction-based views on types.
    4. In what ways may an enumeration type be preferable to a collection of named constants? In what ways may a subrange type be preferable to its base type? In what ways may a string be preferable to an array of characters?
    5. Javaa:
      1. Millaisia rakenteisia literaaleja Javassa voi käyttää? Onko joidenkin tällaisten käyttö rajoitettu joihinkin erikoistilanteisiin? Ja onko niin hyvä?
      2. Voisiko mielestäsi nimetöntä sisäluokkaa pitää "luokkaliteraalina" ja sen ilmentymää "olioliteraalina"?
    6. Explain the difference between type conversion, type coercion, and nonconverting type casts.
    7. Under what circumstances can an array declared within a subroutine be allocated in the stack? Under what circumstances must it be allocated in the heap?

  7. [Modified from Scott 7.1, Vihavainen]
    1. Define the structural and name equivalence for types. Most modern Algol-family languages use some form of name equivalence for types. Explain the difference between strict and loose name equivalence. What kind of languages use structural equivalence of types? Name three languages that use each approach (either name or structural).
    2. Discuss the comparative advantages of structural and name equivalence for types. Is structural equivalence a bad idea? Why or why not? Explain briefly how a compiler actually determines name equivalence and structural equivalence for types, respectively.

  8. [Scott 7.29, osa] Occasionally one encounters the suggestion that a garbage-collected language should provide a delete operation as an optimization: by explicitly deleteing objects that will never be used again, the programmer might save the garbage collector the trouble of finding and reclaiming those objects automatically, thereby improving performance. What do you think of this suggestion? Explain.


Takaisin harjoitusten pääsivulle.