MCQs > IT & Programming > Java > The code below is supposed to find the maximum value of array {6,15,2,5,8,14,10,16,11,17,13,7,1,18,3,4,9,12}. How can the code be substantially improved, if at all? public static intmaxOfArray(int[] a) { int length = a.length; if(length<1) throw new NoSuchElementException('Not at least one integer in array'); while (length > 1) { int k = length; for(inti = 0; i< length/2; i++) { k--; if(a[i]>a[k]) { int j = a[i]; a[i] = a[k]; a[k] = j; } } length /=2; } return a[0]; }

Java MCQs

The code below is supposed to find the maximum value of array 
{6,15,2,5,8,14,10,16,11,17,13,7,1,18,3,4,9,12}. How can the code be substantially
improved, if at all? public static intmaxOfArray(int[] a) { int length = a.length; if(length<1) 
throw new NoSuchElementException("Not at least one integer in array"); while (length >
1) { int k = length; for(inti = 0; i< length/2; i++) { k--; if(a[i]>a[k]) { int j = a[i]; a[i] = a[k];
a[k] = j; } } length /=2; } return a[0]; }

Answer

Correct Answer:

* (must fix if (a[i]>a[k]) to if (a[i]<a[k]))

Explanation:

Note: This Question is unanswered, help us to find answer for this one

Java Skill Assessment

Overall Skill Level-Poor

Your Skill Level: Poor

Retake Quizzes to improve it

More Java MCQ Questions

search

Java Skill Assessment

Overall Skill Level-Poor

Your Skill Level: Poor

Retake Quizzes to improve it