Docsity

Python Programs - Programming Languages and Compilers - Exams, Exams of Programming Languages

Toggle sidebar
bg1
UNIVERSITY OF CALIFORNIA
Department of Electrical Engineering
and Computer Sciences
Computer Science Division
CS 164 P. N. Hilfinger
Spring 2010
CS 164: Test #2 (revised)
Name: Login:
Login of person to your left: and to your right:
You have one hour and twenty minutes to complete this test. Please put your login on each
sheet, as indicated, in case pages get separated. Answer all questions in the space provided
on the exam paper. Show all work (but be sure to indicate your answers clearly.) The exam is
worth a total of 20+ points (out of the total of 200), distributed as indicated on the individual
questions.
You may use any notes or books you please—anything unresponsive. We suggest that you
read all questions before trying to answer any of them and work first on those about which
you feel most confident.
You should have 7 problems on 8 pages.
1. /4
2. /3
3. /
4. /2
5. /4
6. /3
7. /4
TOT /20
1
pf3
pf4
pf5
pf8

Partial preview of the text

Download Python Programs - Programming Languages and Compilers - Exams and more Exams Programming Languages in PDF only on Docsity!

UNIVERSITY OF CALIFORNIA

Department of Electrical Engineering and Computer Sciences Computer Science Division

CS 164 P. N. Hilfinger Spring 2010

CS 164: Test #2 (revised)

Name: Login:

Login of person to your left: and to your right: You have one hour and twenty minutes to complete this test. Please put your login on each sheet, as indicated, in case pages get separated. Answer all questions in the space provided on the exam paper. Show all work (but be sure to indicate your answers clearly.) The exam is worth a total of 20+ points (out of the total of 200), distributed as indicated on the individual questions. You may use any notes or books you please—anything unresponsive. We suggest that you read all questions before trying to answer any of them and work first on those about which you feel most confident. You should have 7 problems on 8 pages.

TOT /

  1. [4 points] Give short answers to the questions below.

a. Java allows multiple inheritance only in the sense that a class may implement any number of interfaces, which don’t contain method bodies or instance variables, but may only extend one class. C++ allows classes to extend (in the Java sense) any number of classes. In lecture, I showed that this complicates the implementation of instance method calls in C++ relative to Java. Suppose that Java used a restriction on multiple inheritance halfway between C++ and current Java: interfaces may have method bodies (not just abstract methods), but not instance variables. For a definition such as

class A extends B implements I 1 , I 2 , ... { }

if we inherit a method body (that is, a non-abstract method) from both B and one or more of the Ik, we use the one from B. If we inherit a method body from both Ij and Ik where j < k, we use the one from Ij. How (if at all) would this change simplify the implementation of multiple inheritance for Java relative to C++?

b. In standard Python, every object instance has a dictionary that maps names of at- tributes (the ‘a’ in ‘x.a’) to their values (which may be methods as well as ordinary object references). But Java, on the other hand, has fixed tables, generated at com- pile time, containing method pointers and does not keep any kind of look-up structure around for routine uses of instance methods and instance variables. Illustrate why Python requires its use of dictionaries (that is, supply an example that shows why the Java implementation strategy cannot work and briefly explain why).

  1. [1 point] Which of the following terms least belongs?

anapest, antispast, dactyl, foot, iamb, paeon, pyrrhus, spondee, synecdoche

  1. [2 points] Give the simplest NDFA you can that recognizes the same language as the regular expression

(ab+c)(a|b)

Do not use more than 6 states (5 actually suffice). In particular, don’t use the result of applying the general regular expression to NDFA construction.

  1. [4 points] Consider the following toy program:

def g(f, n, s): if n <= 0: return f(s) else: return g(f, n-1, s+n) def h(p): def scale(x): return x * p # Stop here print g(scale, 1, 0) h(42)

Assuming that we restrict Python (as we will in Project 3) so that functional values that survive beyond the scope in which they are defined are invalid (so that you may call the value of lambda x: x+y, or pass it as a parameter, but may not return it from a function and then call it), complete the diagram of the contents of the stack at the point where execution reaches the spot marked “Stop here.” In particular, show where dynamic (DL) and static (SL) links point, and values and loca- tions of actual parameters on the stack (don’t forget the value of f). We’re assuming an ia32 architecture.

SL

DL

ra

SL

DL

ra

SL

DL

ra

SL

DL

ra

Top of stack

scale

g

g

h

main

e. class A(object): x = 3 def f(self, y): self.x = y

class B(A): def g(self, y): self.x = self.x + y

class C(B): def h(self): self.g(self.y)

x::C = C() C.h()

f. def f(x): global y y = x

y::String = "" f(12)

  1. [4 points] For each of the type rules below (for a statically typed language), indicate whether it allows illegal programs, disallows legal programs, or neither, assuming standard interpretations of the program constructs. Predicates typeof, defn, and subtype are as de- scribed in the lecture notes: typeof(V, T, E) means the static type of expression V is T in environment E (a list of the form [def(V 1 , T 1 ),.. .,def(Vn, Tn)] for variables Vi and types Ti); defn(I, T, E) means that identifier I has static type T in environment E, and subtype(T, T ′) means that T is a subtype of T ′^ (possibly equal). If a rule is wrong, explain why by giving an example of an excluded legal program or a permitted illegal program.

a. Assume that list(T ) is intended to refer to a Python-style list (as in [1,2,3]), but with statically typed elements, and that setitem(X,I,E1) is the AST for ‘X[I]=E1’. Assume here that assignment statements have the types of their left operands.

typeof(setitem(X,I,E1), T, Env) :- typeof(X, list(T), Env), typeof(E1, T1, Env), subtype(T1,T).

b. Here, suite([S 1 ,... , Sn]) is intended to represent a suite of statements as in our Python dialect, with the same semantics, and assign(X,T,E) stands for the assignment X::T=E (again according to our rules).

typeof(suite([]),void,_). typeof(suite([assign(X,T,E) | Rest]), void, Env) :- typeof(E, T1, Env), typeof(suite(Rest), void, [def(X,T) | Env]), subtype(T1, T). rules for suites that start with other kinds of statements