




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





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.
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).
anapest, antispast, dactyl, foot, iamb, paeon, pyrrhus, spondee, synecdoche
(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.
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.
ra
ra
ra
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)
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