Multiple choice technology programming languages

Which is correct? A. Shape s = new Shape(); s.setAnchor(10,10); s.draw(); B. Circle c = new Shape(); c.setAnchor(10,10); c.draw(); C. Shape s = new Circle(); s.setAnchor(10,10); s.draw(); D. Shape s = new Circle(); s->setAnchor(10,10); s->draw(); E. Circle c = new Circle(); c.Shape.setAnchor(10,10); c.Shape.draw();

  1. A

  2. B

  3. C

  4. D

  5. E

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

Option C is correct: a superclass reference (Shape) can point to a subclass object (Circle). This is polymorphism - you can call methods like setAnchor() and draw() defined in Shape, and they'll execute Circle's implementation. Options A, D, E have syntax errors (abstract instantiation, wrong operators).

AI explanation

Shape s = new Circle(); is valid polymorphism: s is declared with the supertype Shape but refers to a Circle object, and calling s.setAnchor(...) and s.draw() invokes the (possibly overridden) Circle behavior through the Shape reference. Option B fails because Circle c = new Shape() tries to assign a Shape object to a more specific Circle reference without an explicit cast, which doesn't compile; D and E use non-Java syntax (->, c.Shape.method()).