CSC 123/252 Scary Test A scary test will make you study really hard for the real test. Some of the questions are a bit hard in order to give you opportunity to practice. Give yourself about one hour to complete the test. 1. Explain the difference between the F# functions f and g below: let f n = fun y -> let mutable x = n x <- x+y x let g n = let mutable x = n fun y -> x <- x+y x Determine the output of let f1 = f(2) let g1 = g(2) System.Console.WriteLine(f1(1)+f1(1)); System.Console.WriteLine(g1(1)+g1(1)); 1b. which of the above functions can be written in C? Explain. 1c. Write the function g as a lambda expression in C++. You may use "auto" to infer types. 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! { 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 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. Be clear as to how to interpret the result of your experiment. 3. Explain why or why won't the following JAVA generic class compile class A { static T x; } // will this compile? 4. Explain why object[] A = new string[4]; should not compile (regardless of whether it does) 5. Determine the output of the following C# program class A {} class B : A {} public class goodquestion { public int f(A x) { return 1; } public int f(B x) { return 2; } public static void Main() { A n = new B(); goodquestion gq = new goodquestion(); System.Console.WriteLine( gq.f(n) ); }//main } 6. In F#, int(s) will convert string s into an integer and throw an exception if s can't be converted. 6a. Write a function safeint that returns a value of type `int option`: That is, Some(n) where n is an int, or None. The exception should be caught internally and not allowed to escape the function. Hint the syntax of a "try-with block" in F# is: try stuff with | e -> expression to return, must match type of "stuff" 6b. write a function that takes a string, calls safeint on the string, and either print the integer that was parsed or print "invalid input" if it can't be parsed. You're not allowed to call Option.get.