MIDTERM EXAMINATION - CS130 - Spring 2005

Similar documents
MIDTERM EXAMINATION - CS130 - Spring 2003

CS 3360 Design and Implementation of Programming Languages. Exam 1

CS 415 Midterm Exam Spring SOLUTION

CS 415 Midterm Exam Spring 2002

CS 3360 Design and Implementation of Programming Languages. Exam 1

CS3360 Design and Implementation of Programming Languages Final Exam May 16, pm to 12:45pm (2 hour and 40 minutes) NAME:

CSCI 3155: Principles of Programming Languages Exam preparation #1 2007

Programming Languages, Summary CSC419; Odelia Schwartz

CS 415 Midterm Exam Fall 2003

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

CMSC 331 Final Exam Section 0201 December 18, 2000

CS 314 Principles of Programming Languages

Common LISP Tutorial 1 (Basic)

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

UMBC CMSC 331 Final Exam

Names, Bindings, Scopes

CPSC 3740 Programming Languages University of Lethbridge. Data Types

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

UMBC CMSC 331 Final Exam

Functional Programming. Pure Functional Programming

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Expressions and Assignment

INSTITUTE OF AERONAUTICAL ENGINEERING

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Com S 541. Programming Languages I

CSCI 3136 Principles of Programming Languages

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

Lisp Basic Example Test Questions

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Comp 311: Sample Midterm Examination

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

Name EID. (calc (parse '{+ {with {x {+ 5 5}} {with {y {- x 3}} {+ y y} } } z } ) )

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Chapter 3 (part 3) Describing Syntax and Semantics

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

NOTE: Answer ANY FOUR of the following 6 sections:

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

SOFTWARE ARCHITECTURE 6. LISP

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CS558 Programming Languages

MIDTERM EXAM (Solutions)

Midterm Examination (Sample Solutions), Cmput 325

COP4020 Spring 2011 Midterm Exam

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

Pierce Ch. 3, 8, 11, 15. Type Systems

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

Introduction to Scheme

1 Lexical Considerations

Note 3. Types. Yunheung Paek. Associate Professor Software Optimizations and Restructuring Lab. Seoul National University

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

Principles of Programming Languages 2017W, Functional Programming

CSC 533: Organization of Programming Languages. Spring 2005

Symbolic Computation and Common Lisp

CMSC 331 Final Exam Section 0201 December 18, 2000

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Control Flow February 9, Lecture 7

Structure and Interpretation of Computer Programs

Type Bindings. Static Type Binding

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes,

Programming Languages

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Organization of Programming Languages (CSE452) Why are there so many programming languages? What makes a language successful?

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

DO NOT OPEN UNTIL INSTRUCTED

by the evening of Tuesday, Feb 6

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

Functional programming with Common Lisp

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

Organization of Programming Languages CS3200/5200N. Lecture 11

CMSC 330: Organization of Programming Languages

Midterm Exam 2 CS313K Logic, Sets, and Functions Spring, 2009

Functional Programming. Big Picture. Design of Programming Languages

CSE413 Midterm. Question Max Points Total 100

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Lisp. Versions of LISP

Structure and Interpretation of Computer Programs

COS 140: Foundations of Computer Science

Types and Type Inference

CS558 Programming Languages

Concepts of Programming Languages

Principles of Programming Languages. Lecture Outline

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

Recursion & Iteration

Chapter 3. Semantics. Topics. Introduction. Introduction. Introduction. Introduction

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

CSCI 334: Principles of Programming Languages. Lecture 2: Lisp Wrapup & Fundamentals. Higher-Order Functions. Activity

Organization of Programming Languages CS3200 / 5200N. Lecture 06

Equivalent Notations. Higher-Order Functions. (define (f x y) ( body )) = (define f (lambda (x y) ) ) Anonymous Functions.

Course outline. CSE 341: Programming Languages. Why study programming languages? Course motivation and objectives. 1 lecture: Concepts

Functions and Recursion. Dr. Philip Cannata 1

Transcription:

MIDTERM EAMINATION - CS130 - Spring 2005 Your full name: Your UCSD ID number: This exam is closed book and closed notes Total number of points in this exam: 231 + 25 extra credit This exam counts for 25% of your overall grade in the class Problem Number of Points Problem 1 15 Problem 2 30 Problem 3 45 Problem 4 16 Problem 5 35 + 25 extra Problem 6 50 Problem 7 40 Total 231 + 25 extra

Problem 1 (History) (15 points) Circle the correct answer for each of the questions below: a. Which language was the first to try to bring together programming for scientific application and for business applications? 1. PL/I 2. ALGOL 60 3. Ada b. Why was separate compilation an important feature of early FORTRAN? 1. it made it possible to detect syntax errors more precisely 2. it made it possible to tolerate frequent machine downtimes 3. it allowed for better code reuse c. What can be said of Smalltalk? 1. it is faster than C++ 2. it is less object-oriented than Java 3. it is more object-oriented than C++ d. What language introduced the concept of a block structure? 1. Ada 2. ALGOL 60 3. SIMULA 67 e. What design criterion was used extensively in ALGOL 68? 1. Readibility 2. Safety 3. Orthogonality

Problem 2 (List Construction in Lisp) a. After evaluating the Lisp expressions (setq l1 (1 2 3)) and (setq l2 (a b c)), what would the value of each of the following Lisp expressions be? 1. (5 points) (cons l1 l2) ((1 2 3) a b c) 2. (5 points) (cons (car l1) l2) (1 a b c) 3. (5 points) (cons (car (cdr l1)) (cons (car (cdr l2)) NIL)) (2 b) b. After evaluating the Lisp expressions (setq l1 (1 2 3)) and (setq l2 (a b c)), give Lisp expressions to construct the following lists. Your expressions can only use car, cdr, cons, nil, l1 and l2. 1. (5 points) (2 a) (cons (car (cdr l1)) (cons (car l2) nil)) 2. (5 points) ((a 1)) (cons (cons (car l2) (cons (car l1) nil)) nil) 3. (5 points) ((2) (a)) (cons (cons (car (cdr l1)) nil) (cons (cons (car l2) nil) nil))

Problem 3 (Semantics) a. (5 points) What is the use of static semantic? What does one use to define static semantic? Used to describe the legal form of programs in ways that a BNF grammar cannot. Attributed grammar. b. (15 points) What are the three kinds of dynamic semantics that we have seen in class. Name one good use of each. Axiomatic semantics. Good for proving program correctness. Denotational semantics. Good basis for an interpreter. Operational semantics. Good for describing the semantics to a programmer. c. (5 points) Compute the weakest precondition for the following statement and postcondition (assume that all variables are integers): z = 2 * (c - 3) - 1 { z > 1 } {c > 4} by the assignment axiom

c. (20 points) Compute the weakest precondition for the following statement and postcondition (assume that all variables are integers): if (a!= 3) a = 2*a-1; else a -= 2; { a > 0 } By the assignment axiom: {a > 0} a = 2*a - 1 (S1) { a > 0} and: {a > 2} a -=2 (S2) { a > 0} We need to find the weakest P such that: {P and (a! = 3)}S1{a > 0}, {P and (a == 3)}S2{a > 0} {P } if (a! = 3) then S1 else S2{a > 0} P = {a > 0} is the weakest precondition because: {P and (a! = 3)} = {P }, which is the weakest precondition for S1{a > 0}. {P and (a == 3)} = {a == 3}, which is a valid precondition for S2{a > 0}.

Problem 4 (Storage Binding) (16 points) Consider the following fragment of C code: int a[4]={1,2,4,8}; int fun (int z) { static int foo=0; int *x; char *y = "hello!"; int i; x = (int *) malloc(10*sizeof(int)); for (i=0;i<10;i++) x[i] = foo++; return (z+2); } int main() {... } For each memory reference below, indicate the area of memory it references (with an in the corresponding column): Reference static area heap stack a[3] x foo y *y i a *x

Problem 5 (Types) a. (10 points) Give two examples of languages that uses static type binding, and two examples of languages that uses dynamic type binding. Static type binding: C, Java Dynamic type binding: Lisp, Perl, Javascript b. (15 points) Give an example of a language construct in a statically typed language that requires dynamic type checking. Give an example of code that causes a type error undetectable by a compiler The C union union { int i; char *s; } x; x.i = 1;... printf(x.s); c. (10 points) What are the trade-offs between name type compatibility and structure type compatibility? which type compatibility scheme do most language use? Name compatibility is easy to implement but very restrictive, while structure compatibility is difficult to implement but more flexible. Most languages use a combination of both (e.g., declaration compatibility).

d. [ETRA CREDIT] Multidimensional arrays can be stored in row major order, as in C, or in column major order, as in Fortran. (i) (5 points) Why it is important for a programmer to know which way arrays are stored by the language (Circle the correct answer): 1. To limit the overhead of loop index computation 2. To increase data locality 3. To save storage space (ii) (20 points) In a way similar to what we ve done in class, write the access function for 3-dimensional column-major arrays. Assume an array a, with dimension sizes m, n, and p, with address of the first element a[1,1,1], and with element size element size. location(a[i,j,k]) = address of a[1,1,1] + ((k-1)*m*n + (j-1)*m + i-1) * element size

Problem 6 (Lisp Functions) a. (15 points) Write a recursive LISP function called getlast that takes a list as input and returns the last element of the list. Do NOT use setq, let, defvar, or any other way to declare variables. Here is an example: CL-USER(1): (getlast (a b c d)) d (defun getlast (l) (if (null (cdr l)) (car l) (getlast (cdr l))))

b. (25 points) Write a recursive LISP function called inserta that takes a list as input and returns a new list in which symbol a has been inserted after every element of the original list. If the input list is empty, then inserta returns the empty list. Do NOT use setq, let, defvar, or any other way to declare variables. Here is an example: CL-USER(1): (inserta (a b c d)) (a a b a c a d a) (defun inserta (l) (cond ((null l) nil) (t (cons (car l) (cons a (inserta (cdr l))))))) c. (10 points) What does the following Common Lisp form evaluate to? (mapcar # (lambda (x) (list x (list x) )) (a b c)) ((a (a)) (b (b)) (c (c)))

Problem 7 (Expressions) Consider the following program in C syntax: int fun (int *i) { *i += 5; return 2; } void main () { int x = 3; if ((x == 2) && (fun (&x))) x++; x = x + fun (&x); printf("%d\n",x); } What is the output of this program assuming: 1. (10 points) Left-to-right operand evaluation (for ALL operators), short-circuited boolean expressions 5 2. (10 points) Left-to-right operand evaluation (for ALL operators), non-short-circuited boolean expressions 10 3. (10 points) Right-to-left operand evaluation (for ALL operators), short-circuited boolean expressions 15 4. (10 points) Right-to-left operand evaluation (for ALL operators), non-short-circuited boolean expressions 15