CS 1621 MIDTERM EXAM 1 Name: Problem 1 (1x17 =17 points) 2 (21 points) 3 (5 points) 4 (3 points) 5 (4 points) Total ( 50points) Score Page 1
1. (1 x 17 = 17 points) Determine if each of the following statements is true (T) or false (F) : (1) The first widely used high-level programming language is Fortran. (2) COBOL was designed for business applications. (3) Java was designed mainly for web applications. (4) Aliasing means that a pointer may point to two or more memory places. (5) Today readability is becoming increasingly important in evaluating modern programming languages. (6) A type coercion example is that operator + can be redefined by the user to summarize two vector variables. (7) Given the following declaration, A: array of (1..10) of integer B, C: array of (1..10) of integer A, B and C are type compatible under name type compatibility. (8) For the declaration in (7), A and C are type compatible under name structure compatibility. (9) Type checking can only be performed at compile-time to find type errors. (10) The reason that decimal number type is sometimes included as a primitive data type is: it supports accurate computation within range while floating point numbers cannot. (11) Given the follow array declaration, (a) int func1(int LEN) { int arr1[len];.} (b) int func2(int LEN){ int*arr2; arr2=(int*)malloc(len*4); } Approach (a) is more efficient. (12) C programming language can be precisely defined using EBNF (extended BNF) formalism. (13) A BNF grammar is considered as ambiguous if every sentence in the corresponding language has two or more parse trees. (14) A heap-dynamic array is an array whose size can change --- shrink and expand, at runtime. (15) The use of enumeration type in C is safe since its value is mapped to integers such that type checking can be performed thereafter. (16) A dangling pointer refers to a data block in heap and there is no pointer pointing to the data block. (17) Garbage refers to a data block that is allocated in heap but is not accessible to the user program. Page 2
2. (21 points) Brief answer following questions: (1) (2 points) We use (what formalism) to describe tokens. We use (what formalism) to describe the syntax structure of C programming language. (2) (3 points) We usually use attributed grammar to describe static semantics, what are the names of two types of attributes? Give an example for each of them. You don t have to define. Filling at least three blankets. (a) example (b) example (3) (2 points) List the names of three commonly used approaches to define the dynamic semantics of a programming language. You don t have to explain each approach. List at least two. (a) (b) (c) (4) (2 points) Compute the weakest condition for the following assignments with given postcondition. Declaration of variables x, y, and z: int x, y, z; { } y = 10 + y; z=4; x= y + z; { x > 0 } (5) (4 points) Compare the advantages and disadvantages of compilation and interpretation approaches. Page 3
(6) (4 points) Explain or give an example to show that reference counting cannot handle circular linked garbage objects. (7) (4 points) Considering the following C program void fun1(void) { int a, b, c; /*definition 1*/ void fun2(void) { int a,c; /*definition 2*/ } void fun3 (void) { int a; /*definition 3*/ point X: use a, use c; } } When we have calling sequence fun1() fun2() fun3(), at point X, (1) According to static scoping, for use a, a is the one defined in (2) According to static scoping, for use c, c is the one defined in (3) According to dynamic scoping, for use a, a is the one defined in (4) According to dynamic scoping, for use c, c is the one defined in 3. (5 points) Given the following grammar, E E + F E F F F F * T T T a b Construct the parse tree for the sentence a + b * a. Page 4
4. (3 points) Compute array address. Assume (a) The array declaration defines an array with 100 row x 100 column integer elements char a[100][100]; (b) Each character value takes 1 byte; (c) The starting address of the array is 0, i.e. the first item has address 0; (d) The lower bound of the index range of each dimension is one (1). For the array element a[3][6] If we use row major to store the array elements in the memory, its address is OR write down the mapping function 5. (4 points. choose one(1) of the following two problems to solve. There are 3 extra points if you do both and both are correct.) Regular expression or BNF: (1) Write a regular expression to satisfy following requirements: (a) The alphabet contains two characters: 0 and 1 ; (b) The number of appeared 1 s is an even number; For example, 0100001, 00011, 0110 are legal strings while 000001, 01101101 are not. Page 5
(2) Write a BNF grammar for the following language to satisfy following requirements: (a) The alphabet contains two characters: [ and ] ; (b) [ s are NOT required to be next to each other. Neither do ] s; (c) A legal sentence in the language has matching number of [ and ]. For example, [[]], [[[]]], [[][[]]], [[]][][][] are legal sentences; but [[][ [][][[[ are not. Page 6