59.1k views
1 vote
Analyze the following code.

double sum = 0;
for (double d = 0; d < 10; sum += sum + d) {
d += 0.1;
}
A. The program has a syntax error because the adjustment statement is incorrect in the for loop.
B. The program has a syntax error because the control variable in the for loop cannot be of the double type.
C. The program compiles but does not stop because d would always be less than 10.
D. The program compiles and runs fine.

1 Answer

2 votes

Answer:

D. The program compiles and runs fine

Step-by-step explanation:

The program is checking if d is < 10 and adding the sum of d.

in the loop its incrementing d by 0.1

running the code and displaying d and sum give the following

0.1 0

0.2 0.1

0.3 0.4

0.4 1.1

0.5 2.6

0.6 5.7

0.7 12

0.8 24.7

0.9 50.2

1 101.3

1.1 203.6

1.2 408.3

1.3 817.8

1.4 1636.9

1.5 3275.2

1.6 6551.9

1.7 13105.4

1.8 26212.5

1.9 52426.8

2 104855.5

2.1 209713

2.2 419428.1

2.3 838858.4

2.4 1677719.1

2.5 3355440.6

2.6 6710883.7

2.7 13421770

2.8 26843542.7

2.9 53687088.2

3 107374179.3

3.1 214748361.6

3.2 429496726.3

3.3 858993455.8

3.4 1717986914.9

3.5 3435973833.2

3.6 6871947669.9

3.7 13743895343.4

3.8 27487790690.5

3.9 54975581384.8

4 109951162773.5

4.1 219902325551

4.2 439804651106.1

4.3 879609302216.4

4.4 1759218604437.1

4.5 3518437208878.6

4.6 7036874417761.7

4.7 14073748835528

4.8 28147497671060.7

4.9 56294995342126.2

5 112589990684257

5.1 225179981368520

5.2 450359962737044

5.3 900719925474094

5.4 1.80143985094819E+15

5.5 3.60287970189639E+15

5.6 7.20575940379279E+15

5.7 1.44115188075856E+16

5.8 2.88230376151712E+16

5.9 5.76460752303424E+16

5.99999999999999 1.15292150460685E+17

6.09999999999999 2.30584300921369E+17

6.19999999999999 4.61168601842739E+17

6.29999999999999 9.22337203685478E+17

6.39999999999999 1.84467440737096E+18

6.49999999999999 3.68934881474191E+18

6.59999999999999 7.37869762948382E+18

6.69999999999999 1.47573952589676E+19

6.79999999999999 2.95147905179353E+19

6.89999999999999 5.90295810358706E+19

6.99999999999999 1.18059162071741E+20

7.09999999999999 2.36118324143482E+20

7.19999999999999 4.72236648286965E+20

7.29999999999999 9.44473296573929E+20

7.39999999999999 1.88894659314786E+21

7.49999999999999 3.77789318629572E+21

7.59999999999999 7.55578637259143E+21

7.69999999999999 1.51115727451829E+22

7.79999999999999 3.02231454903657E+22

7.89999999999999 6.04462909807315E+22

7.99999999999999 1.20892581961463E+23

8.09999999999999 2.41785163922926E+23

8.19999999999999 4.83570327845852E+23

8.29999999999999 9.67140655691703E+23

8.39999999999999 1.93428131138341E+24

8.49999999999999 3.86856262276681E+24

8.59999999999999 7.73712524553363E+24

8.69999999999999 1.54742504910673E+25

8.79999999999998 3.09485009821345E+25

8.89999999999998 6.1897001964269E+25

8.99999999999998 1.23794003928538E+26

9.09999999999998 2.47588007857076E+26

9.19999999999998 4.95176015714152E+26

9.29999999999998 9.90352031428304E+26

9.39999999999998 1.98070406285661E+27

9.49999999999998 3.96140812571322E+27

9.59999999999998 7.92281625142643E+27

9.69999999999998 1.58456325028529E+28

9.79999999999998 3.16912650057057E+28

9.89999999999998 6.33825300114115E+28

9.99999999999998 1.26765060022823E+29

10.1 2.53530120045646E+29

User Drxzcl
by
6.4k points