DOC PREVIEW
UA CSC 520 - Final Exam

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiProb 12345I IIIIIΣiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiMax 12 12 12 12 12 26 26 26 100(+...)iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiScoreiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccCSc 520 final exam Wednesday 13 December 2000TIME = 2 hoursWrite all answers ON THIS EXAMINATION, and submit it IN THE ENVELOPE at the end of theexam.Do four (4) of the ‘‘short’’ problems (1-5)Do two (2) of the ‘‘long’’ problems (I-III).You may do ONE AND ONLY ONE additional problem for extra credit. If you do so, circle theextra credit problem in the table above or else only 6 problems will be counted.NAME_____Solutions______________________GRADE_________Short Answer (do 4)1. Ordinary Variables and Pointer VariablesConsider the following two program fragments:iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiicccccciiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiccccccx=6; *p=6;y=7; *q=7;foo(x); foo(*p);Program A Program B--------- ---------int x; int y; int *p; int *q;____________ _____________where the definition of the called function is:void function foo( int a) {printf("output = %d", a);}(a) At first sight, one might conclude that the call to foo in both programs A and B will print thesame value. This is not true. Fill in the blank in Program B with a statement that will causeProgram A and Program B to print different results.(b) Explain, in words or using diagrams, what happens in each program to cause foo to print dif-ferent values.Solution(a) p=q(b) p and q are pointer aliased, so that both assignments write to the same location. *q=7makes *p equal to 7 and so the call is to foo with by-value argument 7. In Program A, thiscannot happen, since separately named variables are bound to separate references (locations).final-2-2. Parameter TransmissionCould the instructions a+=2and a=a+2ever behave differently? This question isexplored below.Consider the following two function definitions:Program A Program B--------- ---------void function increment( int a){ void function assign( int a){a+=2; a=a+2;}}Consider the possible actual arguments (arg) in a call to each of these functions. Could a call toincrement(arg) have a different side-effect from a call to assign(arg)(a) If parameter transmission uses call by copy-in/copy-out?(b) If parameter transmission uses call by value/result?(c) If parameter transmission uses call by reference?(d) If parameter transmission uses call by name? In each of your answers above, if they cannot havea different side-effect, explain why not. If they can have a different side-effect, give an exampleand explain.Solution(a) No. At call time, the l-value of the actual argument is saved for copy-out, and fixed at this time,unaffected by any later side-effects. The r-value is copied into a at call. Local a is updatedby two by either instruction, and this new r-value is copied back to the saved l-value. Since theeffect on a is the same, both give same result.(b) No. In both cases the value copied back will be incremented by 2, although in some cases thecopy-back will be to a different location: Suppose i=1in the caller and the actual argumentis a[i++]. When evaluated the first time, this will give the l-value of a[1], and this is whatwill be passed by value; call it v. At return time, a[i++] will again be evaluated with the l-value a[2]; this is the location that will receive v+2.(c) No. Since both instructions, applied to the same l-value will give the same result, all is well.(d) Yes. Suppose the call is with argument a[i++] with i=1in caller, and a[1] set to 10.Then a+=2references this once and so a[1] will get 12, with i=2at return.But a=a+2be equivalent to a[i++] = a[i++] + 2. Since the expression isreferenced twice, the location assigned to will be different; depending on order, this will eitherassign 12 to a[2] (different) or a[2]+2 to a[1]; again different.final-3-3. Typing in C and MLThe languages C and ML declare the types associated with names in distinct ways. The table belowcompares a type declaration in C with the equivalent declaration in ML.(a) Fill in the blanks in the table below. A few examples have been filled in already to guide you.CML-------------------------- ----------------------------int x x : intint *p p : int refint **p _______________________int (*a)(char) a : (char -> int) refint *b(char) b : (char -> int ref)int d(char) _________________________int (*d)(int (*) (char)) d : ((char -> int) ref -> int) refint *e(int (*) (char)) ______________________________int f(int (*) (char)) ______________________________(b) In C, a function itself is not a variable, and a function cannot be directly passed to functions orreturned by functions. However, it is possible to define pointers to functions, which can bepassed to functions or returned by functions. Some examples are in (a) above.For the following ML type declarations, fill in the blank with the correct type declaration in C,provided such a definition is legal in C.Ifitisnot legal, fill in ‘‘not legal’’.CML-------------------------- ----------------------------__________________________ q : ((char -> int) ref -> int) ref__________________________ g : ((char-> int) ref -> int)int (*(h(int)))(char) h : (int -> (char -> int) ref)__________________________ j : (int ref -> (char -> int))__________________________ k : ((char -> int) -> int)__________________________ l : ((char -> int) ref -> (char -> int) ref )__________________________ m : ((int -> int) ref -> (int -> int) )final-4-(a)int **p __ p : int ref refint d(char) __ d : char -> intint *e(int (*) (char)) __ e : ((char -> int) ref -> int ref )int f(int (*) (char)) __ f : ((char -> int) ref -> int )(b)_ int (*q)(int (*) (char)) q : ((char -> int) ref -> int) ref__ int g(int (*) (char)) g : ((char-> int) ref -> int)__ not legal j : (int ref -> (char -> int))__ not legal k : ((char -> int) -> int)__ int *l(int (*) char)(char) l : ((char -> int) ref -> (char -> int) ref )__ not legal m : ((int -> int) ref -> (int -> int) )final-5-4. Conditional ExpressionsWe want to add a new kind of Expression to the language IMP (Watt, Example 3.6, page 67),called a conditional expression. A conditional expression has the


View Full Document

UA CSC 520 - Final Exam

Documents in this Course
Handout

Handout

13 pages

Semantics

Semantics

15 pages

Haskell

Haskell

15 pages

Recursion

Recursion

18 pages

Semantics

Semantics

12 pages

Scheme

Scheme

32 pages

Syllabus

Syllabus

40 pages

Haskell

Haskell

17 pages

Scheme

Scheme

27 pages

Scheme

Scheme

9 pages

TypeS

TypeS

13 pages

Scheme

Scheme

27 pages

Syllabus

Syllabus

10 pages

Types

Types

16 pages

FORTRAN

FORTRAN

10 pages

Load more
Download Final Exam
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Final Exam and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Final Exam 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?