Java OCA OCP Practice Question 145

Question

Given:

3. class Button { 
4.   Button() {  System.out.print("b ");  } 
5.   Button(String name) { 
6.     this();   System.out.print("bn " + name); 
7.   } /*from   w ww  .  j  a v a  2s  .  c o  m*/
8. } 
9. public class HomeButton extends Button { 
10.   HomeButton() {  System.out.print("h ");  } 
11.   HomeButton(String name) { 
12.     this();   System.out.print("hn " + name); 
13.   } 
14.   public static void main(String[] args) { new HomeButton("x "); } 
15. } 

What is the result?

  • A. h hn x
  • B. hn x h
  • C. b h hn x
  • D. b hn x h
  • E. bn x h hn x
  • F. b bn x h hn x
  • G. bn x b h hn x
  • H. Compilation fails


C is correct.

Note

The constructors call their superclass constructors, which execute first, and that constructors can be overloaded.




PreviousNext

Related