1. How can a variable be characterized? [3] 2. After language design and implementation, when are the four times bindings can take place in a program? [2] 3. What is the lifetime of a variable? [2] 4. Assume the following javascript program [4] a). what is the value of x displayed in sub1() assuming static scoping rules? b). what is the value of x displayed in sub1() assuming dynamic scoping rules? var x; function sub1() { document.write("x = " + x + ""); } function sub2() { var x; x = 10; sub1(); } x = 5; sub2(); 5. Consider the following C program and for each of the four marked points in this function, list each visible variable, along with the number of the definition statement that defines it. [4] void fun(void) { int a, b, c; /* definition 1 */ . . . while (. . .) { int b, c, d; /*definition 2 */ . . . <------------- 1 while (. . .) { int c, d, e; /* definition 3 */ . . . <------------- 2 } . . . <-------------- 3 } . . . <-------------- 4 } 6. Translate the following call to Scheme’s COND to C and set the resulting value to y. [2] (COND ((> x 10) x) ((< x 5) (* 2 x)) ((= x 7) (+ x 10)) (t (- 2 x))) 7. What are the two general categories of a selection statement? [2] 8. What is the difference between casting and coercion [1] 9. What are the different syntactic ways parameters can be passed? [3] 10. What are the modes, the conceptual models of transfer, the advantages, and the disadvantages of pass-by-value, pass-by-result, pass-by-value-result, and pass-by-reference parameter-passing methods? [4] 11. Compare and contrast the deep-access method of implementing dynamic scoping to the shallow-access method of implementing dynamic scoping. [2] 12. What is the difference between the static link and the dynamic link stored within an activation record instance (assuming static scoping rules)? [1] ================================================================================ UNGRADED QUESTIONS / OCAML PROBLEMS 13. Write a function called sumOfTuples that accepts a list of (int, int) tuples and computes the per-element sum of the tuple elements. For example: sumOfTuples [(1,2);(2,4);(4;8)] --> (7,15) 14. Define a variant for a new class of datatypes. The datatype should be able to represent one of three possible things: a Value type that holds an int, an Empty type that holds nothing, and an Error type that holds a special Exception type called "except". Provide the definition of the new type. The name of the type should be "value". 15. Implement an asString method for the above declared datatype. You may assume there is a string_of_except function. When the "value" type is Empty, you can return an "Empty" string. 16. Show the stack with all activation record instances, including static and dynamic chains, when execution reaches position 1 in the following skeletal program. Assume bigsub is at level 1. function bigsub() { function a() { function b() { ... <-------------- 1 } // end of b function c() { ... b(); ... } // end of c ... c(); ... } // end of a ... a(); ... } // end of bigsub 17. Assume that the following program is implemented using the shallow-access method using a stack for each variable name. Show the stacks for the time of the execution of fun3, assuming the calling sequence is: main calls fun1; fun1 calls fun3; fun3 calls fun2 void fun1() { float a; . . . } void fun2() { int b, c; . . . <--------- 1 } void fun3() { float d; . . . } void main() { char e, f, g; . . . }