Module 06: Array Flashcards
Question: 1
Which of the following arrays has a length of 5?
double[] arr = new double[6];
double[] arr = {0, 1, 2, 3, 4, 5};
double[] arr = new double[]{1, 2.3, 4, 5};
All of these
None of these
None of These
Which of the following are valid arrays?
I. int[] coolArray = {1, 2, 3};
II. int[] threeThings = {1.0, 2.0, 3.0};
III. int[] = {“1”, “2”, “3”};
I only
II only
III only
I and II
I and III
I only
What is the output of the following program?
- public class SayHello {*
- public void run() {*
- String stringArray[] = {“h”, “e”, “l”, “l”, “o”, “w”};*
- for(int i=0; i <= stringArray.length; i++) {*
- System.out.print(stringArray[i]); }*
- } }*
hel
hello
hellow
Error
h e l l o w
Error
You are working at the frozen yogurt shop in the mall. Your boss asks you to count how many flavors have the word chocolate in their name. You begin going through the list of flavors, but quickly realize that their are over 9000 flavors to check! Luckily, your boss stored all of the different flavors in a Java Array named flavorArray on the company computer. Write a Java program to count the number of flavors that have chocolate in their name.
int chocolateFlavorCount = 0;
for(int i = 0; i < flavorArray.length; i++)
{
flavorArray[i].contains(“chocolate”);
}
System.out.println(chocolateFlavorCount);
int chocolateFlavorCount = 0;
for(int i = 0; i < flavorArray.length; i++)
{
if(flavorArray[i].contains(“chocolate”))
{
chocolateFlavorCount++;
}
}
System.out.println(chocolateFlavorCount);
int chocolateFlavorCount = 0;
for(int i = 0; i < flavorArray.length; i++)
{
if(flavorArray[i].contains(“chocolate”))
{
chocolateFlavorCount++;
}
}
System.out.println(chocolateFlavorCount);
What value will be held in mysteryNumber when this code finishes running?
- int mysteryNumber = 0; String[] mysteryArray = {“Finn”, “Jake”, “Bubblegum”};*
- for(int i = 0; i < mysteryArray.length; i++)*
- {*
- mysteryNumber += mysteryArray[i].length();*
- }*
0
17
1
16
17
What will the Array gameBoard contain after this code runs?
- int WIDTH = 3;*
- int HEIGHT = 3;*
- String[] gameBoard = new String[WIDTH * HEIGHT];*
- for(int m = 0; m < HEIGHT; m++)*
- {*
- for(int n = 0; n < WIDTH; n++)*
- {*
- int someNumber = m * WIDTH + n;*
- if(someNumber % 3 == 0) { gameBoard[someNumber] = “X”;*
- }*
- else { gameBoard[someNumber] = “O”;*
- } } }*
- [“X”, “O”, “X”, “O”, “X”, “O”, “X”, “O”, “X”]
- [“X”, “O”, “O”, “X”, “O”, “O”, “X”, “O”, “O”]
- [“O”, “X”, “X”, “O”, “X”, “X”, “O”, “X”, “X”]
- [“O”, “X”, “O”, “X”, “O”, “X”, “O”, “X”, “O”]
- [“X”, “O”, “O”, “X”, “O”, “O”, “X”, “O”, “O”]
Consider the following code snippet:
int[] arr = {1, 2, 3, 4, 5};
int[] copy = arr;
copy[4] = 2;
After this code runs, what is the value of arr[4]?
5
4
2
The code will error.
2
Consider the following method that processes an array to find the smallest value in the array. The array has a nonzero length and is initialized with int values.
- // arr is the array to be processed public static int findMin (int[] arr)*
- { int min = /* some value */;*
- int index = 0; while (index < arr.length)*
- { if (arr[index] < min) {*
- min = arr[index];*
- } index++;*
- } return min;*
- }*
Which replacement(s) for /* some value */ will always result in correct execution of findMin?
I. Integer.MAX_VALUE
II.Integer.MIN_VALUE
III. arr[0]
I only
II only
III only
I and III only
II and III only
I and III only
Given the following values of arr and the mystery method, what will the values of arr be after you execute: mystery()?
- private int[] arr = {-17, -14, 3, 9, 21, 34};*
- public void mystery() {*
- for (int i = 0; i < arr.length / 2; i += 2) {*
- arr[i] = arr[i] * 2;*
- } }*
- {-34, -28, 6, 18, 42, 68}
- {-17, -14, 3, 18, 21, 34}
- {-34, -28, 6, 9, 21, 34}
- {-34, -14, 6, 9, 21, 34}
- {-34, -14, 6, 9, 42, 34}
- {-34, -14, 6, 9, 21, 34}
Which of the following statements is valid? Assume that variable arr is an array of n integers and that the following is true:
arr[0] != arr[i] for all values from 1 through n - 1
- The element arr[0] does not occur anywhere else in the array
- arr has no duplicates
- arr has duplicates
- arr is sorted
- arr is not sorted
- The element arr[0] does not occur anywhere else in the array
Given the following array:
String[] languages = {“Java”, “JavaScript”, “Python”, “C++”};
Which of the following will produce an ArrayIndexOutOfBoundsException?
for (int i = 1; i <= languages.length; i++)
{
System.out.println(languages[i-1]);
}
for (int i = 0; i < languages.length; i++)
{
System.out.println(languages[i]);
}
for (int i = 0; i < languages.length; i++)
{
System.out.println(languages[i-1]);
}
for (int i = 0; i < languages.length; i++)
{
System.out.println(languages[i-1]);
}
The following codes are intended to add 5 to each item in the array.
I.
int[] numbers = {1, 2, 3, 4};
for (int i = 0; i < numbers.length; i++)
{
numbers[i] += 5;
}
II.
int[] numbers = {1, 2, 3, 4};
for (int number : numbers)
{
number += 5;
}
Which statement is true?
- Both I and II will correctly add 5 to the numbers array.
- Neither I nor II will correctly add 5 to the numbers array.
- I will correctly add 5 to the numbers array. II will not correctly add 5 to the numbers array.
- II will correctly add 5 to the numbers array. I will not correctly add 5 to the numbers array.
- I will correctly add 5 to the numbers array. II will not correctly add 5 to the numbers array
The following code is designed to return true if a target value is present in an array of integers.
public boolean search(int[] arr, int target)
{
for (int number : arr)
{
if (number != target)
{
return false;
} } return true;
}
Which of the following statements is true?
- The code will not work correctly because the loop will not access each of the numbers correctly.
- The code will not work correctly because it may return true too soon.
- The code will not work correctly because the method may return false too soon.
- The code will work as intended.
- The code will not work correctly because the method may return false too soon
What does the following code do when executed?
- int[] highTemp = {88, 92, 94, 90, 88, 83};*
- int target = 90; int count = 0;*
- for (int temp : highTemp) {*
- if (highTemp >= target) {*
- count ++;*
- } }*
- System.out.println(count);*
- It will count the number of times the value in the highTemp array exceeds the target value.
- It will produce an error because the conditional statement is not set up correctly.
- It will produce an ArrayIndexOutOfBoundsException
- It will count the number of times that the value in the array is greater than the previous value in the array.
- It will produce an error because the conditional statement is not set up correctly
The following code is intended to shift all elements to the right by one space and wrap the last element around to the first element.
Which statement is true?
- The code will not work as intended. Line 5 should be changed to for (int i = 0; i < arr.length; i++)
- The code will not work as intended. Line 5 should be changed to for (int i = arr.length; i > 0; i–)
- The code will not work as intended. Line 7 should be changed to arr[i-1] = arr[i];
- The code will not work as intended. It will produce an ArrayIndexOutOfBoundsException.
- The code will work as intended.
- The code will work as intended
The following method is intended to return true if the array element value contains the same number in consecutive array elements.
Which of the following are needed to correct the code?
I. Change line 3 to: int i = 0;
II. Change line 4 to: while (i < nums.length - 1)
III. Swap lines 7 and 10
I only
II only
III only
Make both I and II changes
Make both I and II changes