What will make this code compile and run?

Given the following code:
public class Simple { /* Line 1 */
public float price; /* Line 2 */
public static void main (String[] args) { /* Line 3 */
Simple price = new Simple (); /* Line 4 */
price = 4; /* Line 5 */
} /* Line 6 */
} /* Line 7 */
What will make this code compile and run?

Given the following code:
public class Simple { /* Line 1 */
public float price; /* Line 2 */
public static void main (String[] args) { /* Line 3 */
Simple price = new Simple (); /* Line 4 */
price = 4; /* Line 5 */
} /* Line 6 */
} /* Line 7 */
What will make this code compile and run?

A.
Change line 2 to the following:
Public int price

B.
Change line 4 to the following:
int price = new simple ();

C.
Change line 4 to the following:
Float price = new simple ();

D.
Change line 5 to the following:
Price = 4f;

E.
Change line 5 to the following:
price.price = 4;

F.
Change line 5 to the following:
Price = (float) 4:

G.
Change line 5 to the following:
Price = (Simple) 4;

H.
The code compiles and runs properly; no changes are necessary

Explanation:
price.price =4; is correct, not price=4;
The attribute price of the instance must be set, not the instance itself.



Leave a Reply 0

Your email address will not be published. Required fields are marked *