nerdexam
Oracle

1Z0-803 · Question #31

Given: What should statement1, statement2, and statement3, be respectively, in order to produce the result? Shape: constructor Square: foo Shape: foo

The correct answer is D. Option D. To produce the specified output, statement1 creates a Square object, which implicitly calls the Shape constructor. Statement2 then invokes a Square-specific method that prints "Square: foo", and statement3 calls the inherited foo method from the Shape class on the same Square obj

Working with Inheritance

Question

Given:

What should statement1, statement2, and statement3, be respectively, in order to produce the result? Shape: constructor Square: foo Shape: foo

Exhibits

1Z0-803 question #31 exhibit 1
1Z0-803 question #31 exhibit 2

Options

  • AOption A
  • BOption B
  • COption C
  • DOption D

How the community answered

(58 responses)
  • A
    26% (15)
  • B
    5% (3)
  • C
    12% (7)
  • D
    57% (33)

Why each option

To produce the specified output, statement1 creates a Square object, which implicitly calls the Shape constructor. Statement2 then invokes a Square-specific method that prints "Square: foo", and statement3 calls the inherited foo method from the Shape class on the same Square object.

AOption A

This option would not produce the specified sequence, likely by calling Shape's foo method incorrectly, or failing to call Square's specific method.

BOption B

This option probably alters the order of calls or calls methods that print different strings, thus failing to match the target output.

COption C

This option likely makes incorrect method calls or omits necessary object instantiations, preventing the output Shape: constructor Square: foo Shape: foo from being generated.

DOption DCorrect

Option D contains the necessary statements to first instantiate a Square object (triggering Shape's constructor), then invoke a method unique to the Square class to print "Square: foo", and finally call the inherited foo method from the Shape class on the Square object, resulting in "Shape: foo". This sequence exactly matches the desired output.

Concept tested: Object-oriented inheritance, constructor invocation, method polymorphism

Source: https://docs.oracle.com/javase/tutorial/java/javaOO/inheritance.html

Topics

#inheritance#constructors#method overriding#polymorphism

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice