# CSC 123/252 Scary Test # A scary test will make you study really hard for the real test. # Answer each question carefully. The test will be "graded". #1. Explain the difference between the perl functions f and g below: sub f { sub { my $y=0; $y+=$_[0]; $y} } # AND sub g { my $y=0; sub { $y+=$_[0]; $y } } #Determine the output of $f1 = f(); $g1 = g(); print $f1->(1) + $f1->(1), "\n"; print $g1->(1) + $g1->(1), "\n"; 1b. which of the above functions can be written in C? Explain. 2. Someone has invented a new language called C** that's claimed to be the best ever. The syntax certainly looks familiar: int x = 1; // declares and initializes global variable int f(int y) { return y+1; } // define function with argument y void MAIN() // MAIN must be in all capitals (ingenious!) { int x = 2; // declares "local" variable inside main stdout << f(x) << endl; // fantastic new way to print! // this will print 3 in C** } But you didn't find any documentation on whether this language uses static or dynamic {\bf scoping.} Devise an experiment (write a program in C**) to find out. That is, write one program that will behave differently depending on the scoping rule used. Be clear as to how to interpret the result of your experiment. 3. Assume that A and B are classes in C++ and that variable a is of type A (A a). Explain the difference between (B*)&a and dynamic_cast(&a). 4. Consider the following C# program for expression trees including code in the public class that tries to evaluate a tree. Explain the result of compiling or running the program. If it doesn't compile or run, explain why. (I'm not asking you what it should look like, but to explain why it's correct/incorrect as written). interface expr {} // marker interface class intexp : expr // leaf nodes { public int val; public intexp(int v) {val=v;} } class plusexp : expr // left+right { public expr left, right; public plusexp(expr l, expr r) {left=l; right=r;} } class timesexp : expr // left*right { public expr left, right; public timesexp(expr l, expr r) {left=l; right=r;} } public class goodquestion { public int eval(intexp e) { return e.val; } public int eval(plusexp e) { return eval(e.left) + eval(e.right); } public int eval(timesexp e) { return eval(e.left) * eval(e.right); } public int eval(expr e) { return e.eval(); } public static void Main() { expr E = new plusexp(new intexp(2), new timesexp(new intexp(4),new intexp(3))); goodquestion g = new goodquestion(); //needed to call non-static functions System.Console.WriteLine(g.eval(E)); }//main }