Module 06: Array Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

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

A

None of These

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

A

I only

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

A

Error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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);

A

int chocolateFlavorCount = 0;

for(int i = 0; i < flavorArray.length; i++)

{

if(flavorArray[i].contains(“chocolate”))

{

chocolateFlavorCount++;

}

}

System.out.println(chocolateFlavorCount);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

17

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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”;*
  • } } }*
  1. [“X”, “O”, “X”, “O”, “X”, “O”, “X”, “O”, “X”]
  2. [“X”, “O”, “O”, “X”, “O”, “O”, “X”, “O”, “O”]
  3. [“O”, “X”, “X”, “O”, “X”, “X”, “O”, “X”, “X”]
  4. [“O”, “X”, “O”, “X”, “O”, “X”, “O”, “X”, “O”]
A
  1. [“X”, “O”, “O”, “X”, “O”, “O”, “X”, “O”, “O”]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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.

A

2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A

I and III only

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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;*
  • } }*
  1. {-34, -28, 6, 18, 42, 68}
  2. {-17, -14, 3, 18, 21, 34}
  3. {-34, -28, 6, 9, 21, 34}
  4. {-34, -14, 6, 9, 21, 34}
  5. {-34, -14, 6, 9, 42, 34}
A
  1. {-34, -14, 6, 9, 21, 34}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

  1. The element arr[0] does not occur anywhere else in the array
  2. arr has no duplicates
  3. arr has duplicates
  4. arr is sorted
  5. arr is not sorted
A
  1. The element arr[0] does not occur anywhere else in the array
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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]);

}

A

for (int i = 0; i < languages.length; i++)

{

System.out.println(languages[i-1]);

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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?

  1. Both I and II will correctly add 5 to the numbers array.
  2. Neither I nor II will correctly add 5 to the numbers array.
  3. I will correctly add 5 to the numbers array. II will not correctly add 5 to the numbers array.
  4. II will correctly add 5 to the numbers array. I will not correctly add 5 to the numbers array.
A
  1. I will correctly add 5 to the numbers array. II will not correctly add 5 to the numbers array
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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?

  1. The code will not work correctly because the loop will not access each of the numbers correctly.
  2. The code will not work correctly because it may return true too soon.
  3. The code will not work correctly because the method may return false too soon.
  4. The code will work as intended.
A
  1. The code will not work correctly because the method may return false too soon
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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);*
  1. It will count the number of times the value in the highTemp array exceeds the target value.
  2. It will produce an error because the conditional statement is not set up correctly.
  3. It will produce an ArrayIndexOutOfBoundsException
  4. It will count the number of times that the value in the array is greater than the previous value in the array.
A
  1. It will produce an error because the conditional statement is not set up correctly
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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?

  1. The code will not work as intended. Line 5 should be changed to for (int i = 0; i < arr.length; i++)
  2. The code will not work as intended. Line 5 should be changed to for (int i = arr.length; i > 0; i–)
  3. The code will not work as intended. Line 7 should be changed to arr[i-1] = arr[i];
  4. The code will not work as intended. It will produce an ArrayIndexOutOfBoundsException.
  5. The code will work as intended.
A
  1. The code will work as intended
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

A

Make both I and II changes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Which of the following loops will NOT print every element in an integer array?

(1)

for (int i = 1; i < array.length; i++) {

System.out.println(array[i-1]);

}

(2)

for (int i = 1; i <= array.length; i++) {

System.out.println(array[i-1]);

}

(3)

int i = 1; while (i <= array.length) {

System.out.println(array[i-1]); i++;

}

A

(1)

for (int i = 1; i < array.length; i++) {

System.out.println(array[i-1]);

}

18
Q

Which of the following will initialize a boolean array of three elements all containing true?

I only

II only

III only

I and III only

I, II, and III

A

I and III only

19
Q

What will print out when the following code executes?

0

1

2

3

4

5

A

2

20
Q

Given the array:

String[] fruit = {“Apple”, “Pear”, “Pineapple”, “Carrot”, “Banana”, “Lettuce”};

Which code will correctly replace Carrot and Lettuce in the array?

  1. fruit[4] = “Grape”;
    fruit[6] = “Blueberry”;
  2. fruit[3] = “Grape”;
    fruit[5] = “Blueberry”;
  3. fruit[3, 5] = {“Grape”, “Blueberry”};
  4. fruit[4, 6] = {“Grape”, “Blueberry”};
A
  1. fruit[3] = “Grape”;
    fruit[5] = “Blueberry”;
21
Q

Define Array:

A

Array: An object that can store many values of the same type in a single variable

  • Array is just a list
  • Can be ints or a list of Strings
  • Stores a fixed number of elements of the same type in a single variables
  • Size cannot be changed afterward
  • Type and capacity cannot change
  • Arrays in Java are objects (not primitives)
    [When it is passed to a method you are getting the actual object, not a copy. Any changes to the method updates the original array]
22
Q

array.length

A

Returns the length of the array

23
Q

array[index]

A

Accesses an element in the array to either update or retrieve it

24
Q
A
25
Q

What are the default values for arrays?

A
  1. 0 is the default value for int arrays
  2. null is the default value for objects
  3. 0.0is the default value for double arrays
  4. false is the default value for boolean arrays
26
Q

How do you make an array?

A
  1. Add empty square brackets [] after the type of the left side
  2. Add square brackets [] with the number of elements in the array on the right side

int [] scores = new int[5]

int[] scores = {80, 92, 91, 68, 88};

27
Q

How do you access the last index of an array?

A

int lastIndex = array.length -1;

//This is because arrays are 0 - indexed!

28
Q

How do you access array values?

A

We can access an array value by referencing its index:

int[] scores = {80, 92, 91, 68, 88};

System.out.println(scores[0]};

29
Q

How do you transverse an array?

A
  • Systematically accessing all of our elements in an array is called traversing the array
  • Using loops, we can easily cycle through all of our arrays

Iterating Over an Array

  • As we step through this loop, the value of i changes, allowing us to access each element of the array
30
Q

How do you iterate over an array with a for loop?

A
31
Q

How do you iterate over an array with a while loop?

A
32
Q

What are enhanced for loops?

A

A loop is an alternative for a for or while loop that accesses each value in an array starting with the first value.

  • Is the simplified, but less flexible way to loop through a collection of ideas
  • Referred to as a For-Each loop
  • Starts with the first elements of the array and continued through in order to the last element of the array
33
Q

What is a enhanced for loop variable?

A

Variable created in the enhanced for loop header that contains a copy of the array variable?

34
Q

How does an enhanced for loop look?

A

int[] scores = {80, 92, 91, 68, 88};

for(int score : score)

{

System.out.println(score);

}

  1. score is an enhanced for loop variable
  2. scores is the existing array variable
  3. Prints a copy of values from the array
35
Q

What is the difference between a for loop and an enhanced for loop?

A

Enhanced for loop:

(1) Offer a simplified structure and are especially good when using nested loops
(2) Tend to be easier to write

For loop:

(1) Uses a counter variable which is sometimes needed in the loop
(2) Enhanced for loops only makes a copy with no reference to the index, they are not optimal if you need to update values in the array

36
Q

How do you calculate averages using an array loop?

A
37
Q

How do you find the minimum value using an array loop?

A
38
Q

How do you reorder or reverse arrays?

A
  1. Create a new temp array that is the same size as the original
  2. Copy elements from the original array to the next array in the order that you want
  3. When finished, copy the temp array back over on top of the original array to replace it
39
Q

How do you Shift elements to the right by one for an array?

A
40
Q

How do you Reversing in the order of elements in an array?

A
41
Q

How do you find duplicates in an array?

A