Course Hero Logo

12 fill in the code to complete the following method

Course Hero uses AI to attempt to automatically extract content from documents to surface to you and others so you can study better, e.g., in search results, to enrich docs, and more. This preview shows page 6 - 10 out of 13 pages.

12)Fill in the code to complete the following method for sorting a list.public static void sort(double[] list) {________;}public static void sort(double[] list, int high) {if (high>1) {// Find the largest number and its indexint indexOfMax=0;double max=list[0];for (int i=1; i<=high; i++) {if (list[i]>max) {max=list[i];indexOfMax=i;}}// Swap the largest with the last number in the listlist[indexOfMax]=list[high];list[high]=max;// Sort the remaining listsort(list, high-1);}}12)______
A)sort(list)B)sort(list, list.length-2)C)sort(list, list.length)D)sort(list, list.length-1)
13)Analyze the following recursive method.public static long factorial(int n) {return n * factorial(n-1);}13)______
14)Fill in the code to complete the following method for binary search.public static int recursiveBinarySearch(int[] list, int key) {int low=0;int high=list.length-1;return ________;}public static int recursiveBinarySearch(int[] list, int key,int low, int high) {if (low>high) // The list has been exhausted without a matchreturn-low-1; // Return-insertion point-1int mid=(low+high) / 2;if (key<list[mid])return recursiveBinarySearch(list, key, low, mid-1);else if (key==list[mid])
return mid;elsereturn recursiveBinarySearch(list, key, mid+1, high);}14)______A)recursiveBinarySearch(list, key, low, high)B)recursiveBinarySearch(list, key, low+1, high-1)C)recursiveBinarySearch(list, key, low-1, high+1)D)recursiveBinarySearch(list, key)15)What is the return value for xMethod(4) after calling the following method?static int xMethod(int n) {if (n==1)return 1;elsereturn n+xMethod(n-1);}15)______A)9B)12C)10D)1116)Analyze the following two programsApublic class Test {public static void main(String[] args) {xMethod(5);}
public static void xMethod(int length) {if (length>1) {System.out.print((length-1)+" ");xMethod(length-1);}}}Bpublic class Test {public static void main(String[] args) {xMethod(5);}public static void xMethod(int length) {while (length>1) {System.out.print((length-1)+" ");xMethod(length-1);}}}16)______A)The two programs produce the same output 5 4 3 2 1.B)Program A produces the output 4 3 2 1 and Program B prints 4 3 2 1 1 1....1 infinitely.C)The two programs produce the same output 4 3 2 1.D)The two programs produce the same output 1 2 3 4 5.E)The two programs produce the same output 1 2 3 4.17)

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 13 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Fall
Professor
N/A
Tags
Recursion, base case, Fibonacci number, insertion point

Newly uploaded documents

Show More

Newly uploaded documents

Show More

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture