c++代做 | javascript | 代写java – CSCI-UA.490 Final

CSCI-UA.490 Final

c++代做 | javascript | 代写java – 这是一个关于c++的题目, 主要考察了关于c++/javascript/java的内容,是一个比较经典的题目, 是有一定代表意义的c++/javascript/java等方向综合代写任务

C++语言代写 C++代写 代写C++ 代做C

CSCI-UA.490, Autumn 2018
December 17

CSCI-UA.490 Final

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
Score
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 x = 0;
var f = function A() {
var x = 1;
var g = function B() {
return x; // ***
}
return g;
}
var h = f();
console.log(h());
(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 inner
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 functions f and g.
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
(3) B() access link Z:_____,code for____

(c)(5 points)Ben Bitdiddle has an idea for an awesome javascript optimization. Ob- serving that functionAdoes not usexuntilBis called, he suggests that it is wasteful to allocate variables too early and that we shouldmove the variable declaration to the inside of the function that actually uses it. var x = 0;

var f = function A() {
// Originally: var x = 1
var g = function B() {
var x = 1; // MOVED HERE
return x;
}
return g;
}
var h = f();
console.log(h());
Ben justifies his optimization by saying that he ran the new program, and it gave
him the same result as the old one. Unfortunately, this optimization is not sound.
Edit the program above (e.g., by crossing out lines and writing in new lines) so that
Bens optimization would change its behavior, and state below what the unopti-
mized and optimized outputs of your program would be.

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

(a)(5 points)Consider the following  c++ code:
class A {
int a;
virtual void f();
virtual void g();
}
class B : public A {
int b;
virtual void h();
virtual void f() override;
}
Fill in the object layout and vtables for aBobject. For each entry in the vtable,
indicate the function to which the slot points (e.g.,B::f).
Object Virtual function tables
pb, pa vptr 

(b)(10 points)You have been told that the vtable layout of another classZis: Object Virtual function tables pz, px vptr Z::f1 1 x1 X::f2 2 x py vptr Z::f3 3 y1 Z::f4 4 y Unfortunately, youve lost the source code forX,YandZ. However, you remember some facts about the classes:

  • Zinherits fromXandY.
  • Xhas two data membersx1andx2.
  • Yhas two data membersy1andy2.
  • Zdoes not define any new, non-overriding virtual methods.
  • All methods take no arguments and returnvoid Write implementations ofX,YandZwhich produce this data and vtable layout.

(c)(5 points)Answer true or false to the following questions about the vtable layout from the previous part(b): i. 1 is zero. True or False ii. 2 is zero. True or False iii. 3 is zero. True or False iv. 4 is zero. True or False

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

(a)(5 points)Here is a program which counts down from two to zero.
function countdown2() {
say(2);
say(1);
say(0);
}
Rewrite this program into its continuation passing versioncountdown2(k), given
that the functionsay(n, k)now takes a continuation argument, specifying what
should be executed after it says the number.
(b)(10 points)Wed like to generalize this program so it can count down from an arbi-
trary number, as in the following program:
function countdown(n) {
for (var i = n; i >= 0; i--) {
action(n);
}
}
Rewrite this function into its continuation passing version, countdown(n, k).
(Hint: if you dont know how to CPS a loop, first rewrite this function intorecursive
formwithout a loopbefore CPSing it.)

4 .(20 points)…………………………………………………. .MVars in Java

A student liked MVars so much that he wanted to implement them in Java:
class MVar<T> {
private T val;
private boolean is_full = false;
public synchronized void put(T x) {
while (is_full) wait();
val = x;
is_full = true;
notifyAll();
}
public synchronized T take() {

}

}

(a)(5 points)Using wait, notify and/or notifyAll, fill in the implementation oftake.
(b)(5 points)Using wildcards, what type in  java represents a write-only view of an
MVar of typeT?
(c)(5 points)LetWriteOnlyMVar<T>represent the interface of write-only MVar. Let
F<T> = WriteOnlyMVar<WriteOnlyMVar<T>>. What should the inferred vari-
ance ofFbe?
(d)(5 points)A colleague suggests that if we garbage collect all readers to an MVar, the
MVar itself should be garbage collected, even if there are still writers with access
to MVar. His reasoning: there is no way any further writes to the MVar can have
an observable effect on the program, since there is no one to read what they write
in! In a standard model of garbage collection based onreachability, will an MVar
be garbage collected in this situation? Why or why not?