Quiz Answers

 

Assume a class A that has a static integer field Ten.

Assume a class B that has a static integer field Ten.

 

 

What must be true for the following lines to output 32

 

A.Ten = 10;
B.Ten = 10;
Console.WriteLine(A.Ten + ++B.Ten + A.Ten);

 

 

 

Many got this wrong not because they misunderstood the problem (or even had the wrong answer) … but because they did not read the question properly.

 

What must be true for the following lines to output 32

 

As an example many answered that A must inherit from B. This is a correct possibility but it is not the only one … it does not need to be true. There are 4 possibilities for how this could happen

 

A : B (static in B)

B : A (static in A)

A : X B : X (static in X), there are infinite permutations of this but they can be represented in a single pattern

A : X B : X X : Y (static in Y) again infinite permutations

 

We could say that A and B share an ancestry where the static variable is defined at or above the point of divergence.

 

Glad to see everyone had on their thinking caps!

This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

39 Responses to Quiz Answers

  1. G Stafford says:

    Yo Greg….

    Gimme a shout biatch. 598.4959…you know the area code.

  2. Greg says:

    @Brian thanks for your input.

    Let’s look at a situation where this kind of problem happens all the time. Debugging code that is hard to debug (let’s say its multi-process or multi-threaded code). The ability to look at situations and determine all the possible outcomes in your head is a crucial skill when dealing with those kind of problems. Do most people have to worry about it? probably not but then again this was a stupid little quiz that you didn’t even have to waste your valuable time reading…

    The fact that you took the time to write this comment  sounds to me like you need to unplug for a few … maybe you can curl up next to the fire with a copy of the Hardy Boys in “The Mystery of Who Pissed on Brian’s Cheerios”.

    Cheers,

    Greg

  3. Greg says:

    Kamran did you read the answer?

    All of the answers can be summarized in a single answer (as explained)

  4. Brian says:

    Instead of answering the question as written, the question should be changed to ‘who writes code like this?’

    The answer is:
    Interviewers and people who post obscure questions on the tubes.

  5. IT could be done in number of ways.Like Uwe ,Jermiah, Sean,Soren e.t.c
    But question is not clear

  6. B must be derived from A.

  7. Dan Goldstein says:

    A and B are of the same class and the second line is redundant. The third line is equal to
    (A.Ten++) + B.Ten + A.Ten.
    The postfix operator returns the original value, so B.Ten and A.Ten must both be 11 when they are evaluated for the sum to be 32.

  8. Michael Ibarra says:

    The backing value for the accessor methods for B.Ten must be A.Ten.

    public class A
    {
    private static int ten = 10;

    public static int Ten
    {
    get { return ten; }
    set { ten = value; }
    }
    }

    public class B
    {
    private static int ten = 10;

    public static int Ten
    {
    get { return A.Ten; }
    set { A.Ten = value; }
    }
    }

  9. Felipe says:

    B.Ten field:

    set
    {
    ten = value +1
    }

    This?

  10. parent and child says:

    thacca.

  11. WarePhreak says:

    B must be a subclass of A and does not override or hide the implementation of Ten on A.


    class A
    {
    public static int Ten = 0;
    }

    class B : A
    {
    }

  12. Glitch4572 says:

    The static instance must be shared between class A and B.

  13. that A.Ten and B.Ten reference the same memory location

  14. Kyle says:

    class A
    {
    public static int Ten;
    }

    class B : A
    {
    }

  15. Randolpho says:

    I assume you mean that the console output is “32”.

    The solution is that Ten is not really an int as you imply, it’s a custom class or struct with some operator overloading and implicit conversions. Specifically, addition, prefix incrementation (adds 2, not 1), and implicit conversion from integer. Here’s a reference implementation:

    public class A
    {
    public static FakeInt Ten;
    }

    public class B
    {
    public static FakeInt Ten;
    }

    public struct FakeInt
    {
    public FakeInt(int val)
    {
    Val = val;
    }

    private int Val;

    public static FakeInt operator +(FakeInt a, FakeInt b)
    {
    return new FakeInt(a.Val + b.Val);
    }

    public static FakeInt operator ++(FakeInt a)
    {
    return new FakeInt(a.Val + 2);
    }

    public static implicit operator FakeInt(int a)
    {
    return new FakeInt(a);
    }

    public static implicit operator int(FakeInt a)
    {
    return a.Val;
    }

    public override string ToString()
    {
    return Val.ToString();
    }
    }

    public class Starter
    {
    static void Main()
    {
    A.Ten = 10;
    B.Ten = 10;
    Console.WriteLine(A.Ten + ++B.Ten + A.Ten);
    }
    }

    Console output:
    32

  16. Neil Mosafi says:

    It means the same Ten is on both classes, e.g.

    public class Z
    {
    public static int Ten;
    }
    public class A : Z {}
    public class B : Z {}

  17. swag says:

    someone did a bad job overloading the “++” operator?

  18. Tuna Toksoz says:

    class A
    {

    public static int Ten;
    }
    class B:A
    {

    }

  19. mx says:

    small hack :)

    public class A {
    public static decimal Ten;
    }

    public class B {
    static B() {
    A.Ten = 10.5M;
    }

    public static decimal Ten;
    }

    :)

  20. Sean Solbak says:

    public class A
    {
    public static int Ten;
    }

    public class B : A
    {
    }

  21. frostred says:

    public class A
    {
    public static int Ten;
    }
    public class B : A
    {
    }

  22. Eric says:

    They reference the same value, as in the ++B.Ten actually increments A.Ten.

  23. B is derived from A

  24. Brad says:

    public class B
    {
    public static int Ten;
    }

    public class A : B
    {
    }

  25. Soren says:

    B should derive from A (and implicitly gets the Ten static field) so the addition goes 10 + 11 + 11….right?

  26. Erik says:

    B must derive from A. That way, both B.Ten and A.Ten refer to the same value – when B.Ten is incremented, the subsequent call to A.Ten returns ’11’ instead of ’10’.

    Interesting thing to think about. Thanks for the quiz. =)

  27. Simeon says:

    That they share Ten, ie class B inherits from class A.

    public class A
    {
    public static int Ten;
    }

    public class B : A
    {
    }

  28. SyntaxC4 says:

    Assuming this is C# code the ++ operator could be overridden to subtract 8.

  29. DaRage says:

    A.Ten=10?

  30. Chad Myers says:

    B must derive from A and not declare static Ten.

    [Test]
    public void asdf(){
    A.Ten = 10;
    B.Ten = 10;
    Assert.That(32 == (A.Ten + ++B.Ten + A.Ten));
    }

    public class A{
    public static int Ten { get; set; }
    }

    public class B : A{
    }

  31. Jesse says:

    Someone overloaded the + operator of the B.Ten’s type? I’m lost here…

  32. A and B are derived from X. X contains the declaration of the static Ten.

  33. A and B are references to the same object

  34. Matthew says:

    A and B must reference the same object.

  35. Jim says:

    It ran once before.

  36. Uwe says:

    The field should be declared in a base class that both A and B inherit from?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>