CMSC330 Fall 2013 Practice Problems 6 Solutions

Similar documents
CMSC330 Spring 2008 Final Exam

CMSC 330: Organization of Programming Languages

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

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

CMSC330 Fall 2010 Final Exam Solutions

CMSC330 Fall 2014 Final Exam

CMSC 132: Object-Oriented Programming II

CMSC 330: Organization of Programming Languages. Closures (Implementing Higher Order Functions)

CMSC330 Spring 2012 Final Exam Solutions

CMSC330 Fall 2009 Final Exam

CMSC 330, Fall 2013, Practice Problem 3 Solutions

CMSC 330: Organization of Programming Languages. Closures (Implementing Higher Order Functions)

CMSC330. Objects, Functional Programming, and lambda calculus

Polymorphism. CMSC 330: Organization of Programming Languages. Two Kinds of Polymorphism. Polymorphism Overview. Polymorphism

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

CMSC 330, Fall 2013, Practice Problems 3

CMSC 132: Object-Oriented Programming II

Anonymous Functions CMSC 330: Organization of Programming Languages

Functional Languages and Higher-Order Functions

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

System Software Assignment 1 Runtime Support for Procedures

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Objects. Prof. Clarkson Fall ob-ject: to feel distaste for something Webster's Dictionary. Today s music: Kung Fu Fighting by CeeLo Green

The role of semantic analysis in a compiler

CS558 Programming Languages

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

Chapter 5 Object-Oriented Programming

Weeks 6&7: Procedures and Parameter Passing

ob-ject: to feel distaste for something Webster's Dictionary

SNU Programming Language Theory

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

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

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Some instance messages and methods

Semantic Analysis. Lecture 9. February 7, 2018

Introduction to Software Testing Chapter 2.4 Graph Coverage for Design Elements Paul Ammann & Jeff Offutt

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

CSE 307: Principles of Programming Languages

CSCI-GA Scripting Languages

JAVA MOCK TEST JAVA MOCK TEST II

C10: Garbage Collection and Constructors

CSC 533: Organization of Programming Languages. Spring 2005

CMSC330 Fall 2014 Midterm 1 Solution

Procedure and Object- Oriented Abstraction

Subtyping (Dynamic Polymorphism)

OCaml Language Choices CMSC 330: Organization of Programming Languages

Semantic Analysis and Type Checking

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

A Short Summary of Javali

CMSC 330: Organization of Programming Languages

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Compiler Passes. Semantic Analysis. Semantic Analysis/Checking. Symbol Tables. An Example

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Contents. 8-1 Copyright (c) N. Afshartous

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Data Abstraction. Hwansoo Han

301AA - Advanced Programming [AP-2017]

Parameter Passing Styles

(Not Quite) Minijava

OVERRIDING. 7/11/2015 Budditha Hettige 82

Compaq Interview Questions And Answers

JavaScript: Sort of a Big Deal,

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

Compiler Construction Lent Term 2015 Lectures 10, 11 (of 16)

CS558 Programming Languages

Fundamentals of Programming Languages

Comp 311 Principles of Programming Languages Lecture 21 Semantics of OO Languages. Corky Cartwright Mathias Ricken October 20, 2010

CSE 504: Compiler Design. Runtime Environments

CSE 452: Programming Languages. Previous Lecture. From ADTs to OOP. Data Abstraction and Object-Orientation

Compilers CS S-05 Semantic Analysis

Lecture Notes on Programming Languages

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

Class, Variable, Constructor, Object, Method Questions

Method Resolution Approaches. Dynamic Dispatch

Names, Scopes, and Bindings II. Hwansoo Han

Compilers. Cool Semantics II. Alex Aiken

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

1 Lexical Considerations

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

More on Inheritance. Interfaces & Abstract Classes

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

CMSC 330: Organization of Programming Languages. Objects and Abstract Data Types

Late Binding; OOP as a Racket Pattern

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

CMSC330 Spring 2017 Midterm 2

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

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

IC Language Specification

StackVsHeap SPL/2010 SPL/20

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

22c:111 Programming Language Concepts. Fall Types I

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Object Oriented Programming: Based on slides from Skrien Chapter 2

CS558 Programming Languages

C++ Important Questions with Answers

Transcription:

CMSC330 Fall 2013 Practice Problems 6 Solutions 1. Programming languages a. Describe how functional programming may be used to simulate OOP. An object may be simulated as a tuple, where each element of the tuple is a closures representing a method for the object. b. Describe the difference between OCaml modules and Java classes. Both provide a public definition for a group of functions whose internal details are hidden, but Java classes can also instantiate objects and inherit attributes from other classes (not possible with OCaml modules). c. Describe the difference between strong and weak typing. Strong typing prevents types from being used interchangeably, weak typing allows types to be treated as other types through many implicit type conversions. d. Explain how call-by-name simplifies implementing lazy evaluation. Expressions to be evaluated lazily may be passed as arguments to functions, since function arguments are not evaluated until used. e. Describe the difference between an L-value and an R-value. L-values refer to the address of a symbol, R-values refer to the value for a symbol. f. What is an activation record (frame), and why is it usually allocated on a stack? An activation record contains state information for a function invocation. It is usually allocated on a stack so it can be easily freed upon function return by popping the stack. g. Describe the difference between ad-hoc and parametric polymorphism. Ad hoc polymorphism applies to code supporting a finite range of types whose combinations must be specified, parametric polymorphism applies to code written without mention to type that can transparently support an arbitrary number of types. 2. Function arguments For each code, explain whether g is an upward funarg. a. let f x = let g y = x + y in let app a b = a b in app g 1 ;; g is not an upwards funarg since it is a function parameter passed down to app, not returned by f b. let f x = let g y = x + y in g ;; g is an upwards funarg since it is a function return value for the 2 nd let A funarg is simply a function argument where the function is either 1. Passed as a parameter to a function call 2. Returned as the return value of a function call

3. Static vs. Dynamic Scoping Consider the following OCaml code. let a = 1 ;; let f = fun ( ) -> a ;; // value of a determined here for static scoping let a = 2 ;; f ( );; // value of a determined here for dynamic scoping a. What value is returned by the invocation of f( ) with static scoping? Explain. 1, since the binding for a in the function f = fun ( ) -> a refers to the closest lexical value of a at the point where the function is declared in the code (1 st let a). b. What value is returned by the invocation of f( ) with dynamic scoping? Explain. 2, since the binding for a in the function f = fun ( ) -> a refers to the closest value of a in the call stack at the point where the function is actually invoked (2 nd let a). Consider the following OCaml code. let app f w = let x = 1 in f w ;; // value of x determined here // for dynamic scoping let add x y = let incr z = z+x in app incr y;; // value of x determined here // for static scoping (add 2 3) ;; c. What is the order of invocation for the functions app, add, and incr when evaluating the expression (add 2 3)? 1) add, 2) app, 3) incr incr is defined in add but not invoked until reaching the body of app (as f). d. What value is returned by (add 2 3) with static scoping? Explain. 5, since the binding for x in the function incr refers to the closest lexical value of x (add x) at the point where the function is declared in the code. e. What value is returned by (add 2 3) with dynamic scoping? Explain. 4, since the binding for x in the function incr refers to the closest value of x in the call stack (let x = 1) at the point where the function is actually invoked (by app f w in f w).

4. Parameter passing Consider the following C code. int i = 2; void foo(int f, int g) { f = f-i; g = f; int main( ) { int a[] = {2, 0, 1; foo(i, a[i]); printf("%d %d %d %d\n", i, a[0], a[1], a[2]); a. Give the output if C uses call-by-value 2 2 0 1, since the call to foo( ) creates 2 local variables f & g (initialized with the values of i & a[i]), and all changes to f & g do not affect i or a[i]. b. Give the output if C uses call-by-reference 0 2 0 0, since the call to foo( ) binds f to i & g to a[2], invoking foo( ) = void foo(f i, g a[2]) { f = f i; // equivalent to i = i i i = 0 g = f; // equivalent to a[2] = i a[2] = 0 c. Give the output if C uses call-by-name 0 0 0 1, since the call to foo( ) replaces f with i & g with a[i], foo( ) = void foo(f i, g a[i]) { f = f i; // equivalent to i = i i i = 0 g = f; // equivalent to a[i] = i a[0] = 0 5. Lazy evaluation Given the following OCaml code. let doif p x = if p then x else 0 ;; let rec loop n = loop n ;; doif false (loop 0) ;; a. What is the result of evaluating the doif expression if OCaml uses call-by-value? Infinite loop trying to evaluate loop 0 before its value is passed to doif. b. What is the result of evaluating the doif expression if OCaml uses call-by-name? 0, since loop 0 is directly passed to doif and is not evaluated if p is false. c. Rewrite the code (using thunks) so that the result of evaluating the doif expression is the same as if OCaml used call-by-name, even though OCaml uses call-by-value. let doif p x = if (p ( )) then (x ( )) else 0 let rec loop n = loop n doif (fun ( ) -> false) (fun ( ) -> (loop 0))

6. Garbage collection Consider the following Java code. Object a, b, c; public foo( ) { a = new Object( ); // object 1 b = new Object( ); // object 2 c = new Object( ); // object 3 a = b; b = c; c = a; a. What object(s) are garbage when foo( ) returns? Explain why. Object 1 is garbage there are no longer any references to it within the program. After foo( ) returns, a object 2, b object 3, c object 2. b. Describe the difference between mark-and-sweep & stop-and-copy. 7. Polymorphism Mark-and-sweep stops the program to determine what objects are still reachable. Stop-and-copy in addition will move reachable objects to new locations. Consider the following Java classes: class A { public void a( ) { class B extends A { public void b( ) { class C extends B { public void c( ) { Explain why the following code is or is not legal a. int count(set<a> s) { count(new TreeSet<A>( )); Legal. Actual parameter type (Set<A>) matches formal parameter type (Set<A>) b. int count(set<a> s) { count(new TreeSet<B>( )); Illegal. Actual parameter type (Set<B>) is not a subclass of formal parameter type (Set<A>), even though B is a subclass of A. c. int count(set s) { count(new TreeSet<A>( )); Legal. Type erasure will cause formal parameter type (TreeSet<A>) to become TreeSet, which matches actual parameter type (Set). d. int count(set<?> s) { count(new TreeSet<A>( )); Legal. Actual parameter type (Set<A>) matches formal parameter type (Set<?>), since? matches A. e. int count(set<? extends A> s) { count(new TreeSet<B>()); Legal. Actual parameter type (Set<B>) matches formal parameter type (Set<? extends A>), since? extends A can match A and its subclasses B & C (classes that extend A, including A)

f. int count(set<? extends B> s) { count(new TreeSet<A>()); Illegal. Actual parameter type (Set<A>) does not match formal parameter type (Set<? extends B>), since? extends B can match only B and its subclass C (classes that extend B, including B) g. int count(set<? extends B> s) { for (A x : s) x.a( ); Legal. The actual parameter type (Set<? extends B>) indicates s contains elements of class B or its subclasses. So any element of s may be treated as an object of class B or its subclasses (e.g., C). The for loop treats elements of s as objects of class A, which is a superclass of B, and thus is legal (can use subclass in place of superclass). h. int count(set<? extends B> s) { for (C x : s) x.c( ); Illegal. The actual parameter type (Set<? extends B>) indicates s contains elements of class B or its subclasses. So any element of s may be treated as an object of class B or its subclasses (e.g., C). The for loop treats elements of s as objects of class C, and is illegal since elements of s may be objects of class B (cannot use superclass in place of subclass). i. int count(set<? super B> s) { for (A x : s) x.a( ); Illegal. The actual parameter type (Set<? super B>) indicates s contains elements of class B or its superclasses. So any element of s may be treated as an object of class B or its superclasses (e.g., A, Object). The for loop treats elements of s as objects of class A, and is illegal since elements of s may be objects of class Object (cannot use superclass in place of subclass). j. int count(set<? super B> s) { for (C x : s) x.c( ); Illegal. The actual parameter type (Set<? super B>) indicates s contains elements of class B or its superclasses. So any element of s may be treated as an object of class B or its superclasses (e.g., A, Object). The for loop treats elements of s as objects of class C, which is not included and thus illegal.