nerdexam
Oracle

1Z0-851 · Question #73

Given: 10: public class Hello { 11: String title; 12: int value; 13: public Hello() { 14: title += " World"; 15: } 16: public Hello(int value) { 17: this.value = value; 18: title = "Hello"; 19…

The correct answer is C. Compilation fails. The method Hello() is undefined for type Hello. If you want this code to compile there should be

Analyzing and Troubleshooting System Problems

Question

Given: 10: public class Hello { 11: String title; 12: int value; 13: public Hello() { 14: title += " World"; 15: } 16: public Hello(int value) { 17: this.value = value; 18: title = "Hello"; 19: Hello(); 20: } 21: } and: 30: Hello c = new Hello(5); 31: System.out.println(c.title); What is the result?

Options

  • AHello
  • BHello World
  • CCompilation fails.
  • DHello World 5
  • EThe code runs with no output.
  • FAn exception is thrown at runtime.

How the community answered

(46 responses)
  • A
    2% (1)
  • C
    83% (38)
  • D
    9% (4)
  • E
    2% (1)
  • F
    4% (2)

Explanation

The method Hello() is undefined for type Hello. If you want this code to compile there should be

Topics

#Java constructors#constructor chaining#this() invocation#compilation error

Community Discussion

No community discussion yet for this question.

Full 1Z0-851 Practice