WinDev Coding Questions


I'm up at WinDev in Boston this week (I know, I know, it's really Quincy), and last night MSDN had an Ask The Experts reception where, in addition to the great food and drink, we also gave away some cool prizes for correct answers to .NET trivia and coding questions.  I've included the code questions below.  Note that the session attendees did a terrific job with these, especially given how intense some of the questions are and that we asked the participants not to use their computers (the ones in their heads were fine).

Question 1 (thanks to Mike for supplying the question):
Replace the TBD comment with a single line of code that causes this program to display "Win-Dev!"  You cannot modify any of the existing code,  nor add code above or below the existing code.  You can only write one line of code in place of the TBD comment (by single line, we mean one semicolon).  You cannot use the Console class in your one line.

        class App {
            static void Main() {
                // TBD
            }
        }

        sealed class Internal {
            sealed class Private {
                static void PrintMessage( string s1, string s2 ) {
                    System.Console.WriteLine(s1 + "-" + s2);
                }
            }
        }

Question 2 (thanks to Adi for supplying the question):
What does the following code print?  There's a long answer, and there's a short answer, both of which were accepted.
        class C
        { 
        const string s = @" 
        class C 
        {{
        const string s = @{1}{0}{1};
        static void Main()
        {{
        System.Console.WriteLine(s,s,'\u0022');
        }}
        }}";
        static void Main()
        {
        System.Console.WriteLine(s,s,'\u0022');
        }
        }

Question 3 (care of Steve):
What does the following code display when run on Everett?  What about when run on Whidbey?
        using System;
        using System.Runtime.InteropServices;
        public class Program
        {
            static void Main()
            {
                SomeType s;
                unsafe
                {
                    Console.WriteLine("{0} {1} {2} {3} {4}",
                        (byte*)&s.Field1 - (byte*)&s,
                        (byte*)&s.Field2 - (byte*)&s,
                        (byte*)&s.Field3 - (byte*)&s,
                        sizeof(SomeType),
                        Marshal.SizeOf(typeof(SomeType)));
                }
            }
        }

        struct SomeType
        {
            public char Field1;
            public int Field2;
            public char Field3;
        }

Question 4 (care of Steve):
What does the following code display?  Multiple answers were accepted.
        using System;
        using System.Threading;
        class Class1
        {
            static void Main()
            {
                Console.WriteLine("A");
                new Class2();
                GC.Collect();
                new Class1();
                GC.Collect();
                Console.WriteLine("B");
            }

            ~Class1()
            {
                Console.WriteLine("C");
                Class2.Are.Set();
                Console.WriteLine("D");
            }
        }

        class Class2
        {
            public static ManualResetEvent Are = new ManualResetEvent(false);
            ~Class2()
            {
                Console.WriteLine("E");
                Class2.Are.WaitOne();
                Console.WriteLine("F");
            }
        }

Congratulations to all of the winners.  And thanks to everyone who played!

For those of you watching at home, you can try out questions 2,3,4 just by compiling and running them (but try to reason them out in your head first).  The answer to number 1 we were looking for was the following (though we gave partial credit for code that was close):
        typeof(Internal).GetNestedType("Private", System.Reflection.BindingFlags.NonPublic).GetMethod("PrintMessage",
                System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).Invoke(null, new object[]{"Win","Dev!"});
Never said it was a short line 🙂

Also, as a result of these questions (namely #4), a few people were asking about finalization and how it works.  The following are some great resources:
Chris Brumme's blog discourse on finalization
Jeffrey Richter's book Applied Microsoft .NET Framework Programming

Oh, and being in Boston for the week, all I can say is... GO SOX!!!

Comments (7)

  1. Quick question on Question 1. Are the sealed modifiers there for a reason? When I looked at it my first thought was ‘reflection’ since the method you want to call is a private member – and you couldnt inherit and use the methody anyway since its private. Yea?

  2. Stephen Toub says:

    Reflection is the way to go for this. The sealed modifiers are just there to spice things up and to add an extra bit of confusion, but the same answer works without them.

  3. Mike Goleman says:

    My surgeon identified me with a procedure as a modified maze procedure.  It was as endoscopice approach, how would I code thst as a CPT code

  4. I’m up at WinDev in Boston this week (I know, I know, it’s really Quincy), and last night MSDN had an Ask The Experts reception where, in addition to the great food and drink, we also gave away some cool prizes for correct answers to .NET trivia and coding questions.  I’ve included the code questions below.  Note that the session attendees did a terrific job with these, especially given how intense some of the questions are and that we asked the participants not to use their computers (the ones in their heads were fine).

    I do not agree. Go to http://apartments.waw.pl/

Skip to main content