C# Quiz

Published February 20, 2010
Advertisement
In the spirit of Washu's C++ quizzes, I've decided to try a few based on C#. While not as heinous as C++, C# still has plenty of little quirks to make things interesting. Resist the temptation to break out your compiler to cheat, and don't rely on the answer of someone else as there is no guarantee that they're correct. For reference, I'm working off the C# 4.0 Specification.

Question 1: Does the following program compile? If not, why? If so, or if compilation errors are corrected, what is its output?
#define classnamespace Test{#if class    /* should compile? */    class @class    {        public int @int(short @int)        {            return (int)@int + 2;        }        public int \u0069nt(byte o)        {            return 4;        }    }#endif    class Program    {        static void Main()        {            @class @int = new @class();            System.Console.WriteLine(@int.@int(5));        }    }}


Question 2: What is the output of the following program?
class A{    int i;    public A(int i)    {        this.i = i;    }    public static explicit operator A(int i)    {        return new A(i);    }    public override string ToString()    {        return i.ToString();    }}class Program{    static void Main()    {        int A = 4;        System.Console.WriteLine((A)-5);        System.Console.WriteLine((A)(-5));    }}


Question 3: Does this program compile? If so, what is its output?
struct Foo{    static readonly Foo F = new Foo(5);    public int I;    public Foo(int i)    {        I = i;        this = F;    }}class Program{    static void Main()    {        Foo f = new Foo(2);        System.Console.WriteLine(f.I);    }}


Question 4: What is the output of the following program?
class Program{    class A { }    class B { }    static void Main()    {        int A = 10;        int B = 20;        int G = 15;        F(G < A, B > (-7));        F(G < A, B > -7);    }    static void F(int i)    {        System.Console.WriteLine(i);    }    static void F(bool b1, bool b2)    {        System.Console.WriteLine("{0} {1}", b1, b2);    }    static int G(int v)    {        return v;    }}


Question 5: Is the following defined behavior? If so, what is its output?
class Test{    static int a = b + 1;    static int b = a + 1;    static void Main()     {         System.Console.WriteLine("a = {0}, b = {1}", a, b);     }}


Have at it!
Previous Entry Matrix Projection Poll
Next Entry Quiz Answers
0 likes 4 comments

Comments

MaulingMonkey
Here's my results as checked against the VS2k8 C# compiler after the fact (each comma separated entry corresponding to a question-mark in the "Question"):

A1: Correct, incorrect, correct
A2: Incorrect
A3: Correct, incorrect (Correct reasoning, tripped up on the details)
A4: Correct
A5: 99% certain I'm correct, Correct

If we're only counting entirely correct answers, that's only a 40% grade. FFFFFFFF...
February 20, 2010 02:29 AM
Zao
1. It doesn't build, because the spelling of the two members are the same. Depending on how you correct it, it outputs on of the two possible answers.
2. All possible combinations of choose 2 of {-5, -1}.
3. Probably 5, because that would be neat.
4. As generics are inferior to templates, -7; false true.
5. As C# tends to have pansy rules about expression evaluation order in general, and most certainly do default-initialize statics like C++ at the least, a = 1, b = 2.

Edit: #1 was apparently a multi-part question...
February 20, 2010 10:51 AM
Telastyn
I will probably do poorly here, since I tend to ignore/forget the dusty corners of languages and just avoid anything vaguely undefined. Everything is a guess without a compiler in front of me and ignoring previous answers.

(oh, and this is a bit off from Washu quiz, since at least a few of those were not terribly un-standard C++. This is a bit esoteric for C#)

1. I suspect that it does compile, though my gut instinct is that it does not. I do not remember unicode escapes being acceptable identifier parts. Assuming it does compile, I suspect that it prints 7, but that's a guess of least surprise since I have no idea what the conversion priority rules are.

2. Again, because of the question format, I'd guess -1, -5. Though asked what either of the statements produces I'd probably say -5 for each. Frankly, I could care less of operator precedence rules.

3. I don't immediately see any reason why not. 5. If it did not compile, it's because I forgot a rule regarding structs, or this assignment (which I'm assuming C++ semantics since I forget the C# ones).

4. Akin to #2, the question makes me change my inclinations. -7 then 'false true'. And again, I could give half a rat's ass about operator precedence for code I can easily refactor to make 100% clear.

5. My gut instinct says it's not, but if I remember the standard correctly, the only sticking point is the order of accessing the parameters. I don't remember if C# specifies that parameters are accessed in order of use.

If it does (and I remember the initialization rules correctly) a begins initialization first which triggers b's initialization based off of a's current state, returning 1. Thus the program would print 2 1.

If it doesn't I believe it should still print that, but it's behavior dependent on the parameter access order of the implementation you have (and dependent on optimization at the call site and a few other codegen details)
February 20, 2010 03:07 PM
superpig
I'm going to do so badly on these...

1) Let's see. I think the preprocessor is fine; you can use C# keywords as preprocessor tokens, you just can't use preprocessor keywords as preprocessor tokens, i.e. you can't use #define or #if as tokens. @-signs are taken care of by the lexer, as are escaped Unicode sequences. The parser, however, will complain that the class 'class' has two members named 'int'.

2) I think it's -1 in the first case, and -5 in the second.

3) It doesn't compile; assignments to 'this' are not allowed.

4) -7, false true

5) It's not defined.
February 21, 2010 11:05 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement

Latest Entries

New Blog

2022 views

Progress Update

1550 views

Start of Project

1513 views

New Job

2220 views

The Downward Spiral

2879 views

Job Interviews

1480 views
Advertisement