Q:

I have troubles with this simple OOP exercise (C#/Java)

Given this piece of code:
public static void main(String[] args)
{
    C c = new A();
    B bl = (B)(new A());
    B b2 = new D();
    A a = new D();
}

I was required to determine the relationships between the classes, based on the given code, and pick the correct answer from the list below:

C inherits from A, B inherits from A, D inherits from A.
A inherits from C, B inherits from A, D inherits from A.
C inherits from A, B inherits from A, D inherits from B.
A inherits from C, C inherits from B, D inherits from A.
A inherits from C, B inherits from A, D inherits from B,
C inherits from B.

From the first line I determined that A inherits from C (due to the upcasting) which eliminated options 1 and 3.
The second line was somewhat problematic to me. I don't know if I'm correct, but I used it to conclude that A does not inherit from B, because otherwise the casting wouldn't have been necessary.
From the third line I determined that D inherits from B, and from the fourth that D inherits from A as well. 
According to some website which had answers to this question, the correct answer is option 4. This I understand, because this is the only option which partially contains the conclusions I arrived at, while the rest contain obvious errors or unsupported claims. What I DON'T understand is, why does C inherit from B? (according to option 4). I did not manage to conclude that from anywhere in the code. 
Why is that true? Help will be much appreciated :)

A:

Just because casting isn't necessary doesn't mean you can spell it out explicitly.  You're right - technically you don't need the (B) in:
B bl = (B)(new A());

Anyways, getting this dependency tree isn't necessarily simple.  Here's all you really know:

A is the child/grandchild/etc of C
A is the child/grandchild/etc of B
D is the child/grandchild/etc of B
D is the child/grandchild/etc of A

It's very important to realize that just because you know that A inherits from C does not mean that A is the direct child of C.  There could be other classes between them in the inheritance chart.
Assuming there aren't any other classes (for all you know, there's a Class 'E' that's between any of those two classes), there are actually two ways to chart it - there's not enough info to tell which it is.
Option A: C is the Base Class
C is the base class - there's no reason it can't, because it doesn't derive from anything else.  A has to derive from both B and C, so B must be up the tree between A and C.  Likewise, D derives from A, making it one long chain:
    public class A : B { }
    public class B : C { }
    public class C { }
    public class D : A { } 

Option B: B is the Base Class.
B is the base class.  After all, it doesn't derive from anything, so there's no reason it can't be.  Similar to the first option, A has to derive from both B and C, which means C is directly up the chain between A and B.  And also similarly, D derives from A, so it goes at the bottom of a long chain.
    public class A : C { }
    public class B { }
    public class C : B { }
    public class D : A { } 

So, when it's all said and done:

A: Inherits from both B and C
B: Possibly inherits from C
C: Possibly inherits from B
D: Inherits from A, B, and C

