Coffeehouse Thread

36 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

C# interview question

Back to Forum: Coffeehouse
  • User profile image
    TheSchwartz

    Hopefully I'm not the only one who likes code puzzles?  What happens with this block:

    -------------------------

    int i;

    for (i=0; ;i++)
    {
       Console.Writeln(i);
    }

    -------------------------

    Jon Schwartz
    35 freeware .NET/SQL tools
    www.ms-inc.net

  • User profile image
    Stebet

    I guess it loops endlessly since there's no conditional check to terminate the for loop.

  • User profile image
    bonk

    yes, its and endless loop, i is iterating endlessly and its value is printed to console forever. The loop has no exit condition and therefore will run forever. Forver? Not really - once it  reaches Int32.MaxValue, an OverflowException will be thrown.

    That code will not compile ...  Wink



  • User profile image
    Stebet

    bonk wrote:
    yes, its and endless loop, i is iterating endlessly and its value is printed to console forever. The loop has no exit condition and therefore will run forever. Forver? Not really - once it  reaches Int32.MaxValue, an OverflowException will be thrown.

    That code will not compile ... 





    Heheh.. very true.. didn't catch that one immediately Wink

  • User profile image
    amotif

    TheSchwartz wrote:
    Hopefully I'm not the only one who likes code puzzles?


    The thread subject says "C# interview question." Do you use trick questions in interviews? Just curious.

  • User profile image
    littleguru

    You get an exception, once you reach the Integer.MaxValue Wink

  • User profile image
    Maurits

    littleguru wrote:
    You get an exception, once you reach the Integer.MaxValue Wink


    Do you?  Or does it wrap to a negative number?

  • User profile image
    bitmask

    Maurits wrote:
    littleguru wrote: You get an exception, once you reach the Integer.MaxValue Wink


    Do you?  Or does it wrap to a negative number?


    In C# you'll wrap around. "Check for arithmetic overflow / underflow" is false by default.

    In VB.NET overflow / underflow checking is on by default, so you would generate an exception.

  • User profile image
    nightski

    Actually, this code does compile and it loops forever.

  • User profile image
    Stitch 2.0

    I'd say it won't compile since Console only has the WriteLine Method and no Writeln.... at least in .Net 2.0

  • User profile image
    nightski

    Meh,
    You are right as far as Writeln goes Smiley  I was referring to the loop.

  • User profile image
    Steve411

    TheSchwartz wrote:
    Hopefully I'm not the only one who likes code puzzles?  What happens with this block:

    -------------------------

    int i;

    for (i=0; ;i++)
    {
       Console.Writeln(i);
    }

    -------------------------

    Jon Schwartz
    35 freeware .NET/SQL tools
    www.ms-inc.net


    See I figured it'd only loop once because of the 0 integer. But now that I look at it, it will loop endlesly due to the ++ assignment.
    Console.WriteIn(i) is invalid. Try Console.WriteLine(i.ToString()), you can't write out integer values unless first converting them to a string or accessing the ToString() property. In 1.1 at least.

    Steve.

  • User profile image
    Stitch 2.0

    Steve411 wrote:
    [...]you can't write out integer values unless first converting them to a string or accessing the ToString() property. In 1.1 at least.

    Steve.

    Sorry to disappoint you Wink
    There is an overload for the WriteLine-Methid which is sepcified like this:
    [C#] public static void WriteLine(int);
    You can actually write out integers....

  • User profile image
    Steve411

    Stitch 2.0 wrote:
    Steve411 wrote:[...]you can't write out integer values unless first converting them to a string or accessing the ToString() property. In 1.1 at least.

    Steve.

    Sorry to disappoint you
    There is an overload for the WriteLine-Methid which is sepcified like this:
    [C#] public static void WriteLine(int);
    You can actually write out integers....


    Sorry. Smiley

    Steve.

  • User profile image
    ScanIAm

    Stitch 2.0 wrote:
    Steve411 wrote: [...]you can't write out integer values unless first converting them to a string or accessing the ToString() property. In 1.1 at least.

    Steve.

    Sorry to disappoint you
    There is an overload for the WriteLine-Methid which is sepcified like this:
    [C#] public static void WriteLine(int);
    You can actually write out integers....


    Sorry to disappoint you, too:

    C:\Documents and Settings\xxxx xxxxxxxx\My Documents\Visual Studio Projects\ConsoleApplication7\Class1.cs(24): 'System.Console' does not contain a definition for 'Writeln'

  • User profile image
    bonk

    ScanIAm wrote:
    Stitch 2.0 wrote:
    Steve411 wrote: [...]you can't write out integer values unless first converting them to a string or accessing the ToString() property. In 1.1 at least.

    Steve.

    Sorry to disappoint you
    There is an overload for the WriteLine-Methid which is sepcified like this:
    [C#] public static void WriteLine(int);
    You can actually write out integers....


    Sorry to disappoint you, too:

    C:\Documents and Settings\xxxx xxxxxxxx\My Documents\Visual Studio Projects\ConsoleApplication7\Class1.cs(24): 'System.Console' does not contain a definition for 'Writeln'

    Amazing how much hussle this trival peace of code caused Wink
    ScanIAm I though we made already clear that it is WriteLine not Writeln (Writeln is java).

  • User profile image
    Maurits

    To give a formal answer:

    It prints this:

    0
    1
    2
    ...
    (INT_MAX)
    (INT_MIN)
    ...
    -2
    -1

    and then it starts over, repeating forever until something outside the program stops it.

  • User profile image
    ScanIAm

    bonk wrote:
    ScanIAm wrote:
    Stitch 2.0 wrote:
    Steve411 wrote: [...]you can't write out integer values unless first converting them to a string or accessing the ToString() property. In 1.1 at least.

    Steve.

    Sorry to disappoint you
    There is an overload for the WriteLine-Methid which is sepcified like this:
    [C#] public static void WriteLine(int);
    You can actually write out integers....


    Sorry to disappoint you, too:

    C:\Documents and Settings\xxxx xxxxxxxx\My Documents\Visual Studio Projects\ConsoleApplication7\Class1.cs(24): 'System.Console' does not contain a definition for 'Writeln'

    Amazing how much hussle this trival peace of code caused
    ScanIAm I though we made already clear that it is WriteLine not Writeln (Writeln is java).



    I guess the parts in pink confused me...

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.