COP4020 Fall 2006 Final Exam

Similar documents
Chapter 10. Implementing Subprograms ISBN

G Programming Languages - Fall 2012

LECTURE 18. Control Flow

G Programming Languages - Fall 2012

Implementing Subprograms

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Programming Languages: Lecture 12

Chapter 10. Implementing Subprograms

COP4020 Programming Languages. Names, Scopes, and Bindings Prof. Robert van Engelen

Run-Time Environments

6. Names, Scopes, and Bindings

UNIT 3

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

Control Abstraction. Hwansoo Han

COP5621 Exam 4 - Spring 2005

G Programming Languages - Fall 2012

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Chapter 9 :: Subroutines and Control Abstraction

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Programming Languages

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Chap. 8 :: Subroutines and Control Abstraction

Names and Abstractions: What s in a Name?

An Activation Record for Simple Subprograms. Activation Record for a Language with Stack-Dynamic Local Variables

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

Principles of Programming Languages

Implementing Subprograms

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 97 INSTRUCTIONS

Chapter 8 :: Subroutines and Control Abstraction. Final Test. Final Test Review Tomorrow

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

Attributes of Variable. Lecture 13: Run-Time Storage Management. Lifetime. Value & Location

Special Topics: Programming Languages

Chapter 7 Control I Expressions and Statements

References and pointers

NOTE: Answer ANY FOUR of the following 6 sections:

LECTURE 15. Names, Scopes, and Bindings: Example Problems

CIT Week13 Lecture

LECTURE 14. Names, Scopes, and Bindings: Scopes

Concepts Introduced in Chapter 7

MIDTERM EXAM (Solutions)

CSE 307: Principles of Programming Languages

Chapter 5. Names, Bindings, and Scopes

CS 415 Midterm Exam Fall 2003

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return

Chapter 9. Def: The subprogram call and return operations of a language are together called its subprogram linkage

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

Programming Languages

Weeks 6&7: Procedures and Parameter Passing

CSCI Compiler Design

Introduction to Programming Using Java (98-388)

Run-time Environments - 2

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

CS 314 Principles of Programming Languages. Lecture 13

Chapter 3:: Names, Scopes, and Bindings

System Software Assignment 1 Runtime Support for Procedures

The role of semantic analysis in a compiler

CS 345. Functions. Vitaly Shmatikov. slide 1

Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

Chapter 3:: Names, Scopes, and Bindings (cont.)

Chapter 8 ( ) Control Abstraction. Subprograms Issues related to subprograms How is control transferred to & from the subprogram?

Chapter 3:: Names, Scopes, and Bindings (cont.)

CS 415 Midterm Exam Spring 2002

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CMSC 331 Final Exam Section 0201 December 18, 2000

CSCI312 Principles of Programming Languages!

Chapter 5 Names, Bindings, Type Checking, and Scopes

CS4215 Programming Language Implementation

Topic IV. Block-structured procedural languages Algol and Pascal. References:

Compilers. 8. Run-time Support. Laszlo Böszörmenyi Compilers Run-time - 1

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

CSC 533: Organization of Programming Languages. Spring 2005

CSE 3302 Notes 7: Control Abstraction

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

Programming Languages & Paradigms PROP HT Course Council. Subprograms. Meeting on friday! Subprograms, abstractions, encapsulation, ADT

Subprograms. Copyright 2015 Pearson. All rights reserved. 1-1

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

Qualifying Exam in Programming Languages and Compilers

MIDTERM EXAMINATION - CS130 - Spring 2003

1 Lexical Considerations

Today's Topics. CISC 458 Winter J.R. Cordy

CS558 Programming Languages. Winter 2013 Lecture 3

Run Time Environments

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

Compiler Theory. (Semantic Analysis and Run-Time Environments)

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

Implementing Subprograms

14. Exception Handling

CMSC 4023 Chapter 9. Fundamentals of Subprograms Introduction

Run-time Environments

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Run-time Environments

The Procedure Abstraction Part I: Basics

Run-time Environments - 3

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

COP4020 Programming Languages. Subroutines and Parameter Passing Prof. Robert van Engelen

The Procedure Abstraction

Special Topics: Programming Languages

Transcription:

COP4020 Fall 2006 Final Exam Name: (Please print) Put the answers on these sheets. You can collect 100 points in total for this exam. 1. Consider the following Ada program fragment: search: loop i := i+1; exit search when a[i]<0; j := j-1; exit search when b[j]<a[i]; end loop search; What kind of construct is this? (mark one) (4 points) (a) a nondeterministic statement (b) an enumeration-controlled loop (c) a logically-controlled mid-test loop (d) a selection statement 2. What are named parameters? (mark one) (4 points) (a) they allow formal parameter names to be explicitly bound to actual parameter values in subroutine calls (b) parameters that are used in catch handlers of exceptions in C++ (c) parameters that adopt parameter passing by name (d) parameters with in-out modes in Ada 3. Consider the following Prolog definitions: wishes(andy, [ball,bike,rocket]). wishes(buzz, [rocket,laser]). wishes(mary, [doll,ball,scarf]). share_toy(t) :- wishes(x,a), wishes(y,b), \+(X=Y), member(t,a), member(t,b). Which of the following queries are successful? (mark one or more) (4 points) (a) share_toy(x) (b) share_toy(doll) (c) share_toy(ball) (d) share_toy(buzz) 1

4. What is the formula that determines the number of iterations of the Fortran loop: DO I=A,B,S END DO where A is the start value, B the end value, and S the step size? (mark one) (4 points) (a) B A + 1 (b) max( B A+S S, 0) (c) max(b A + S, 0) (d) S (B A) 5. In-mode parameters in Ada are implemented by (mark one or more) (4 points) (a) call by value (b) call by reference (c) call by result (d) call by name 6. Consider the following pseudo code program fragment: subroutine swap(a,b) begin t := a; a := b; b := t; end Suppose that the programming language adopts the reference model for variables (similar to Java) and thus the parameters are passed by sharing. What is the result after calling swap(x,y) on variables x and y? (mark one) (4 points) (a) the values of x and y are interchanged (b) the values of x and y are unchanged (c) the value of x is set to y and y is unchanged. (d) the values of x and y are set to the value of t 7. What happens when a dynamically allocated object s lifetime exceeds its binding lifetime indefinitely (until program termination)? (mark one) (4 points) (a) produces dangling references to the object (b) gives a runtime error (c) introduces a memory leak (d) the object is still in scope 2

8. After executing the code in a catch handler in C++ when an exception occurred, where does the program normally continue? (mark one) (4 points) (a) in the caller of the current function (current function exits) (b) the statements that immediately follow the throw statement in the try block (c) the statements that immediately follow the last catch clause (d) the statements in another catch clause for the same exception 9. Show the typical layout of a stack-allocated subroutine frame, label the frame s slots, and describe the content of each slot. (7 points) 10. Iterators in Java and C++ are called iterator objects. Other programming languages, notably Clu, Python, Ruby, and C#, implement true iterators. Explain the difference. (7 points) 3

11. Tail-recursive functions: (a) Give an example of a tail-recursive function. (4 points) (b) Optimize this function using tail-recursion optimization. (4 points) 4

12. Consider the following diagram depicting subroutine nesting levels and the stack frames after a sequence of subroutine calls: Nesting Stack frames A B D C B C D D C A Draw the static links. Suppose subroutine D now accesses a variable declared in A. How many static links have to be traversed to access this variable? (6 points) 13. What is backward chaining and how is it used as a resolution strategy in Prolog? (8 points) 5

14. Consider the program: procedure P(n:real, k:integer) var x:real; procedure Q(m:integer) var n:real; procedure R(k:real) var x:real; begin (* begin of R *) end (* end of R *) begin (* begin of Q *) <== (*) end (* end of Q *) begin (* begin of P *) end (* end of P *) What is the reference environment at the location marked (*) in this program? List the subroutines that can be called at (*) and the variables and arguments that can be accessed (for variables and arguments, provide answers in the form n of Q )? (8 points) 6

15. Consider the following pseudo-code program: x : integer /* global */ y : integer /* global */ procedure one(p : procedure) x : integer x := y y := 2 P() procedure two x := x+y procedure three write_integer(x) begin /* main program */ x := 0 y := 1 one(two) three() end /* main program */ Assuming static scoping or dynamic scoping rules are used (with shallow or deep bindings). What value does the program print? (8 points) Output: Static Scoping Dynamic Scoping Dynamic Scoping with Shallow Binding with Deep Binding 16. What is the value printed by the following pseudo-code program for each of the four parameter passing modes shown in the table? (8 points) a : integer /* global variable */ b : integer /* global variable */ procedure p(x : integer, y : integer) a := y; x := x + y; begin /* main program */ a := 2 b := 1 p(a, b) write_integer(a) end /* main program */ Output: By value By reference By value/result By name 7

17. Consider the following outline of a Queue class in C++ that includes exception handling. class EmptyQueue {; class QueueOverflow {; class OutOfMemory {; class Queue { public: Queue Queue() { if () throw OutOfMemory(); ; int pop() { if () return ; else throw EmptyQueue(); ; void push(int elt) { if () else throw QueueOverflow(); private: // other Queue stuff ; void sample() { Queue q(); // <----- (1) try { q.pop(); // <----- (2) q.push(); // <----- (3) catch(emptyqueue) { // <--- Handler (A) catch(outofmemory) { // <--- Handler (B) void main() { try{ sample(); // <----------- (4) catch(queueoverflow) { // <--- Handler (C) catch(outofmemory) { // <--- Handler (D) // <------------- (5) There are three types of exceptions associated with the Queue class. The Queue class methods can only raise the exceptions shown in the program outline above. Suppose an exception is raised by one of the three statements (1), (2), and (3). Indicate in the table below which handlers are executed (A D) and where the execution continues (1-5)? (8 points) Exception caused by (1) (2) (3) Handlers executed and continuation point 8