C#の問題

C#の問題 2題

問題 その1 : 下記の実行結果は?

string s1 = null;
string s2 = null;
string s = s1 + s2;
Console.WriteLine(s.Length);

選択肢

  1. 0と出力
  2. 3行目でエラー
  3. 4行目でエラー
  4. その他


問題 その2 : 下記の実行結果は?

 class Program
{
  static object o = new object();
  static int Fac(int n)
  {
    lock (o)
    {
      Thread.Sleep(1000);
      if (n <= 1) return 1;
      return n * Fac(n - 1);
    }
  }
  static void Main(string[] args)
  {
    Thread th1 = new Thread(new ThreadStart(delegate
    {
      Console.WriteLine("th1 : Fac(2) = {0}", Fac(2));
    }));
    Thread th2 = new Thread(new ThreadStart(delegate
    {
      Console.WriteLine("th2 : Fac(2) = {0}", Fac(2));
    }));
    th1.Start();
    th2.Start();
  }
}

選択肢

  1. 2秒後と4秒後にそれぞれ出力
  2. 2秒後に2行とも出力
  3. 何も出力されずに無限ループ
  4. その他


余談
あるときから、VisualStudioでFormのデザインを開くと落ちるようになった。原因は、タイマのハンドラでNullReferenceExceptionを起こしていたため。