[2] | 1 | # Rick van der Zwet
|
---|
| 2 | # StudentID: 0433373
|
---|
| 3 | # $Id: lecture4.txt 301 2007-11-09 09:21:48Z rick $
|
---|
| 4 |
|
---|
| 5 | All quotes are based on the 8th edition of Concepts of programming
|
---|
| 6 | languages from Robert W. Sebesta
|
---|
| 7 |
|
---|
| 8 | *** Chapter 5 Review Questions ***
|
---|
| 9 | 3, 4, 6, 8, 9, 12, 13, 14
|
---|
| 10 |
|
---|
| 11 | 3)
|
---|
| 12 | Q: In what way are reserved words better than keywords?
|
---|
| 13 | A: A reserved word ... can be confusing pg 205
|
---|
| 14 |
|
---|
| 15 | 4)
|
---|
| 16 | Q: What is an alias?
|
---|
| 17 | A: When more than ... are called aliases pg 206
|
---|
| 18 |
|
---|
| 19 | 6)
|
---|
| 20 | Q: What is the l-value of a variable? What is the r-value?
|
---|
| 21 | A: The address of ... called its l-value pg 206
|
---|
| 22 | A variable its value ... called its r-value pg 207
|
---|
| 23 |
|
---|
| 24 | 8)
|
---|
| 25 | Q: After language design and implemntation, what are the four times
|
---|
| 26 | bindings can take place in a program?
|
---|
| 27 | A: Binding can take ... or run time pg 208
|
---|
| 28 |
|
---|
| 29 | 9)
|
---|
| 30 | Q: Define static binding and dynamic binding.
|
---|
| 31 | A: A binding is ... is called dynamic pg 208
|
---|
| 32 |
|
---|
| 33 | 12)
|
---|
| 34 | Q: Define static, stack-dynamic, explicit heap-dynamic and implicit
|
---|
| 35 | heap-dynamic variables. What are the advantages and disadvantages of
|
---|
| 36 | these?
|
---|
| 37 | A: All descibed in detail at 5.43 pg 215-219
|
---|
| 38 |
|
---|
| 39 | 13)
|
---|
| 40 | Q: Define coercion, type error, type checking and strong typing.
|
---|
| 41 | A: Coercion : A compatible type ... is called coercion pg 219
|
---|
| 42 | Type error : A type error ... an inappropriate type pg 219
|
---|
| 43 | Type checking : Type checking is . of compatible types pg 219
|
---|
| 44 | Stong typing : A proramming language ... are always detected pg
|
---|
| 45 | 219-220
|
---|
| 46 |
|
---|
| 47 | 14)
|
---|
| 48 | Q: Define name type compatibility and structure type compatibility. What
|
---|
| 49 | are the relative merits of these two?
|
---|
| 50 | A: Name type equivalence ... have identical structures. pg 221
|
---|
| 51 |
|
---|
| 52 | *** Chapter 5 Problem Set ***
|
---|
| 53 | 8, 9, 12, 14
|
---|
| 54 |
|
---|
| 55 | 8)
|
---|
| 56 | Q: Consider the following Ada skeletal program:
|
---|
| 57 | <pg 241>
|
---|
| 58 | Assume that the execution of this program is in the following unit
|
---|
| 59 | order: Main -> Sub1 -> Sub2 -> Sub3 (-> means Calls)
|
---|
| 60 | a) Assuming static scoping/dynamic in the following which declaration of X is
|
---|
| 61 | the correct one for a reference to X?
|
---|
| 62 | A: Part | Dynamic | Static |
|
---|
| 63 | -------------------------
|
---|
| 64 | Sub1 | Sub1 | Sub1 |
|
---|
| 65 | Sub2 | Sub1 | Sub1 |
|
---|
| 66 | Sub3 | Sub1 | Main |
|
---|
| 67 |
|
---|
| 68 | 9)
|
---|
| 69 | Q: Assume Ada program compiled, what vlue of X is printed in procedure
|
---|
| 70 | Sub1?
|
---|
| 71 | A: Static-scoping : 5
|
---|
| 72 | Dynamic-scoping : 10
|
---|
| 73 |
|
---|
| 74 | 12)
|
---|
| 75 | Q: Consider the following C program, for earch of the four marked
|
---|
| 76 | points in the functin, list each visible variable, along with the
|
---|
| 77 | number of the definition statement that define it.
|
---|
| 78 | A: Marker | Def 1 | Def 2 | Def 3 |
|
---|
| 79 | ----------------------------------------------
|
---|
| 80 | 1 | A | BCD | - |
|
---|
| 81 | 2 | A | B | CDE |
|
---|
| 82 | 3 | A | BCD | - |
|
---|
| 83 | 4 | ABC | - | - |
|
---|
| 84 |
|
---|
| 85 | 14)
|
---|
| 86 | Q: Consider the following program:
|
---|
| 87 | Give the following calling sequences and assuming that dynamic scoping
|
---|
| 88 | is used.
|
---|
| 89 | A: Part | Main | Sub 1 | Sub 2 | Sub 3 |
|
---|
| 90 | --------------------------------------
|
---|
| 91 | a | - | Y | BZ | AXW |
|
---|
| 92 | b | - | YZ | - | AXW |
|
---|
| 93 | c | - | AYZ | B | XW |
|
---|
| 94 | d | - | AYZ | - | XW |
|
---|
| 95 | e | - | Y | ABZ | XW |
|
---|
| 96 | f | - | AYZ | B | XW |
|
---|
| 97 |
|
---|
| 98 |
|
---|
| 99 | *** Chapter 6 Review Questions ***
|
---|
| 100 | 9, 10, 11, 14, 19
|
---|
| 101 |
|
---|
| 102 | 9)
|
---|
| 103 | Q: Define static, fixed stack-dynamic, stick,-dynamic and heap-dynamic
|
---|
| 104 | arrays. What are the advantages of each?
|
---|
| 105 | A: A static array ... for space changes pg 265-266
|
---|
| 106 |
|
---|
| 107 | 10)
|
---|
| 108 | Q: What is a heterogeneous array?
|
---|
| 109 | A: A heterogeneous array ... of the same type pg 267
|
---|
| 110 |
|
---|
| 111 | 11)
|
---|
| 112 | Q: What happends when a nonexistent element of an array is referenced in
|
---|
| 113 | Perl?
|
---|
| 114 | A: A reference to ... Perl yields _undef_, but no error is reported pg
|
---|
| 115 | 265
|
---|
| 116 |
|
---|
| 117 | 14)
|
---|
| 118 | Q: What languages support array slices with stepsizes?
|
---|
| 119 | A: Fortran 95, Python, Perl pg 272
|
---|
| 120 |
|
---|
| 121 | 19)
|
---|
| 122 | Q: Define row major order and column major order.
|
---|
| 123 | A: In row major .. and so forth pg 274
|
---|
| 124 | In column major ... and so forth pg 274-275
|
---|
| 125 |
|
---|
| 126 | *** Chapter 6 Problem Set ***
|
---|
| 127 | 4, 11, 12, 13
|
---|
| 128 |
|
---|
| 129 | 4)
|
---|
| 130 | Q: Compare the tombstone and lock-and-key methods of avaiding dangling
|
---|
| 131 | pointers, from the points of veiw of safety and implementation cost.
|
---|
| 132 | A: Tombstones are safe, the pointer is never pointing to a wrong value,
|
---|
| 133 | but costly and space ineffient, because tombstones are kept forevert
|
---|
| 134 | and every call needs to follow yet another pointer.
|
---|
| 135 | Lock-and-keys requires more book keeping in terms of space and
|
---|
| 136 | comparisons, but will clean up the 'junk' nicely. Its not clear to me
|
---|
| 137 | wether the key and dispose value will always be unique and not
|
---|
| 138 | possible to be faked. This will be a possible security risk.
|
---|
| 139 |
|
---|
| 140 | 11)
|
---|
| 141 | Q: In the Burroughs Extended ALGOL language, matrices are stored as a
|
---|
| 142 | single-dimentioned array of pointers to the rows of the matrix, which
|
---|
| 143 | are treated as single-dimentioned arrays of values. What are the
|
---|
| 144 | advantages and disadvantages of such a scheme?
|
---|
| 145 | A: Row opererations -like adding,removing,looping are fast-, but column
|
---|
| 146 | operations are costly.
|
---|
| 147 |
|
---|
| 148 | 12)
|
---|
| 149 | Q: Analyze and write a comparison of C's malloc and free functions with
|
---|
| 150 | C++'s new and delete operators. Use safety as primary consideration in
|
---|
| 151 | the comparison.
|
---|
| 152 | A: new and delete are type safe (no need for casts), malloc and free are
|
---|
| 153 | not. Also malloc returns a void* which then has to be cast to the
|
---|
| 154 | appropriate pointer type. new returns the correct pointer type itself;
|
---|
| 155 | type safety. malloc requires you to tell the number of bytes to allocate,
|
---|
| 156 | new figures it out itself.
|
---|
| 157 | source: http://www.velocityreviews.com/forums/\
|
---|
| 158 | t284406-differences-between-newdelete-and-mallocfree.html
|
---|
| 159 |
|
---|
| 160 | 13)
|
---|
| 161 | Q: Analyse and write a comparision of using C++ pointers and Java
|
---|
| 162 | references variables to refer to fixed heap-dynamic variables. Use
|
---|
| 163 | safety and convenience as the primary considerations in the comparison
|
---|
| 164 | A: Java fixed the problem by disallowing to mess around with pointers
|
---|
| 165 | completely. This will make writing code much more clear and remove
|
---|
| 166 | the security problem of the pointers.
|
---|
| 167 |
|
---|
| 168 | *** Chapter 7 Problem Set ***
|
---|
| 169 | 7, 8, 9, 10, 11, 13
|
---|
| 170 |
|
---|
| 171 | 7)
|
---|
| 172 | Q: Describe a situation in which the add operator in a programming
|
---|
| 173 | language would not be commutative.
|
---|
| 174 | A: Take a look at "foo" + "bar" = "foobar" this could not be turned
|
---|
| 175 | around
|
---|
| 176 |
|
---|
| 177 | 8)
|
---|
| 178 | Q: Descibe a situation in which the add operator in a programming
|
---|
| 179 | language would not be associative.
|
---|
| 180 | A: We use 16 bit signed integers namely -32768, 32767, and 1.
|
---|
| 181 | * (-32768 + 32767) + 1 = (-1) + 1 = 0.
|
---|
| 182 | * -32768 + (32767 + 1) = -32767 + <error overflow> = <error overflow>
|
---|
| 183 |
|
---|
| 184 | 9)
|
---|
| 185 | Q: Assume the following rules of associativity and precedence for
|
---|
| 186 | expressions, show the order using parenthesizes. and subscript to
|
---|
| 187 | determine the order
|
---|
| 188 | Precedence Highest - Lowest
|
---|
| 189 | *,/,not
|
---|
| 190 | +,-,&,mod
|
---|
| 191 | - (unary)
|
---|
| 192 | =,/=,<,<=,>=,>
|
---|
| 193 | and
|
---|
| 194 | or,xor
|
---|
| 195 | Associativity: Left to right
|
---|
| 196 | A: a :: (((a * b)^1 - 1)^2 + c)^3
|
---|
| 197 | b :: (((a * (b - 1)^1)^2 / c)^3 mod d)^4
|
---|
| 198 | c :: (((a - b)^1 / c)^2 & (((d * e)^3 / a)^4 - 3)^5)^6
|
---|
| 199 | d :: ((-a)^1 or ((c = d)^2 and e)^3)^4
|
---|
| 200 | e :: (((a > b)^1 xor c)^3 or (d <= 17)^2)^4
|
---|
| 201 | f :: (-(a + b)^1)^2
|
---|
| 202 |
|
---|
| 203 | 10)
|
---|
| 204 | Q: Show the order of evaluation of the expression of Problem 9, assuming
|
---|
| 205 | that there are no precedence rules and all operators associate right to
|
---|
| 206 | left
|
---|
| 207 | A: a :: (a * (b - (1 + c)^1)^2)^3
|
---|
| 208 | b :: (a * ((b - 1)^2 / (c mod d)^1)^3)^4
|
---|
| 209 | c :: ((a - b)^5 / (c & (d * (e / (a - 3)^1)^2)^3)^4)^6
|
---|
| 210 | d :: (-(a or (c = (d and e)^1)^2)^3)^4
|
---|
| 211 | e :: (a > (b xor (c or (d <= 17)^1)^2)^3)^4
|
---|
| 212 | f :: (-(a + b)^1)^2
|
---|
| 213 |
|
---|
| 214 | 11)
|
---|
| 215 | Q: Write a BNF description of the precendence and associate rules
|
---|
| 216 | defined for the expressions in Problem 9. Assume the only operands are
|
---|
| 217 | the names a,b,c,d and e
|
---|
| 218 | A: Least important ones last:
|
---|
| 219 | <stmt> -> <stmt> * <stmt>
|
---|
| 220 | | <stmt> / <stmt>
|
---|
| 221 | | <stmt> not <stmt>
|
---|
| 222 | | <stmt> + <stmt>
|
---|
| 223 | | <stmt> - <stmt>
|
---|
| 224 | | <stmt> & <stmt>
|
---|
| 225 | | <stmt> mod <stmt>
|
---|
| 226 | | - <stmt>
|
---|
| 227 | | <stmt> = <stmt>
|
---|
| 228 | | <stmt> /= <stmt>
|
---|
| 229 | | <stmt> < <stmt>
|
---|
| 230 | | <stmt> <= <stmt>
|
---|
| 231 | | <stmt> >= <stmt>
|
---|
| 232 | | <stmt> > <stmt>
|
---|
| 233 | | <stmt> and <stmt>
|
---|
| 234 | | <stmt> or <stmt>
|
---|
| 235 | | <stmt> xor <stmt>
|
---|
| 236 | | <oper>
|
---|
| 237 | <oper> -> "a" | "b" | "c" | "d" | "e"
|
---|
| 238 |
|
---|
| 239 |
|
---|
| 240 | 13)
|
---|
| 241 | Q: Let the function fun be defined as:
|
---|
| 242 | <<page 240>>
|
---|
| 243 | Suppose fun is used in a program as follows:
|
---|
| 244 | <<page 240>>
|
---|
| 245 | What are the values of sum and sum2:
|
---|
| 246 | a) if the operands in the expressions are evaluated left to right?
|
---|
| 247 | b) if the operands in the expressions are evaluated right to left?
|
---|
| 248 | A: Problem subanswers also given
|
---|
| 249 | a) * sum1 = undef, sum2 = undef
|
---|
| 250 | * sum1 = (10 / 2) + (3 * 14 - 1) = 5 + 41 = 46
|
---|
| 251 | * sum2 = (3 * 14 - 1) + (14 / 2) = 41 + 7 = 48
|
---|
| 252 | b) * sum1 = undef, sum2 = undef
|
---|
| 253 | * sum1 = (14 / 2) + (3 * (14 - 1)) = 7 + 39 = 46
|
---|
| 254 | * sum2 = (3 * (14 -1)) + (10 / 2) = 39 + 5 = 44
|
---|