Java OCA OCP Practice Question 348

Question

What is the result of the following program?

1: public class Main { 
2:   private String color; 
3:   public Main() { 
4:     this("white"); 
5:   } /*w  w  w . j av  a2s  .  c o m*/
6:   public Main(String color) { 
7:     color = color; 
8:   } 
9:   public static void main(String[] args) { 
10:    Main e = new Main(); 
11:    System.out.println("Color:" + e.color); 
12:   } 
13: } 
  • A. Color:
  • B. Color:null
  • C. Color:White
  • D. Compiler error on line 4.
  • E. Compiler error on line 10.
  • F. Compiler error on line 11.


B.

Note

Line 10 calls the constructor on lines 3-5.

That constructor calls the other constructor.

The constructor on lines 6-8 assigns the method parameter to itself.

It leaves the color instance variable on line 2 set to its default value of null.




PreviousNext

Related