Given the code fragment:
Which code fragment can be inserted at line n1 to enable the code to print 0.0?
Given the code fragment:
Which code fragment can be inserted at line n1 to enable the code to print 0.0?
In Java, when an instance of a class is created, all its member variables are automatically initialized to their default values. For primitive data types like double, this default value is 0.0. By instantiating an object of the Ball class using 'Ball b = new Ball();', the 'weight' member variable is set to its default value of 0.0. Therefore, the correct answer is to create an instance of the Ball class, which will automatically set 'weight' to 0.0, allowing the code to print 0.0.
D - By default value for primitive Double is 0.0.
Java initialize all number member variables to zero. Only need to instantiate object b with correct answer D.
Ok, correct answer is D