# Rick van der Zwet # StudentID: 0433373 # $Id: lecture4.txt 301 2007-11-09 09:21:48Z rick $ All quotes are based on the 8th edition of Concepts of programming languages from Robert W. Sebesta *** Chapter 5 Review Questions *** 3, 4, 6, 8, 9, 12, 13, 14 3) Q: In what way are reserved words better than keywords? A: A reserved word ... can be confusing pg 205 4) Q: What is an alias? A: When more than ... are called aliases pg 206 6) Q: What is the l-value of a variable? What is the r-value? A: The address of ... called its l-value pg 206 A variable its value ... called its r-value pg 207 8) Q: After language design and implemntation, what are the four times bindings can take place in a program? A: Binding can take ... or run time pg 208 9) Q: Define static binding and dynamic binding. A: A binding is ... is called dynamic pg 208 12) Q: Define static, stack-dynamic, explicit heap-dynamic and implicit heap-dynamic variables. What are the advantages and disadvantages of these? A: All descibed in detail at 5.43 pg 215-219 13) Q: Define coercion, type error, type checking and strong typing. A: Coercion : A compatible type ... is called coercion pg 219 Type error : A type error ... an inappropriate type pg 219 Type checking : Type checking is . of compatible types pg 219 Stong typing : A proramming language ... are always detected pg 219-220 14) Q: Define name type compatibility and structure type compatibility. What are the relative merits of these two? A: Name type equivalence ... have identical structures. pg 221 *** Chapter 5 Problem Set *** 8, 9, 12, 14 8) Q: Consider the following Ada skeletal program: Assume that the execution of this program is in the following unit order: Main -> Sub1 -> Sub2 -> Sub3 (-> means Calls) a) Assuming static scoping/dynamic in the following which declaration of X is the correct one for a reference to X? A: Part | Dynamic | Static | ------------------------- Sub1 | Sub1 | Sub1 | Sub2 | Sub1 | Sub1 | Sub3 | Sub1 | Main | 9) Q: Assume Ada program compiled, what vlue of X is printed in procedure Sub1? A: Static-scoping : 5 Dynamic-scoping : 10 12) Q: Consider the following C program, for earch of the four marked points in the functin, list each visible variable, along with the number of the definition statement that define it. A: Marker | Def 1 | Def 2 | Def 3 | ---------------------------------------------- 1 | A | BCD | - | 2 | A | B | CDE | 3 | A | BCD | - | 4 | ABC | - | - | 14) Q: Consider the following program: Give the following calling sequences and assuming that dynamic scoping is used. A: Part | Main | Sub 1 | Sub 2 | Sub 3 | -------------------------------------- a | - | Y | BZ | AXW | b | - | YZ | - | AXW | c | - | AYZ | B | XW | d | - | AYZ | - | XW | e | - | Y | ABZ | XW | f | - | AYZ | B | XW | *** Chapter 6 Review Questions *** 9, 10, 11, 14, 19 9) Q: Define static, fixed stack-dynamic, stick,-dynamic and heap-dynamic arrays. What are the advantages of each? A: A static array ... for space changes pg 265-266 10) Q: What is a heterogeneous array? A: A heterogeneous array ... of the same type pg 267 11) Q: What happends when a nonexistent element of an array is referenced in Perl? A: A reference to ... Perl yields _undef_, but no error is reported pg 265 14) Q: What languages support array slices with stepsizes? A: Fortran 95, Python, Perl pg 272 19) Q: Define row major order and column major order. A: In row major .. and so forth pg 274 In column major ... and so forth pg 274-275 *** Chapter 6 Problem Set *** 4, 11, 12, 13 4) Q: Compare the tombstone and lock-and-key methods of avaiding dangling pointers, from the points of veiw of safety and implementation cost. A: Tombstones are safe, the pointer is never pointing to a wrong value, but costly and space ineffient, because tombstones are kept forevert and every call needs to follow yet another pointer. Lock-and-keys requires more book keeping in terms of space and comparisons, but will clean up the 'junk' nicely. Its not clear to me wether the key and dispose value will always be unique and not possible to be faked. This will be a possible security risk. 11) Q: In the Burroughs Extended ALGOL language, matrices are stored as a single-dimentioned array of pointers to the rows of the matrix, which are treated as single-dimentioned arrays of values. What are the advantages and disadvantages of such a scheme? A: Row opererations -like adding,removing,looping are fast-, but column operations are costly. 12) Q: Analyze and write a comparison of C's malloc and free functions with C++'s new and delete operators. Use safety as primary consideration in the comparison. A: new and delete are type safe (no need for casts), malloc and free are not. Also malloc returns a void* which then has to be cast to the appropriate pointer type. new returns the correct pointer type itself; type safety. malloc requires you to tell the number of bytes to allocate, new figures it out itself. source: http://www.velocityreviews.com/forums/\ t284406-differences-between-newdelete-and-mallocfree.html 13) Q: Analyse and write a comparision of using C++ pointers and Java references variables to refer to fixed heap-dynamic variables. Use safety and convenience as the primary considerations in the comparison A: Java fixed the problem by disallowing to mess around with pointers completely. This will make writing code much more clear and remove the security problem of the pointers. *** Chapter 7 Problem Set *** 7, 8, 9, 10, 11, 13 7) Q: Describe a situation in which the add operator in a programming language would not be commutative. A: Take a look at "foo" + "bar" = "foobar" this could not be turned around 8) Q: Descibe a situation in which the add operator in a programming language would not be associative. A: We use 16 bit signed integers namely -32768, 32767, and 1. * (-32768 + 32767) + 1 = (-1) + 1 = 0. * -32768 + (32767 + 1) = -32767 + = 9) Q: Assume the following rules of associativity and precedence for expressions, show the order using parenthesizes. and subscript to determine the order Precedence Highest - Lowest *,/,not +,-,&,mod - (unary) =,/=,<,<=,>=,> and or,xor Associativity: Left to right A: a :: (((a * b)^1 - 1)^2 + c)^3 b :: (((a * (b - 1)^1)^2 / c)^3 mod d)^4 c :: (((a - b)^1 / c)^2 & (((d * e)^3 / a)^4 - 3)^5)^6 d :: ((-a)^1 or ((c = d)^2 and e)^3)^4 e :: (((a > b)^1 xor c)^3 or (d <= 17)^2)^4 f :: (-(a + b)^1)^2 10) Q: Show the order of evaluation of the expression of Problem 9, assuming that there are no precedence rules and all operators associate right to left A: a :: (a * (b - (1 + c)^1)^2)^3 b :: (a * ((b - 1)^2 / (c mod d)^1)^3)^4 c :: ((a - b)^5 / (c & (d * (e / (a - 3)^1)^2)^3)^4)^6 d :: (-(a or (c = (d and e)^1)^2)^3)^4 e :: (a > (b xor (c or (d <= 17)^1)^2)^3)^4 f :: (-(a + b)^1)^2 11) Q: Write a BNF description of the precendence and associate rules defined for the expressions in Problem 9. Assume the only operands are the names a,b,c,d and e A: Least important ones last: -> * | / | not | + | - | & | mod | - | = | /= | < | <= | >= | > | and | or | xor | -> "a" | "b" | "c" | "d" | "e" 13) Q: Let the function fun be defined as: <> Suppose fun is used in a program as follows: <> What are the values of sum and sum2: a) if the operands in the expressions are evaluated left to right? b) if the operands in the expressions are evaluated right to left? A: Problem subanswers also given a) * sum1 = undef, sum2 = undef * sum1 = (10 / 2) + (3 * 14 - 1) = 5 + 41 = 46 * sum2 = (3 * 14 - 1) + (14 / 2) = 41 + 7 = 48 b) * sum1 = undef, sum2 = undef * sum1 = (14 / 2) + (3 * (14 - 1)) = 7 + 39 = 46 * sum2 = (3 * (14 -1)) + (10 / 2) = 39 + 5 = 44