nerdexam
Oracle

1Z0-851 · Question #88

Given: 1. public class Plant { 2. private String name; 3. 4. public Plant(String name) { 5. this.name = name; 6. } 7. 8. public String getName() { 9. return name; 10. } 11.} 1. public class Tree…

The correct answer is D. The code will compile if public Plant() { this("fern"); } is added to the Plant class. See the full explanation below for the reasoning.

Question

Given: 1. public class Plant { 2. private String name; 3. 4. public Plant(String name) { 5. this.name = name; 6. } 7. 8. public String getName() { 9. return name; 10. } 11.} 1. public class Tree extends Plant { 2. public void growFruit() { 3. } 4. 5. public void dropLeaves() { 6. } 7. } Which statement is true?

Options

  • AThe code will compile without changes.
  • BThe code will compile if public Tree() { Plant(); } is added to the Tree class.
  • CThe code will compile if public Plant() { Tree(); } is added to the Plant class.
  • DThe code will compile if public Plant() { this("fern"); } is added to the Plant class.
  • EThe code will compile if public Plant() { Plant("fern"); } is added to the Plant class.

How the community answered

(33 responses)
  • A
    15% (5)
  • B
    3% (1)
  • C
    3% (1)
  • D
    73% (24)
  • E
    6% (2)

Community Discussion

No community discussion yet for this question.

Full 1Z0-851 Practice