Final Help Center

Learn more

Warning: The hard deadline has passed. You can attempt it, but you will not get credit for it. You are welcome to try it as a learning exercise.

Question 1

Consider the following Cool class and method definition:
class C {
    opVal(x: Int, y: Int, flag: Bool): Int {
        if flag then
            if x + y = 0 then 0 else x + y fi
        else {
            x <- x + y;
            x + y;
        }
        fi
    };
};

What is the size (in bytes) of the activation record for the above function, assuming that the minimum space needed for temporaries is allocated in the activation record?

Question 2

For the class and method definition in the last question, which of the following statements are true?
[Choose all that apply]

Question 3

Consider the following Cool program:
class Main inherits IO {
    s(): Int { { out_string("s"); 1; } };
    cmp(x:Int, y:Int): Bool { not(x <= y) };
    r(x:Int): Int { {
        out_string("r");
        if cmp(x,2) then r(r(x - 1)) + r(x - 1) else s() fi;
    } };
    main(): Object { r(3) };
};
In the activation tree, what is the maximum number of children for any activation record? What is the maximum depth?
Assume a single root as a tree has a depth of 1.

Question 4

Assume the current runtime stack contains activation records main, r, r, cmp (reading from the bottom of the stack to the top). What are the possible outputs of the program up to this point?
[Choose all that apply]

Question 5

Consider the following program.
 1. k := i + j
 2. x := k * 10
 3. if x > z goto line 7
 4. x := z - y
 5. f := x * j
 6. goto line 10
 7. x := y + 1
 8. i := j
 9. j := j + 2
10. y := f * 2
11. a := x + y
12. if x = 0 goto line 7
Assuming only variable "a" is live on exit, and that no register spilling is allowed, which of the following pairs of variables could potentially be assigned to the same register?
[Choose all that apply]

Question 6

In Cool, if class C already has method foo defined, it is not permitted to define a new method foo with a different number of arguments or with arguments of different types. However, it is possible to do so in languages like Java and C++, which support method overloading. You have decided to implement method overloading in Cool by using the same algorithm as in Java and C++.

Consider a dispatch expression e.foo(e1,...,en) in which e0:T0 and for each i, ei:Ti.
Class T0 may have a set of one or more definitions of foo with different numbers or types of arguments (but no two definitions may have the same number and same types of arguments.) There are no constraints on the return types of the definitions - return types are not considered in the selection algorithm. From this set Sfoo we choose a definition such that:
(1) the definition foo(x1:T′1,...,xn:T′n):T′r has n arguments,
(2) for all i∈[1,n], Ti≤T′i,
(3) for any other definition foo(y1:T′′1,...,yn:T′′n):T′′r that satisfies (1) and (2) it is the case that for all i∈[1,n], T′i≤T′′i.
If there is not a unique method that satisfies all three rules, then type checking fails.

The type rules in Cool have the form:
O,M,C ⊢ e: T
What parts of the type environment must be changed to support method overloading?
[Choose all that apply]

Question 7

Using the definitions in Problem 6, consider a program with an overloaded method f. Assuming there are at least two definitions of f that have different signatures, what is the minimum number of definitions of f and number of arguments to f for the overloading resolution algorithm to possibly report that there is not a unique best choice of f?

Question 8

Consider the following Cool program:
class C {
    next : C;
    set(n : C): C { { next <- n; self; } };
};
class Main{
    main(): Int {
        let x : C in {
            let o1 : C <- new C, o2 : C <- new C, o3 : C <- new C,
                o4 : C <- new C, o5 : C <- new C, o6 : C <- new C in
                { o1.set(o2); o2.set(o3); o3.set(o1); o5.set(o6); x <- o5; };
            (*GC*)
            0;
        }
    };
};
Assume that a garbage collection occurs at the line marked "GC" and that no garbage is collected before that point. How many objects
of type C are visited by the garbage collection if a mark and sweep algorithm is used? If an object is examined more than once count it
just once (i.e., we want the count of objects, not the count of object visits).
[Your answer should be in Arabic numerals with no other punctuation (e.g. 0 or 1000, not zero or 1,000 or 1.000).]

Question 9

For the Cool program in the previous question, how many objects of type C are visited if the garbage collection performed at line
"GC" uses the stop and copy algorithm instead? (Again, only count each object once, regardless of how many times it is visited
or copied.)
[Your answer should be in Arabic numerals with no other punctuation (e.g. 0 or 1000, not zero or 1,000 or 1.000).]

Question 10

Again using the same Cool program, if reference counting is used instead, there is no explicit garbage collection
pass performed at line "GC", but some objects may have already been collected. How many objects of type C are
still in the heap when the execution is at line "GC"?
[Your answer should be in Arabic numerals with no other punctuation (e.g. 0 or 1000, not zero or 1,000 or 1.000).]
    
You cannot submit your work until you agree to the Honor Code. Thanks!