6.2?Consider the following array: int[] a = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 }; Wh
ID: 3655460 • Letter: 6
Question
6.2?Consider the following array: int[] a = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 }; What is the value of total after the following loops complete? a.?i nt total = 0; for (i nt i = 0; i < 10; i ++) { total = total + a[i ] ; } c.?int total = 0; for (i nt i = 1; i < 10; i = i + 2) { total = total + a[i ] ; } d.?int total = 0; for (int i = 2; i <= 10; i ++) { total = total + a[i ] ; } f.?int total = 0; for (int i = 9; i >= 0; i --) { total = total + a[i ] ; } r6.3?Consider the following array: int[] a = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 }; What are the contents of the array a after the following loops complete? a.?for (int i = 1; i < 10; i ++) { a[i ] = a[i - 1] ; } c.?for (int i = 0; i < 9; i ++) { a[i ] = a[i + 1] ; } d.?for (int i = 8; i >= 0; i --) { a[i ] = a[i + 1] ; } f.?for (i nt i = 1; i < 10; i = i + 2) { a[i ] = 0; } Methods: Write a method that returns the maximum of two integers that are passed in as arguments. The method would have the following header: public static int max(int num1, int num2) Write a complete method including the header that returns true if the incoming argument, a double, is positive. Write a method that returns a 2D array that is a perfect copy of an incoming 2D array. Remember, you don't know the array dimensions beforehand. The method head would be: public static int[][] copyArray(int[][] array) Write a method that would print the reverse of a string. The method header would be: public static void reverseString(String s) Chapter 5 review questions: ?r4.4?What do these loops print? a.?for (i nt i = 1; i < 10; i ++) { System. out. pri nt(i + " ") ; } b.?for (i nt i = 1; i < 10; i += 2) { System. out. pri nt(i + " ") ; } c.?for (i nt i = 10; i > 1; i --) { System. out. pri nt(i + " ") ; } d.?for (i nt i = 0; i < 10; i ++) { System. out. pri nt(i + " ") ; } e.?for (i nt i = 1; i < 10; i = i * 2) { System. out. pri nt(i + " ") ; } f.?for (i nt i = 1; i < 10; i ++) { i f (i % 2 == 0) { System. out. pri nt(i + " ") ; } } ?r4.10?How many iterations do the following loops carry out? Assume that i is not changed in the loop body. a.?for (i nt i = 1; i <= 10; i ++) . . . b.?for (i nt i = 0; i < 10; i ++) . . . c.?for (i nt i = 10; i > 0; i --) . . . d.?for (i nt i = -10; i <= 10; i ++) . . .Explanation / Answer
It can help to do what's called "loop unrolling". Just write out what happens at each stage of the loop. When the loop begins, i = 1, so: a[1] = a[0], and the array is {1, 1, 3, 4, 5, 4, 3, 2, 1, 0} The next time through the loop, i = 2, so: a[2] = a[1], and the array becomes {1, 1, 1, 4, 5, 4, 3, 2, 1, 0} The next time through the loop, i = 3, so: a[3] = a[2], and the array becomes {1, 1, 1, 1, 5, 4, 3, 2, 1, 0} Do a few more steps of this and you'll see, plain as day, exactly what happens to the array.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.