What does the following fragment of code display? What do you think the programmer intended the code to do, and how would you fix it?

```
int sum = 0;
int product = 1;
int max = 20;
for (int i = 1; i <= max; i++)
sum = sum + i;
product = product * i;
System.out.println(β€œThe sum is β€œ + sum +
β€œ and the product is β€œ + product);
```


The sum is 210 and the product is 21.0. It is likely that the programmer wanted to compute the product of the first 20 integer values, only the statement
sum = sum + i;
is inside the body of the for loop. Change the for loop to be:
```
for (int i = 1; i <= max; i++){
sum = sum + i;
product = product * i;
}
```

Computer Science & Information Technology

You might also like to view...

Which of the following statements are true?

a. A class should describe a single entity and all the class operations should logically fit together to support a coherent purpose. b. A class should always contain a no-arg constructor. c. The constructors must always be public. d. The constructors may be protected.

Computer Science & Information Technology

For the string text "The cat in the hat is fat on the mat now!" and the search pattern "dog" along with a performance operation of O(1), what is the search time using the Boyer-Moore algorithm?

a. O(45) b. O(126) c. O(9) d. O(42)

Computer Science & Information Technology

Explain why you would use advanced filtering instead of basic filtering.

What will be an ideal response?

Computer Science & Information Technology

During the testing process, sometimes a program is checked for errors by following the steps with a calculator and sample data.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology