Exam2_Key - ICS 313-03 Fundamentals of Programming...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ICS 313-03 Fundamentals of Programming Languages Exam II (20%) (75 Minutes) Student ID: Name: KEY Question Max 1 14 2 26 3 20 4 20 5 20 Total Mark 100 21 December 2002 (021) Question 1 [14] Assume the following program was compiled and executed using static scoping rules : program main; var x : integer; procedure sub1; begin { sub1 } writeln ( ‘x = ‘ , x} end; {sub1 } procedure sub2; var x : integer; begin { sub2 } x := 5; sub1 end; { sub2 } begin { main } x := 10; sub2 end. { main } 1.1 What value of x is printed in procedure sub1? x = 10 1.2 Under dynamic scoping rules, what value of x is printed in procedure sub1? x=5 Question 2 [16, 10] 2.1 Define static, fixed stack-dynamic, stack-dynamic, and dynamic arrays. What are the advantages of each? Static: range of subscripts and storage bindings are static Advantage: execution efficiency (no allocation or deallocation) Fixed stack dynamic: range of subscripts is statically bound, but storage is bound at elaboration time Advantage: space efficiency Stack-dynamic: range and storage are dynamic, but fixed from then on for the variable’s lifetime Advantage: flexibility (size need not be known until the array is about to be used) Dynamic: subscript range and storage bindings are dynamic and not fixed Advantage: flexibility (arrays can grow and shrink during program execution as needed) 2.2 Suppose that a language includes user-defined enumeration types and that the enumeration values could be overloaded; that is, the same literal value could appear in two different enumeration types, as in: type colors = (red, blue, green, white); mood = (happy, angry, blue); Use of the constant blue cannot be type checked. Propose a method of allowing such type checking without completely disallowing such overloading. Give an example. Require all references to enumeration constants to be qualified with the type name, as in colors.blue Question 3 [10, 10] 3.1 Describe a situation in which the add operator in a programming language would not be commutative. An expression such as a + fun(b), and fun(b) changes a. 3.2 Consider the following C program: int fun(int *i) { *i += 5; return 4; } void main () { int x = 3; x = x + fun(&x); } What is the value of x after the assignment statement in main, assuming a. Operands are evaluated left to right. 7 b. Operands are evaluated right to left. 12 Question 4 [20] The following code segment was used as evidence that the readability of some code with gotos is better that the equivalent code without gotos. This code finds the first row of an n × n integer matrix named x that has nothing but zero values. for i := 1 to n do begin for j := 1 to n do if x[i, j] <> 0 then goto reject; writln (‘First all-zero row is:’, i); break; reject: end; Rewrite this code without gotos in C or Java for (i = 1; i <= n; i++) { flag = 1; for (j = 1; j <= n; j++) if (x[i][j] != 0) { flag = 0; break; } if (flag == 1) { printf("First all-zero row is: %d\n", i); break; } } Question 5 [20] Consider the following program written in C syntax: void main() { int value = 2, list[5] = {1, 3, 5, 7, 9}; swap(value, list[0]); swap(list[0], list[1]); swap(value, list[value]); } void swap(int a, int b) { int temp; temp = a; a = b; b = temp; } For each of the following parameter-passing methods, what are all of the values of the variables value and list after each of the three calls to swap? a. Passed by value b. Passed by reference c. Passed by name d. Passed by value-result a. Passed by value value = 2, list = {1, 3, 5, 7, 9} value = 2, list = {1, 3, 5, 7, 9} value = 2, list = {1, 3, 5, 7, 9} [ after first swap ] [ after second swap ] [ after third swap ] b. Passed by reference value = 1, list = {2, 3, 5, 7, 9} value = 1, list = {3, 2, 5, 7, 9} value = 2, list = {3, 1, 5, 7, 9} [ after first swap ] [ after second swap ] [ after third swap ] c. Passed by name value = 1, list = {2, 3, 5, 7, 9} value = 1, list = {3, 2, 5, 7, 9} value = 2, list = {3, 2, 1, 7, 9} [ after first swap ] [ after second swap ] [ after third swap ] a. Passed by value-result value = 1, list = {2, 3, 5, 7, 9} value = 1, list = {3, 2, 5, 7, 9} value = 2, list = {3, 2, 1, 7, 9} [ after first swap ] [ after second swap ] [ after third swap ], assume that we change first parameter first ...
View Full Document

This note was uploaded on 09/15/2010 for the course ICS ics103 taught by Professor Alvi during the Spring '07 term at King Fahd University of Petroleum & Minerals.

Ask a homework question - tutors are online