Quiz #4 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

In order, what are the COOL types for each of the three expressions in the following block?
Assume a class hierarchy Foo ≤ Bar ≤ Bazz ≤ Object along with the usual primitive COOL types.
{
    case 3 of y: Int => y; z: Bool => z; esac;
    if 0 = 1 then new Bar else new Foo fi;
    let x: Bazz ← new Bar in x;
}

Question 2

If we have the following program:
Class Main inherits IO {
    x : Int ← 5;
    foo(z:Int) : Int {
       x+z
    };
    bar(y:Int) : Int {{
       let x : Int ← 1 in
       let z : Int ← 2 in
       foo(y);
    }};
    main(): Object {{
       let x : Int ← 7 in
       out_int(foo(bar(3)));
    }};
}
What would the the program print assuming it is statically scoped?

Question 3

For the program in above question (Question 2), what would the the program print assuming it is dynamically scoped?

Question 4

Consider the following COOL program, with the type of method bar in class Bar omitted:
class Main {
   main() : Object {
      (new Bar).bar()
   };
};
class Foo inherits IO {
   foo() : SELF_TYPE {{
      out_string("Foo.foo()\n");
      foo();
      self;
   }};
};
class Bar inherits Foo {
   foo() : SELF_TYPE {{
      out_string("Bar.foo()\n");
      new SELF_TYPE;
   }};
   bar() : (*MISSING*) {
      case foo() of
         f : Foo => f@Foo.foo();
         b : Bar => (new Bazz).foo();
         o : Object => foo();
      esac
      };
};
class Bazz inherits Bar {
   foo() : SELF_TYPE {{
      out_string("Bazz.foo()\n");
      (new Bar)@Foo.foo();
      self;
   }};
};
Which of the following would be a valid return type for method bar in class Bar according to the static type checking rules for COOL?
[Check all that apply]

Question 5

For the program in above question (Question 4), which is the output of the program?

Question 6

Consider the following COOL program:
class Main inherits IO {
    sing() : Int { 1 };
    valcomp(x: Int, y:Int) : Bool { x ≤ y };
    fib(x:Int) : Int {
        if valcomp(x,2) then sing() else fib(x - 1) + fib(x - 2) fi
 };
    main() : Int {{
        x:Int ← let y : Int ← 4 in fib(y);
        out_int(x);
     }};
};
In the activation tree, how many direct children does the activation of the first call to fib in main have, and what is the maximum depth of the activation tree? Assume a single root node as a tree has a a depth 1.

Question 7

For the program in above question (Question 6), how many activation records are there in total, assuming out_int has no child calls?

Question 8

For the program in above question (Question 6), which are possible activation stack states during the execution of main?
The answer from left to right represents from the top to the bottom of the stack.
[Check all that apply.]

Question 9

Consider the expression (7 + 5) * (3 + 2).
Which of the following are possible stack machine states during evaluation?
[Check all that apply]

Question 10

Consider computing expressions using a 1-register stack machine.
[Check all that apply.]
    
You cannot submit your work until you agree to the Honor Code. Thanks!