Name PID COMP 524 Spring 2018 Midterm Thursday, March 1 This exam is open note, open book and open computer. It is not open people. You are to submit this exam through gradescope. Resubmissions have been enabled. Download the fillable pdf and add your name and PID. Save the file and then make sure that the save worked properly. Remember to save your work often and confirm that it is being saved. If you are unable to open the pdf, use a paper test. If you do that, you are responsible for scanning it and uploading the scanned pdf to gradescope before 6 pm. The test is based on 100 points. Questions have different values. You should read the entire exam before starting to answer questions. The test is designed for 60 minutes; you have 75 minutes to complete it. By submitting this exam, you are pledging that you have neither given nor received unauthorized help.
Question 1 (20 points) What would the following program output if the language is lexically (statically) scoped? What would it output if it is dynamically scoped? You must provide traces with the start and end of each function, the value of x at each point, and the return values of functions in order to get credit. If a function does not return a value, say so. Sample trace (clearly not correct or complete): x = 72 Enter f x = 54 Enter g x = 16 x = 17 Return 46 Resume f Return 22 Print 87 Lexical (Static) Scoping var x : int = 0 function f() { var x : int = 3 return g() function g() { x++ h() return x function h() { x++ print f() Dynamic Scoping
Question 2 (10 points) type T = int[4] type V = int[4] type S = T var a: T = [1, 2, 3, 4] var b: V = a var c: S = b var d = [1, 2, 3, 4] Given that the above is a valid snippet and all of the variables listed would compare to be equal, what kind of equivalence does this language support (structural or name)? Explain your answer. If the other type of equivalence were used, which variables would be considered equivalent? Does this language support type aliasing? Explain your answer and indicate what would be different if the opposite were true. That is, would something in the snippet be invalid or would some of the equivalences change?
Question 3 (30 points) struct Node *head = NULL; struct Node { int value; struct Node *next; struct Node *prev; ; void delete(struct Node *node, int key) { while (node!= NULL && node->value!= key) { node = node->next; if (node!= NULL) { if (node->prev!= NULL) { node->prev->next = node->next; else { head = node->next; int main() { int i = 1, j = 2, k = 3; struct Node *a = malloc(sizeof(struct Node)); struct Node *b = malloc(sizeof(struct Node)); struct Node *c = malloc(sizeof(struct Node)); a->value = i; b->value = j; c->value = k; Given the program to the left in a C-like language (i.e., one that supports pointers and allocation and freeing), identify which variables are located in the stack, which are in the heap, and which in static storage. You can assume that main behaves like any other function. You can assume that the free function frees the struct that it is pointing to. Variable locations i j k a b c head Does the program contain any memory leaks? If so, suggest how you might fix them. You do not need to write the code just describe what needs to be done head = a; head->prev = NULL; a->next = b; b->next = c; b->prev = a; c->next = NULL; c->prev = b; delete(head, 2); free(head); Notation: If T is a structure with an element e, T *v v is a pointer to type T *v refers to the actual value v->t same as (*v).
Question 4 (10 points) Suppose a language supports primitive values that can be null. Given an example of how this could be useful to the user of the language (users of the language include both writers and readers). How might this affect the implementation of primitive values in this language? Can you identify any potential negatives with supporting null primitives?
Question 5 (15 points) Type checking can be done either dynamically or statically. Explain the benefits and costs of dynamic type checking. Identify specific features that require dynamic type checking (e.g., data types that need it, language choices such as required declarations, different types of binding)
Question 6 (15 points) Discuss the pros and cons of automatic type conversion vs required casting. Which is more efficient for the user of the language? Which is more efficient for the compiler? Does it matter if the language does dynamic or static type checking? Describe the problems that can occur when automatic type conversion and polymorphism both exist in a language. Are there only specific types of polymorphism that cause these problems? Give an example of polymorphism that should not be affected by automatic conversion.