source: liacs/cvp/assignments/lecture4.txt@ 89

Last change on this file since 89 was 2, checked in by Rick van der Zwet, 15 years ago

Initial import of data of old repository ('data') worth keeping (e.g. tracking
means of URL access statistics)

File size: 8.6 KB
Line 
1# Rick van der Zwet
2# StudentID: 0433373
3# $Id: lecture4.txt 301 2007-11-09 09:21:48Z rick $
4
5All quotes are based on the 8th edition of Concepts of programming
6languages from Robert W. Sebesta
7
8*** Chapter 5 Review Questions ***
93, 4, 6, 8, 9, 12, 13, 14
10
113)
12Q: In what way are reserved words better than keywords?
13A: A reserved word ... can be confusing pg 205
14
154)
16Q: What is an alias?
17A: When more than ... are called aliases pg 206
18
196)
20Q: What is the l-value of a variable? What is the r-value?
21A: The address of ... called its l-value pg 206
22 A variable its value ... called its r-value pg 207
23
248)
25Q: After language design and implemntation, what are the four times
26 bindings can take place in a program?
27A: Binding can take ... or run time pg 208
28
299)
30Q: Define static binding and dynamic binding.
31A: A binding is ... is called dynamic pg 208
32
3312)
34Q: Define static, stack-dynamic, explicit heap-dynamic and implicit
35 heap-dynamic variables. What are the advantages and disadvantages of
36 these?
37A: All descibed in detail at 5.43 pg 215-219
38
3913)
40Q: Define coercion, type error, type checking and strong typing.
41A: 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
4714)
48Q: Define name type compatibility and structure type compatibility. What
49 are the relative merits of these two?
50A: Name type equivalence ... have identical structures. pg 221
51
52*** Chapter 5 Problem Set ***
538, 9, 12, 14
54
558)
56Q: 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?
62A: Part | Dynamic | Static |
63 -------------------------
64 Sub1 | Sub1 | Sub1 |
65 Sub2 | Sub1 | Sub1 |
66 Sub3 | Sub1 | Main |
67
689)
69Q: Assume Ada program compiled, what vlue of X is printed in procedure
70 Sub1?
71A: Static-scoping : 5
72 Dynamic-scoping : 10
73
7412)
75Q: 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.
78A: 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
8514)
86Q: Consider the following program:
87 Give the following calling sequences and assuming that dynamic scoping
88 is used.
89A: 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 ***
1009, 10, 11, 14, 19
101
1029)
103Q: Define static, fixed stack-dynamic, stick,-dynamic and heap-dynamic
104 arrays. What are the advantages of each?
105A: A static array ... for space changes pg 265-266
106
10710)
108Q: What is a heterogeneous array?
109A: A heterogeneous array ... of the same type pg 267
110
11111)
112Q: What happends when a nonexistent element of an array is referenced in
113 Perl?
114A: A reference to ... Perl yields _undef_, but no error is reported pg
115 265
116
11714)
118Q: What languages support array slices with stepsizes?
119A: Fortran 95, Python, Perl pg 272
120
12119)
122Q: Define row major order and column major order.
123A: In row major .. and so forth pg 274
124 In column major ... and so forth pg 274-275
125
126*** Chapter 6 Problem Set ***
1274, 11, 12, 13
128
1294)
130Q: Compare the tombstone and lock-and-key methods of avaiding dangling
131 pointers, from the points of veiw of safety and implementation cost.
132A: 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
14011)
141Q: 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?
145A: Row opererations -like adding,removing,looping are fast-, but column
146 operations are costly.
147
14812)
149Q: 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.
152A: 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
16013)
161Q: 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
164A: 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 ***
1697, 8, 9, 10, 11, 13
170
1717)
172Q: Describe a situation in which the add operator in a programming
173 language would not be commutative.
174A: Take a look at "foo" + "bar" = "foobar" this could not be turned
175 around
176
1778)
178Q: Descibe a situation in which the add operator in a programming
179 language would not be associative.
180A: 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
1849)
185Q: 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
196A: 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
20310)
204Q: 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
207A: 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
21411)
215Q: 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
218A: 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
24013)
241Q: 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?
248A: 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
Note: See TracBrowser for help on using the repository browser.