Sunday, February 14, 2010

Inheritance Quiz

Now pay attention, class! Try to guess what does this fine C# program do?


using System;

public class A {
public static void hello()
{
Console.WriteLine("Hello World!");
}
public class B : A {
}
}

class Test {
public static void Main(string[] args)
{
A.hello();
A.B.hello();
A.B.B.hello();
A.B.B.B.hello();
A.B.B.B.B.hello();
}
}



I'll give you a few options. This C# program, when compiled with GMCS will:

(a) Compile without error and print "Hello World!" five times.
(b) Compile without error, but abort with a runtime error.
(c) Abort compilation because of the cyclic inheritance.
(d) Abort compilation because A.B is not defined.
(e) Abort compilation because A.B.B is not defined.
(f) Crash the compiler.


No cheating, please!

No comments:

Post a Comment