AP Computer Science Unit 6

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Consider the following code segment. int[] nums = {4, 3, 2, 1, 0}; int sum = 0; for (int k = 0; k <= sum; k++) { if (nums [k] % 2 == 0) { sum += nums [k]; } else { sum -= nums [k]; } } System.out.println(sum); What, if anything, is printed as a result of executing the code segment? (Note that the value of sum (which is in the boolean condition of the for header) is changing) 0 1 2 4 There is a runtime error

1

Consider the following code segment. int[] arr = {4, 3, 2, 1, 0}; int total = 0; for (int k = 0; k <= total; k++) { if (arr[k] % 2 == 0) { total += arr[k]; } else { total -= arr[k]; } } System.out.print(total); What, if anything, is printed as a result of executing the code segment? A 2 B 1 C 0 D -4 E Nothing is printed because the code segment causes a runtime error.

1

Consider the following code segment. int[] arr = {3, 1, 8, 6, 2}; for (int k : arr) { k = k + 10; System.out.print(k + " "); } for (int k : arr) System.out.print(k + " "); What is printed as a result of executing the code segment? 0 1 2 3 4 0 1 2 3 4 3 1 8 6 2 3 1 8 6 2 10 11 12 13 14 0 1 2 3 4 13 11 18 16 12 3 1 8 6 2 13 11 18 16 12 13 11 18 16 12

13 11 18 16 12 3 1 8 6 2

The array fruits is declared below. String [] fruits = {"apples", "bananas", "cherries", "dates"}; Which of the following code segments will cause an ArrayIndexOutOfBoundsException ? I. for (int i = 0; i <= fruits.length; i++) { System.out.println(fruits[i]); } II. for (int i = 0; i <= fruits.length - 1; i++) { System.out.println(fruits[i]); } III. for (int i = 1; i <= fruits.length; i++) { System.out.println(fruits[i - 1]); } A I only B II only C I and III only D II and III only E I, II, and III

I only

The array names is declared below. String [] names = {"amy", "bo", "callie", "dave"}; Which of the following code segments will cause an ArrayIndexOutOfBoundsException ? I. for (int i = 0; i <= names.length; i++) { System.out.println(names [i]); } II. for (int i = 0; i <= names.length - 1; i++) { System.out.println(names [i]); } III. for (int i = 1; i <= names.length; i++) { System.out.println(names [i - 1]); } I only II only I and III only II and III only I, II, and III

I only

Can the size of an Array be changed?

No. The size of an array is established at the time of creation and cannot be changed.

What are arrays of objects set to

Null. No objects are automatically created.

public static int maxVal(int[] a) { int max = a[0]; for(int i = 1; i < a.length; i++) { if(a[i] > max) { max = a[i]; } } return max; } What does this method do?

Returns the max integer value in the array

public static int minVal(int[] a) { int min = Integer.MAX_VALUE; for(int i = 0; i < a.length; i++) { if(a[i] < min) { min = a[i]; } } return min; } What does this method do?

Returns the min integer value in the array

The method mystery() takes an integer array as a parameter and returns a new array that is equal to the old array, except that every value in the new array is double the value it was before. The original array should not be modified. Is the method correct? public static int[] mystery(int arr[]) { int arr2[] = new int[arr.length]; for(int i = 0; i <= arr.length; i++) { arr2[i] = arr[i]*2; } return arr2; }

No

How to make two separate arrays that are copies of each other

int a1[] = {1, 2, 3, 4}; int a2[] = new int[a1.length]; //a2 has same length as a1 for(int i = 0; i < a2.length; i++) { a2[i] = a1[i]; // copy each element }

Initializer list examples

int[ ] scores = {99,98,87,83,65}; String[ ] names = {"Amy", "Bob", "Colin", "Dodi", "Earl"}; Pet[] myPets = {new Pet("Fluffy", 3), new Pet("Wilbur",5), ....}; //5 pets total

Consider the following code segment, which is intended to print the maximum value in an integer array values. Assume that the array has been initialized properly and that it contains at least one element. int maximum = /* missing initial value */; for (int k = 1; k < values.length; k++) { if (values[k] > maximum) { maximum = values[k]; } } System.out.println(maximum); Which of the following should replace /* missing initial value */ so that the code segment will work as intended? A 0 B values[0] C values[1] D Integer.MIN_VALUE E Integer.MAX_VALUE

values[0]

What can you NOT do with for each loops

you cannot change the values in the array because you are only working with a copy of the array element. Also, you cannot get the index of an element. (Note: The inability to change array elements in a for-each loop holds for primitive (int...) values and immutable objects like Strings. You can change objects with for-each loops if their class has mutator/modifier methods. Although you are working with copies of the references, this still allows you to access the original objects.)

What are the valid index values for an array

0 through one less than the number of elements in a 1d array using an index

What do each of these do? scores[0] = 99; scores[4] = 95;

// puts 99 in the first bucket of the array // put 95 in the last bucket of the array (5 buckets total)

What is the index of the first element in an array called a?

0

What does this array look like? int[] scores = new int[5];

0 0 0 0 0

Declaring an array of an object

Account[] myAccounts; myAccounts = new Account[50]; for(int i=0; i < myAccounts.length; i++) { myAccounts[i] = new Account();

Used to represent collections of related data using a single variable rather than multiple variables. Arrays are an example of this.

Data Structures

What can arrays store?

Either primitive data or object reference data (the addresses of objects).

I. int[] arr = {1, 2, 3, 4, 5}; for (int x = 0; x < arr.length; x++) { System.out.print(arr[x + 2]); } II. int[] arr = {1, 2, 3, 4, 5}; for (int x : arr) { System.out.print(x + 2); } Which of the following best describes the behavior of code segment I and code segment II ? Both code segment I and code segment II will print 34. Both code segment I and code segment II will print 34567. Code segment I will cause an ArrayIndexOutOfBoundsException and code segment II will print 34. Code segment I will cause an ArrayIndexOutOfBoundsException and code segment II will print 34567. Both code segment I and code segment II will cause an ArrayIndexOutOfBoundsException.

Code segment I will cause an ArrayIndexOutOfBoundsException and code segment II will print 34567.

Whats are arrays of primitives set to?

Elements of type int are initialized to 0 Elements of type double are initialized to 0.0 Elements of type boolean are initialized to false

What does this array look like? Pet[] myPets = new Pet[5]; myPets[0] = new Pet("Fluffy", 3); myPets[1] = new Pet("Wilbur",5);

Fluffy 3 Wilbur 5 null null null

Consider the following method. public int[] transform(int[] a) { a[0]++; a[2]++; return a; } The following code segment appears in a method in the same class as transform. /* missing code */ arr = transform(arr); After executing the code segment, the array arr should contain {1, 0, 1, 0}. Which of the following can be used to replace /* missing code */ so that the code segment works as intended? I. int[] arr = {0, 0, 0, 0}; II. int[] arr = new int[0]; III. int[] arr = new int[4]; A I only B II only C III only D I and II E I and III

I and III

Consider the following method: public int[] changeArray(int[] a) { a[1] += 3; a[3]++; return a; } The following code segment appears in a method in the same class as changeArray: /* missing code */ arr = changeArray (arr); After executing the code segment, the array arr should contain {0, 3, 0, 1}. Which of the following can be used to replace /* missing code */ so that the code segment works as intended? I. int[] arr = {0, 0, 0, 0}; II. int[] arr = new int[0]; III. int[] arr = new int[4]; I only II only III only I and II I and III

I and III

Consider the following method, which is intended to return the number of strings of length greater than or equal to 3 in an array of String objects. public static int checkString(String[] arr) { int count = 0; for (int k = 0; k < arr.length; k++) { if (arr[k].length() >= 3) { count++; } } return count; } Which of the following code segments compile without error? I. checkString(new String[]); II. checkString(new String[0]); III. String[] str = {"cat", "dog"}; checkString(str); A II only B III only C I and III only D II and III only E I, II, and III

II and III only

Consider the following class definition. public class House { private int yearBuilt; public int getYearBuilt() { return yearBuilt; } /* There may be instance variables, constructors, and other methods not shown. */ } The following code segment, which appears in a class other than House, prints the year each House object in houseArray was built. Assume that houseArray is a properly declared and initialized array of House objects. for (House k : houseArray) { System.out.println(k. getYearBuilt()); } Which of the following could be used in place of the given code segment to produce the same output? I. for (int k = 0; k < houseArray.length; k++) { System.out.println(getYearBuilt(k)); } II. for (int k = 0; k < houseArray.length; k++) { System.out.println(k. getYearBuilt()); } III. for (int k = 0; k < houseArray.length; k++) { System.out.println(houseArray [k]. getYearBuilt()); } I only II only III only I and II only II and III only

III only

Consider the following class definition. public class Toy { private int yearFirstSold; public int getYearFirstSold() { return yearFirstSold; } /* There may be instance variables, constructors, and other methods not shown. */ } The following code segment, which appears in a class other than Toy, prints the year each Toy object in toyArray was first sold by its manufacturer. Assume that toyArray is a properly declared and initialized array of Toy objects. for (Toy k : toyArray) { System.out.println(k.getYearFirstSold()); } Which of the following could be used in place of the given code segment to produce the same output? I. for (int k = 0; k < toyArray.length; k++) { System.out.println(getYearFirstSold(k)); } II. for (int k = 0; k < toyArray.length; k++) { System.out.println(k.getYearFirstSold()); } III. for (int k = 0; k < toyArray.length; k++) { System.out.println(toyArray[k].getYearFirstSold()); }

III only

The Fibonacci numbers are a sequence of integers. The first two numbers are 1 and 1. Each subsequent number is equal to the sum of the previous two integers. For example, the first seven Fibonacci numbers are 1, 1, 2, 3, 5, 8, and 13. The following code segment is intended to fill the fibs array with the first ten Fibonacci numbers. The code segment does not work as intended. int[] fibs = new int[10]; fibs[0] = 1; fibs[1] = 1; for (int j = 1; j < fibs.length; j++) { fibs[j] = fibs[j - 2] + fibs[j - 1]; } Which of the following best identifies why the code segment does not work as intended? A In the for loop header, the initial value of j should be 0. B In the for loop header, the initial value of j should be 2. C The for loop condition should be j < fibs.length - 1. D The for loop condition should be j < fibs.length + 1. E The for loop should increment j by 2 instead of by 1.

In the for loop header, the initial value of j should be 2.

The method mystery() takes an integer array as a parameter and returns a new array that is equal to the old array, except that every value in the new array is double the value it was before. The original array should not be modified. Is the method correct? public static int[] mystery(int arr[]) { for(int i = 0; i < arr.length; i++) { arr[i] = arr[i]*2; } return arr; }

No

public static boolean isFive(int[] a) { boolean b = false; for(int each: a) { if(each == 5) { b = true; } } return b; } What does this method do?

It returns a boolean value depending on whether or not there is a value in the array that equals 5

public static boolean biggerThanZero(int[] a) { boolean b = true; for(int each: a) { if(each < 0) { b = false; } } return b; } What does this method do?

It returns a boolean value depending on whether or not there is a value in the array that is negative

public static boolean fours(int[] a) { boolean b = false; for(int i = 1; i < a.length; i++) { if(a[i-1] == a[i]) { b = true; } } return b; } What does this method do?

It returns a boolean value that depends on whether or not there are any consecutive values in the array.

public static int evens(int[] a) { int count = 0; for(int i = 0; i < a.length; i++) { if(a[i] % 2 == 0) { count++; } } return count++; } What does this method do?

It returns the amount of even values in the array

public static double average(int[] a) { double sum = 0; for(int i = 0; i < a.length; i++) { sum += a[i]; } sum /= a.length; return sum; } What does this method do?

It returns the average of the values in the array

public static void reverse(int[] a) { for(int i = 0; i < a.length/2; i++) { int temp = 0; temp = a[i]; a[i] = a[a.length - 1 - i]; a[a.length - 1 - i] = temp; } } What does this method do?

It reverses the array

public static void left(int[] a) { int first = a[0]; for(int i = 0; i < a.length - 1; i++) { a[i] = a[i + 1]; } a[a.length - 1] = first; } What does this method do?

It shifts all values in the array to the left.

public static void right(int[] a) { int last = a[a.length-1]; for(int i = a.length-1; i > 0; i--) { a[i] = a[i-1]; } a[0] = last; } What does this method do?

It shifts all values in the array to the right.

What can be used to access all the elements in an array and what is it called?

Iteration statements, and it is called traversing the array

The method mystery() takes an integer array as a parameter and returns a new array that is equal to the old array, except that every value in the new array is double the value it was before. The original array should not be modified. Is the method correct? public static int[] mystery(int arr[]) { arr = new int[arr.length]; for(int i = 0; i < arr.length; i++) { arr[i] = arr[i] * 2; } return arr; }

No

Consider the following code segment, which is intended to print the sum of all elements of an array. int[] arr = {10, 5, 1, 20, 6, 25}; int sum = 0; for(int k = 0; k <= arr.length; k++) { sum += arr[k]; } System.out.println("The sum is " + sum); A runtime error occurs when the code segment is executed. Which of the following changes should be made so that the code segment works as intended? The for loop header should be replaced with for (int k = 1; k <= arr.length - 1; k++). The statement in the body of the for loop should be replaced with sum += arr[k - 1]. The statement in the body of the for loop should be replaced with sum += arr[0]. The for loop header should be replaced with for (int k = 0; k <= arr.length; k--). The for loop header should be replaced with for (int k = 0; k < arr.length; k++).

The for loop header should be replaced with for (int k = 0; k < arr.length; k++).

Consider the following method, which is intended to return the index of the first negative integer in a given array of integers. public int indexOfFirstNegative(int[] nums) { int index = 0; while (nums[index] >= 0) { index++; } return index; } What precondition is needed on the values array so that the method will work as intended? The array nums must contain at least one negative integer. The array nums must contain at least one nonnegative integer. The array nums must contain at least one positive integer. No precondition is needed. The method will never work as intended. No precondition is needed. The method will always work as intended.

The array nums must contain at least one negative integer.

Consider the following method, which is intended to return the index of the first negative integer in a given array of integers. public int positionOfFirstNegative(int[] values) { int index = 0; while (values[index] >= 0) { index++; } return index; } What precondition is needed on the values array so that the method will work as intended? A The array values must contain at least one negative integer. B The array values must contain at least one nonnegative integer. C The array values must contain at least one positive integer. D No precondition is needed. The method will never work as intended. E No precondition is needed. The method will always work as intended.

The array values must contain at least one negative integer.

Suppose you have an integer array of scores and you want to sum the values in the array: int[] scores = { ... }; int sum = 0; Write a for each loop that sums the values

for(int each: scores) // each is just variable name (could be x ...) { sum += each; }

Suppose you have an integer array of scores and you want to sum the values in the array: int[] scores = { ... }; int sum = 0; Write a traditional for loop that sums the values

for(int i = 0; i < scores.length; i++) { int s = scores[i]; sum += s; }

Arrays are always passed to methods by copy of the reference (address). What does this mean?

This means that the method actually changes the original array!

When a class has a field that is an array, the array can be initialized in the constructor of the class.

True

When an array is passed as a parameter to a method, a copy of the reference of the array is passed to the formal parameter.

True

Given that a is an array of integers and val is an integer value, which of the following best describes the conditions under which the following code segment will return true? boolean temp = false; for(int x: a) { temp = (x == val); } return temp; Whenever the first element in a is equal to val. Whenever a contains any element which equals val. Whenever the last element in a is equal to val. Whenever only 1 element in a is equal to val.

Whenever the last element in a is equal to val.

The method mystery() takes an integer array as a parameter and returns a new array that is equal to the old array, except that every value in the new array is double the value it was before. The original array should not be modified. Is the method correct? public static int[] mystery(int arr[]) { int arr2[] = new int[arr.length]; for(int i = 0; i <arr2.length; i++) arr2[i]=arr[i]*2; return arr2; }

Yes

The method mystery() takes an integer array as a parameter and returns a new array that is equal to the old array, except that every value in the new array is double the value it was before. The original array should not be modified. Is the method correct? public static int[] mystery(int arr[]) { int arr2[] = new int[arr.length]; for(int i = arr2.length; i > 0; i++) { arr2[i - 1] = arr[i - 1] * 2; } return arr2; }

Yes

Which of the following statements about "for-each" loops (enhanced for loops) is false: You can use a for-each loop to access every element in an array. You cannot get the index of a particular element using a for-each loop. You can modifiy an object which has a modifier method using a for-each loop. You can modify a primitive using a for-each loop.

You can modify a primitive using a for-each loop.

Given the following code segment what will be returned when you execute: getIndexLastSmaller(5); private int[ ] nums= {1, 2, 3, 9, 12, 20}; public int getIndexLastSmaller(int compare) { for (int i = nums.length; i >=0; i--) { if (nums[i] < compare)return i; } return -1; / / shows none found } -1 1 2 You will get an ArrayIndexOutOfBoundsException

You will get an ArrayIndexOutOfBoundsException

int a[ ] = {8, 2, 5, 1, 3}; int[ ] b = a; b[1] = 9; What are the values stored in arrays a and b after the code above executes? a = {8, 2, 5, 1, 3} b = {9, 2, 5, 1, 3} This code results in a NullPointerException. a = {8, 9, 5, 1, 3} b = {8, 9, 5, 1, 3} a = {9, 2, 5, 1, 3} b = {9, 2, 5, 1, 3} a = {8, 2, 5, 1, 3} b = {8, 9, 5, 1, 3}

a = {8, 9, 5, 1, 3} b = {8, 9, 5, 1, 3}

int a1[] = {1, 2, 3, 4}; int a2[] = a1; a2[2] = 7;

a1 is now {1, 2, 7, 4} a2 is now {1, 2, 7, 4}

Consider the following code segment. int[] arr = {1, 2, 3, 4, 5}; Which of the following code segments would correctly set the first two elements of array arr to 10 so that the new value of array arr will be {10, 10, 3, 4, 5} ? A arr[0] = 10; arr[1] = 10; B arr[1] = 10; arr[2] = 10; C arr[0, 1] = 10; D arr[1, 2] = 10; E arr = 10, 10, 3, 4, 5;

arr[0] = 10; arr[1] = 10;

Traverse an array using a for loop forwards

for (int i = 0; i < a.length ; i++) { System.out.println(a[i]); }

Traverse an array using a for loop backwards

for (int i = a.length - 1; i >= 0; i--) { System.out.println(a[i]); }

In the code segment below, assume that the int array numArr has been properly declared and initialized. The code segment is intended to reverse the order of the elements in numArr. For example, if numArr initially contains {1, 3, 5, 7, 9}, it should contain {9, 7, 5, 3, 1} after the code segment executes. /* missing loop header */ { int temp = numArr[k]; numArr[k] = numArr[numArr.length - k - 1]; numArr[numArr.length - k - 1] = temp; } Which of the following can be used to replace /* missing loop header */ so that the code segment works as intended? A for (int k = 0; k < numArr.length / 2; k++) B for (int k = 0; k < numArr.length; k++) C for (int k = 0; k < numArr.length / 2; k--) D for (int k = numArr.length - 1; k >= 0; k--) E for (int k = numArr.length - 1; k >= 0; k++)

for (int k = 0; k < numArr.length / 2; k++)

In the code segment below, assume that the int array nums has been properly declared and initialized. The code segment is intended to reverse the order of the elements in nums. For example, if nums initially contains {2, 4, 6, 8, 10}, it should contain {10, 8, 6, 4, 2} after the code segment executes. /* missing loop header */ { int temp = nums[k]; nums[k] = nums[nums.length - k - 1]; nums[nums.length - k - 1] = temp; } Which of the following can be used to replace /* missing loop header */ so that the code segment works as intended? for (int k = 0; k < nums.length / 2; k++) for (int k = 0; k < nums.length; k++) for (int k = 0; k < nums.length / 2; k--) for (int k = nums.length - 1; k >= 0; k--) for (int k = nums.length - 1; k >= 0; k++)

for (int k = 0; k < nums.length / 2; k++)

Consider the following code segment. int[] numbers = {1, 2, 3, 4, 5, 6}; for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } Which of the following for loops produces the same output as the code segment? A for (int x : numbers) { System.out.println(numbers[x]); } B for (int x : numbers) { System.out.println(numbers); } C for (int x : numbers) { System.out.println(x); } D for (numbers : int x) { System.out.println(numbers[x]); } E for (numbers : int x) { System.out.println(x); }

for (int x : numbers) { System.out.println(x); }

Consider the following code segment. int[] nums = {1, 7, -3, 14, 5, 6}; for (int i = 0; i < nums.length; i++) { System.out.println(nums [i]); } Which of the following for loops produces the same output as the code segment? for (int x : nums) { System.out.println(nums [x]); } for (int x : nums) { System.out.println(nums); } for (int x : nums) { System.out.println(x); } for (nums: int x) { System.out.println(nums [x]); } for (nums: int x) { System.out.println(x); }

for (int x : nums) { System.out.println(x); }

Consider the following two code segments. I. int[] arr = {1, 2, 3, 4, 5}; for (int x = 0; x < arr.length; x++) { System.out.print(arr[x + 3]); } II. int[] arr = {1, 2, 3, 4, 5}; for (int x : arr) { System.out.print(x + 3); } Which of the following best describes the behavior of code segment I and code segment II ? A Both code segment I and code segment II will print 45. B Both code segment I and code segment II will print 45678. C Code segment I will cause an ArrayIndexOutOfBoundsException and code segment II will print 45. D Code segment I will cause an ArrayIndexOutOfBoundsException and code segment II will print 45678. E Both code segment I and code segment II will cause an ArrayIndexOutOfBoundsException.

for (int y = x + 1; y < arr.length; y++)

The code segment below is intended to set the boolean variable duplicates to true if the int array arr contains any pair of duplicate elements. Assume that arr has been properly declared and initialized. boolean duplicates = false; for (int x = 0; x < arr.length - 1; x++) { /* missing loop header */ { if (arr[x] == arr[y]) { duplicates = true; } } } Which of the following can replace /* missing loop header */ so that the code segment works as intended? for (int y = 0; y <= arr.length; y++) for (int y = 0; y < arr.length; y++) for (int y = x; y < arr.length; y++) for (int y = x + 1; y < arr.length; y++) for (int y = x + 1; y <= arr.length; y++)

for (int y = x + 1; y < arr.length; y++)

Consider the following method. public static int getValue(int[] data, int j, int k) { return data[j] + data[k]; } Which of the following code segments, when appearing in another method in the same class as getValue, will print the value 70 ? int arr = {40, 30, 20, 10, 0}; System.out.println(getValue(arr, 1, 2)); int[] arr = {40, 30, 20, 10, 0}; System.out.println(getValue(arr, 1, 2)); int[] arr = {50, 40, 30, 20, 10}; System.out.println(getValue(arr, 1, 2)); int arr = {40, 30, 20, 10, 0}; System.out.println(getValue(arr, 2, 1)); int arr = {50, 40, 30, 20, 10}; System.out.println(getValue(arr, 2, 1));

int[] arr = {50, 40, 30, 20, 10}; System.out.println(getValue(arr, 1, 2));

Consider the following method public int[] addNum(int[] array, int first, int second, int num) { int[] newArray = new int[array.length]; newArray[first] = array[first] + num; newArray[second] = array[second] + num; return newArray; } Which of the following code segments, appearing in the same class as the addNum method, will result in array2 having the contents {0, 0, 13, 0, 9, 0, 0}? int[] array1 = {5, 2, 8, 6, 4, 3, 9}; int[] array2 = addNum(array1, 2, 4, 5); int[] array1 = {5, 8, 2, 4, 6, 3, 9}; int[] array2 = addNum(array1, 2, 4, 5); int[] array1 = {-5, -5, 13, 0, 9, 0, 0}; int[] array2 = addNum(array1, 2, 4, 5); int[] array1 = {0, -5, 8, 0, 9, 0, 0}; int[] array2 = addNum(array1, 2, 4, 5); int[] array1 = {5, 2, 8, 6, 4, 3, 9}; int[] array2 = addNum(array1, 3, 5, 5);

int[] array1 = {5, 2, 8, 6, 4, 3, 9}; int[] array2 = addNum(array1, 2, 4, 5);

Ways to declare an array

int[] nums = new int[10]; int nums[] = new int[10]; //same as above String apStudents[] = new String[20]; double avgs[]= {96.5, 73.2, 86.7, 95.2}; int x = scan.nextInt(); String names[]=new String[x];

int[ ] scores = {99, 98, 87, 83, 65}; What are the length, first index, and last index of scores respectively? Note that length is a field and not a method, unlike the String length() method, so you don't add parentheses after length.

length is 5 first index is 0 last index is 4

Consider the following code segment: int[] myArray = {5, 5, 5, 5, 5};Which of the following code segments would change myArray to {5,7,7,5,5}? myArray[1] = 7; myArray[2] = 7; myArray[2] = 7;myArray[3] = 7; , Not Selected myArray[1, 2] = 7; , Not Selected myArray[2, 3] = 7; , Not Selected myArray = 5, 7, 7, 5,5;

myArray[1] = 7; myArray[2] = 7;

What does this array look like? Pet[] myPets = new Pet[5];

null null null null null

What is the index of the last element in an array called nums?

nums.length - 1

Consider the following code segment, which is intended to print the maximum value in an integer array nums. Assume that the array has been initialized properly and that it contains at least one element. int max = /* missing initial value */; for (int k = 1; k < nums.length; k++) { if (nums[k] > max) { max = nums[k]; } } System.out.println(max); Which of the following should replace /* missing initial value */ so that the code segment will work as intended? 0 nums[0] nums[1] Integer.MIN_VALUE Integer.MAX_VALUE

nums[0]

Consider the following code segment. int[] arr = {1, 2, 3, 4, 5}; for(int x = 1; x < arr.length - 1; x++) { arr[x + 1] = arr[x] + arr[x + 1]; } Which of the following represents the contents of arr after the code segment has been executed?

{1,2,5,9,14}

Consider the following code segment. int[] arr = {10, 20, 30, 40, 50}; for(int x = 1; x < arr.length - 1; x++) { arr[x + 1] = arr[x] + arr[x + 1]; } Which of the following represents the contents of arr after the code segment has been executed? A {10, 20, 30, 70, 120} B {10, 20, 50, 90, 50} C {10, 20, 50, 90, 140} D {10, 30, 60, 100, 50} E {10, 30, 60, 100, 150}

{10, 20, 50, 90, 140}


Ensembles d'études connexes

Maternity NCLEX PN, HESI: Pediatric Pharm

View Set

Ecce Romani Exercise IVe Vocabulary

View Set

Introduction to Curriculum, Instruction, and Assessment

View Set

Washington State portion (real estate exam)

View Set