c++作业 | 作业javascript | 代做thread | java – CSCI-UA.490, Spring 2020

CSCI-UA.490, Spring 2020

c++作业 | 作业javascript | 代做thread | java – 这道题目是利用java进行的编程代写任务, 涵盖了c++/javascript/thread/java等方面

java代写 代写java

CSCI-UA.490, Spring 2020

CSCI-UA.490 Final Exam

This is a open-book one hour fifty minute exam. The maximum possible score is 75 points. Make sure you print your name legibly. All of the intended answers may be written within the space provided. You may use the back of the preceding page for scratch work.

Prob # 1 # 2 # 3 # 4 Total
Max 20 20 15 20 75

1 .(20 points)…………………………………………………………… Scope

(a)(5 points)What is the result of evaluating the following piece of code?
var i = 0;
var callback0 = function A() {
console.log(i); // ***
i = 1;
var callback1 = function B() {
(b)(10 points)Fill in the missing parts in the following diagram of the run-time struc-
tures for the execution of this code up to the point immediately before the function
returns (marked with***). For each empty blank, write in either the value, or the
number/letter of the appropriate activation record, closure or code snippet. There
may be more entries in the box or closure boxes than are required; leave unused
entries blank. Dont forget to add bindings for function definitions. If you are an-
swering this question on a separate sheet, simply specify which activation record /
closure (e.g., 1, 2, X or Y) you are giving answers for, before providing your answers.
Activation Records Closures Compiled Code
(1) top access link (none) X:____,code for____ code for A
(2) A() access link Y:_____,code for____ code for B

(c)(5 points)Ben Bitdiddle has been working on a version of this code generalized to work for arbitrarily many callbacks: for ( var i = 0; i < N; i++) { callbacks[i] = function A() { console.log(i); } }

Unfortunately, each callback always printsN. Modify this code so each callback
prints a number corresponding to the value ofiat the time the callback was allo-

2 .(15 points)………………………………………………….. .Virtual tables

(a)(10 points)Consider the following  c++ code:
class A {
int a;
class B {
int b;
virtual void f();
virtual void g();
class C : public A, B {
int c;
virtual void f() override;
virtual void h();
Fill in the object layout and vtables for aCobject. For each entry in the vtable,
indicate the function to which the slot points (e.g.,B::f), and the absolute value of
anyadjustment necessary to call the function in question (sign doesnt matter; 1
means you need to adjust the pointer by one slot). There may be more slots in the
object / vtable than are necessary to answer this question. If you are answering this
question on a separate sheet, simply specify if you are giving answers for the object
layout, or which virtual table (A or B) before giving your answers.
Object Virtual function tables
pc (A)


(b)(5 points)Consider the following C++ code:

class A1 { virtual void a1(); }
class B1 : A1 { virtual void b1(); }
class C1 : A1 { virtual void c1(); }
class A2 : B1, C1 { virtual void a2(); }
class B2 : A2 { virtual void b2(); }
class C2 : A2 { virtual void c2(); }
class A3 : B2, C2 { virtual void a3(); }
What is the space complexity of an object of typeAn(in other words, what is the
asymptotic space usage of such an object asngoes to infinity?) Briefly explain why.

3 .(15 points)………………………….. Continuations and concurrency

(a)(5 points)Ben Bitdiddle has written this function in his own variant of JavaScript,
function f() {
return g(h1(), h2());
BackwardsScript is like JavaScript, except that arguments are evaluated from right-
to-left. Ben is sad that he cant run his program on regular  javascript interpreter.
Rewrite this program into continuation passing style, so that it is indifferent to
evaluation order.

(b) Alyssa P. Hacker really likes lazy evaluation, so she decides to port lazy evaluation to Java, adding asynchronizedstatement to ensure its thread safe:

class Thunk {
Object object_;
Callable<Object> fn_;
public Thunk(Callable<Object> fn) {
fn_ = fn;
synchronized public Object force() {
if (fn_ != null) {
object_ = fn_();
fn_ = null;
return object_;
i. (5 points)This code can deadlock with itself. Briefly describe a situation in
which deadlock could occur.
ii. (5 points)Bob Bitdiddle is trying to optimize the code, and proposes the follow-
ing new code:
public Object force() {
if (fn_ != null) {
Object obj = fn_();
synchronized (this) {
object_ = obj;
fn_ = null;
return object_;
What could go wrong with Bobs new implementation?

4 .(20 points)……………………… Declaration-site variance inference

Josh Bloch famously stated that We simply cannot afford another wildcards, referring
to the complexity of use-site variance stemming from wildcards. Accordingly, youve
been assigned to a  java task force to get rid of use-site variance and add declaration-site
variance to Java. However, in a shocking break from historical precedent, the Java com-
mittee believes that Java programmers should not have to explicitly write the variance
of their definitions. Instead, they want toinfervariance automatically!
(a)(1 point)What should the inferred variance ofWriterbe?
interface Writer<A> {
void put(A x);
(b)(3 points)What should the inferred variance ofWritablebe? Briefly explain why.
interface Writable<A> {
void write(A x);
Writer<A> getWriter();
(c)(3 points)What should the inferred variance ofQueuebe? Briefly explain why.
interface Queue<A> {
A dequeue();
void enqueue(A x);
(d)(5 points)The committee asks you to implement a prototype declaration-site vari-
ance for Java. However, youre too lazy to actually modify a Java compiler, and
decide to write a source-to-source translator, to translate declaration-site variance
to use-site variance. If you infer that an interfaceM<T>is covariant, how should
you rewrite the type of the functionvoid f(M<T> x)so that it would accept all
valid subtypes ofM<T>?
(e)(8 points)The developers of the JScience library are a bit worried about your vari-
ance inference proposal. In their library, they have implemented a number of
classes which are parametrized by a unit of measure; for example,Unit<Mass>
indicates a scalar quantity which measures mass. In the definition ofUnit<Q>,Q
is never actually used in the body of the class; the type parameter is used purely to
help clients avoid mixing up their units. What is the inferred variance(s) ofUnit,
and why is that a bad thing?