The following bit of code is attempting to find the sum of a

The following bit of code is attempting to find the sum of all the elements in the array \'codes\'. One line contains a logic error. Which line is it?

long[] codes = ...; // Assume the array has been filled with values
...
long sum = 0; // line 1
for ( int i = 1; i < codes.length; i++ ) { // line 2

sum = sum + codes[i]; // line 3

}

Which of the following is true about a sifting operation?

None of the above.

The following bit of code is attempting to find the average number of characters in all of the Strings in a String array. One line has a syntax error. Which line is it?

String[] names = ... // Assume the array has been filled with values
...
double averageLength = 0; // line 1
int i;
for (i = 0; i < names.length(); i++ ) { // line 2

averageLength += names[i].length(); // line 3

}
averageLength = averageLength / i; // line 4

The following bit of code is attempting to find the x-coordinate of the Point that is farthest to the right in the x-y plane in the array \'points\'. One line contains an error. Which line is it?

Point[] points = ... // Assume the array has been filled with Point object references
...
double xMax = points[0].getX(); // line 1
for ( int i = 1; i < points.length; i++ ) { // line 2

if ( points[i].getX() > xMax ) { // line 3

xMax = points[i]; // line 4

}

}

The operation may produce more than one result.

Solution

1.) Line 2 have a logic error. The answer will be none of the above. But it will give wrong result as array starts from index 0 in Java. So, if array is [1,2,3] then it will give 5 as answer which is incorrect as it should give 6 as answer.

2.) Line 2 will give error as you can predict the length of a multidimensional array.

The following bit of code is attempting to find the sum of all the elements in the array \'codes\'. One line contains a logic error. Which line is it? long[] co

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site