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.

### 39 Responses to Quiz Answers

1. G Stafford says:

Yo Greg….

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

2. Peter Ritchie says: fun stuff.

3. Greg says:

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

4. Greg says:

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

5. Brian says:

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

Interviewers and people who post obscure questions on the tubes.

6. KAmran Shahid says:

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

7. Neil Williams says:

B must be derived from A.

8. 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.

9. 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; }
}
}

10. Felipe says:

B.Ten field:

set
{
ten = value +1
}

This?

11. parent and child says:

thacca.

12. 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 { } ```

13. Glitch4572 says:

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

14. Eber Irigoyen says:

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

15. Kyle says:

class A
{
public static int Ten;
}

class B : A
{
}

16. Jon Sagara says:

B : A

17. 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

18. 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 {}

19. swag says:

20. Tuna Toksoz says:

class A
{

public static int Ten;
}
class B:A
{

}

21. mx says:

small hack public class A {
public static decimal Ten;
}

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

public static decimal Ten;
} 22. Sean Solbak says:

public class A
{
public static int Ten;
}

public class B : A
{
}

23. frostred says:

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

24. Eric says:

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

25. Andrew Browne says:

B is derived from A

26. Brad says:

public class B
{
public static int Ten;
}

public class A : B
{
}

27. Soren says:

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

28. 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. =)

29. Simeon says:

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

public class A
{
public static int Ten;
}

public class B : A
{
}

30. SyntaxC4 says:

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

31. DaRage says:

A.Ten=10?

32. 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{
}

33. alberto says:

A : B

34. Jesse says:

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

35. Jeremiah Clark says:

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

36. Paul Bartlett says:

A and B are references to the same object

37. Matthew says:

A and B must reference the same object.

38. Jim says:

It ran once before.

39. Uwe says:

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