APCS 6.1-6.3 Practice
Mixed up Code The following method has the correct code to return the largest value in an integer array called vals, but the code is mixed up. Put the code in order from 1-8. 7 1 6 2 3 4 5 8 Given that array is an array of integers and target is an integer value, which of the following best describes the conditions under which the following code segment will return true? Whenever the first element in array is equal to target. Whenever array contains any element which equals target. Whenever the last element in array is equal to target. Whenever only 1 element in array is equal to target. What code would you add to the following to output the length of each String that is returned from the following method? What are some of the reasons you would use an enhanced for-each loop instead of a for loop?I: If you wish to access every element of an array. II: If you wish to modify elements of the array. III: If you wish to refer to elements through a variable name instead of an array index. I only II only III only I and II I and III II and III I, II, and III Given the following code segment, which of the following will cause an infinite loop? Assume that temp is an int variable initialized to be greater than zero and that a is an array of integers.for ( int k = 0; k < a.length; k++ ){ while ( a[ k ] < temp ) { a[ k ] *= 2; }} The values don't matter this will always cause an infinite loop. Whenever a includes a value that is less than or equal to zero. Whenever a has values larger then temp. When all values in a are larger than temp. Whenever a includes a value equal to temp. What is the output? 00000 267125 412142410 Nothing because there is Compiler Error None of the above Which of the following loop headers will not cause an ArrayIndexOutOfBounds error while traversing the array scores? Will cause error for (int i = scores.length - 1; i >= 0; i++) for (int i = 0; i <= scores.length; i++) Will not cause error for (int i = 0; i < scores.length; i++) for (int i = 0; scores.length > i; i++) for (int i = 1; i < scores.length; i++) Given the following values of a and the method mystery what will the values of a be after you execute: mystery( )?private int[ ] a = {-20, -15, 2, 8, 16, 33};public void mystery(){ for (int i = 0; i < a.length i+=2) { a[i] = a[i] * 2; }} {-40, -30, 4, 16, 32, 66} {-40, -30, 4, 8, 16, 33} {-20, -15, 2, 16, 32, 66} {-40, -15, 4, 8, 16, 33} {-40, -15, 4, 8, 32, 33} The following method has the correct code to triple the first four elements in the array, but parts of the code are missing. Fill in the blanks. public void tripleFirstFour() { for (int i = 0; i < 4 ; i++) { values[i] = values[i] * 3; } } What will the following code print out? public class Test1{ public static void main(String[] args) { String[] names = {"Jamal", "Emily", "Destiny", "Mateo", "Sofia"}; int index = 1; System.out.println(names[index - 1]); index++; System.out.println(names[index]); System.out.println(names[index / 2]); names[index] = "Rafi"; index--; System.out.println(names[index + 1]); }}