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 5

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


Jazz

Jazz

E

Surfo

Surfo

E, por que dentro del metodo static, no puedes hacer uso de recursos que no sean estáticos o al menos que sean creados dentro de el .

sireesha

sireesha

D

Hans

Hans

No, E.
Try it yourself!