Week 6: Exercises

1. Exceptions

What will this program print?

class MyException : Exception { }

static class Test {
  static int foo(int i) {
    try {
      WriteLine("a " + i);
      if (i == 0)
        throw new MyException();
      WriteLine("b " + i);
      return foo(i - 1);
    } catch (MyException e) {
      WriteLine("c " + i);
      if (i < 2)
        throw e;
      return i;
    } finally {
      WriteLine("d " + i);
    }
  }  
  
  static void Main(string[] args) {
    WriteLine("e " + foo(3));
  }
}

2. Polynomials

Design and implement a C# class Polynomial representing a polynomial of a single variable. Your class should include the following: