1 Spring 2016 (Monday, March 21) Name: CS 3360 Design and Implementation of Programming Languages Exam 1 This test has 18 questions and pages numbered 1 through 6. Reminders This test is closed-notes and closed-book. However, you are allowed to bring 1 page (8.5 X 11) of notes (both sides). Your notes must be your own, they must be hand written, and they must be turned in with your test. This test is to be done individually, and you are not to exchange or share materials with other students during the test. If you need more space, use the back of a page. Note when you do that on the front. This test is timed. Your test will not be graded if you try to take more than the time allowed. Therefore, before you begin, please take a moment to look over the entire test so that you can budget your time. Clarity is important; if your writing or code is sloppy or hard to read, you will lose points. Correct syntax also makes some difference. There are 100 points all. 1. (4 points) There are many different criteria for evaluating programming languages, including readability, writability, reliability, overall cost, and so on. Among these, the most important criterion for judging a programming languages is. Justify your answer by stating the reason. 2. (2 points) The design and evaluation of a programming language is highly dependent on the domain in which it is to be used. Which of the followings is an incorrect association between a programming language and its application area? (a) Fortran for scientific applications (b) COBOL for business applications (c) C for systems programming (d) Lisp for artificial intelligence (e) Java for scripting 3. (2 points) A language is said to be if it has a relatively small set of primitive constructs that can be combined in a relatively small number of ways to build the control and data structures of the language. Such a language is easy to learn and read because every possible combination is legal without an exception and the meaning is context independent.
2 4. (2 points) An attribute grammar is a BNF with three additions: attributes, attribute computation functions, and predicate functions. Among the three additions, the role of is to state the semantic rules or constraints of a grammar rule. 5. (2 points) Which of the following statements are incorrect about PHP strings? (a) A string can be expressed by enclosing it in a pair of single or double quotes, e.g., Hello and Hello. (b) There is no semantic difference between the use of single and double quotes in expressing a string. (c) A special character can be contained in a string by preceding it with a escape character. (d) Two strings can be concatenated using the dot (.) operator. 6. (2 points) The address of a variable (l-value) is the machine memory address with which it is associated. Which of the followings is an incorrect statement about the address of a variable? (a) A variable may have different addresses at different times during execution. (b) The same name my have different addresses at different places in a program. (c) If two variable names can be used to access the same memory location, they are called aliases. (d) Aliases are harmful to program efficiency. 7. (2 points) Which storage binding scheme is described below? Storage bindings are created for variables when their declaration statements are elaborated. It allows recursion and conserves storage, however, it incurs overhead of allocation and deallocation and doesn t support history sensitive variables for subprograms. (a) Static (b) Stack-dynamic (c) Explicit heap-dynamic (d) Implicit heap-dynamic 8. (2 points) There are several design choices regarding the length of string values, and Java uses: (a) Static length (b) Limited dynamic length (c) Dynamic length. (d) All of above 9. (2 points) A(n) type is one in which the range of possible values can be easily associated with the set of positive values. Examples in Java include integers, characters, and booleans. 10. (5 points) The major methods of implementing programming languages are compilation, pure interpretation, and hybrid implementation. Which method does Java use and why?
3 11. (10 points) Define what binding is, state the difference between static binding and dynamic binding, and give an example of each in terms of binding of variable attributes in Java. 12. (15 points) This question is about the ambiguity of a grammar. (a) When a grammar is said to be ambiguous? (b) Prove or disprove the ambiguity of the following grammar. <E> -> <E> + <E> 5 (c) Rewrite the above grammar to make the + operator right associative.
4 13. (10 points) Write a BNF grammar for the language of palindromes. Example sentences are aba, pop, and a man a plan a canal panama. You may assume that a non-terminal <letter> is already defined. 14. (10 points) The BNF grammar below defines an identifier that has the form: a letter followed by a string consisting of letters and digits. Define an attribute grammar to impose a length limitation, say up to 32 characters, in the identifier. <ident> -> <letter> <ident> -> <letter> <letter-or-digits> <letter-or-digits> -> <letter> <letter-or-digits> <letter-or-digits> -> <digit> <letter-or-digits> <letter-or-digits> -> <letter> <letter-or-digits> -> <digit>
5 15. (5 points) There are five different subscript and storage bindings possible for arrays. Name and explain the approach used by Java. 16. (5 points) Suppose a programming language L that supports multi-dimensional arrays. Your program written in L contains a two dimensional array, say A[50,100]; each element of the array requires 4 bytes of memory. You learned that A[0,0] and A[2,10] are stored at memory addresses 1000 and 1840, respectively, on a byte-addressable machine. Determine the memory address where A[30,50] is stored. 17. (5 points) In PHP, any value is either true or false. List all false values.
6 18. (15 points) Write a PHP function named diff that takes two arrays, say a and b, and returns a new array. The result array should contain only those elements of a that are not present in b. See sample outputs below. Do not use any standard array manipulation functions such as array diff. diff(array(1,2), array(1,3)) ===> array(2) diff(array(1,1,2), array(2)) ===> array(1,1) diff(array(1), array("1")) ===> array(1) diff(array("a"), array("a")) ===> array()