Unit 6: Array Flashcards

1
Q

How do you initialize an array?

A

type[] name = new type[int];
or
type[] name = {type, type, …};

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

What is the defining characteristic of an array?

A

An array is of a fixed size.

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

When would you use a for-each loop instead of a for loop?

A

When you want to access every element of an array and want to refer to elements through a variable name instead of an array index.

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

How do you find the size of an Array?

A

Array.length

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
Which of the following correctly initializes an array arr to contain four elements
each with value 0?
I int[] arr = {0, 0, 0, 0};
II int[] arr = new int[4];
III int[] arr = new int[4];
for (int i = 0; i < arr.length; i++)
arr[i] = 0;
(A) I only
(B) III only
(C) I and III only
(D) II and III only
(E) I, II, and III
A

(E) I, II, and III

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

Refer to the following code segment. You may assume that arr is an array of int
values.
int sum = arr[0], i = 0;
while (i < arr.length)
{
i++;
sum += arr[i];
}
Which of the following will be the result of executing the segment?
(A) Sum of arr[0], arr[1], . . . , arr[arr.length-1] will be stored in sum.
(B) Sum of arr[1], arr[2], . . . , arr[arr.length-1] will be stored in sum.
(C) Sum of arr[0], arr[1], . . . , arr[arr.length] will be stored in sum.
(D) An infinite loop will occur.
(E) A run-time error will occur.

A

(E) A run-time error will occur.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
Consider this program segment:
for (int i = 2; i <= k; i++)
if (arr[i] < someValue)
System.out.print("SMALL");
What is the maximum number of times that SMALL can be printed?
(A) 0
(B) 1
(C) k - 1
(D) k - 2
(E) k
A

(C) k - 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
6. What will be output from the following code segment, assuming it is in the same
class as the doSomething method?
int[] arr = {1, 2, 3, 4};
doSomething(arr);
System.out.print(arr[1] + " ");
System.out.print(arr[3]);
...
public void doSomething(int[] list)
{
int[] b = list;
for (int i = 0; i < b.length; i++)
b[i] = i;
}
(A) 0 0
(B) 2 4
(C) 1 3
(D) 0 2
(E) 0 3
A

(C) 1 3

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

True or False?

Arrays are mutable.

A

True.

Arrays in Java are mutable; values at a specifc index can be changed. However, the size of an array cannot be altered.

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

How would you declare and initialize a String array called “numbers” holding the values “one”, “two”, and “three”?

A

String[] numbers = {“one”, “two”, “three”};

OR

String[] numbers = new String[3];
numbers[0] = “one”;
numbers[1] = “two”;
numbers[2] = “three”;

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

True or False? You can add objects of different types in an array.

A

False.

An array holds multiple values of the SAME type.

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

public static void main(String[] args) {

int[] vec = {8,-2,4,5,-8};

foo(vec);

}

private static void foo(int[] x) {

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

    int y = Math.abs(x[i]);

    for(int j = 0; j < y; j++) {

        System.out.print(x[i] + " ");

    }

    System.out.println();

}

}

Which of the following represents a possible output for the program above?

64

4

16

25

8 8 8 8 8 8 8 8

4 4 4 4

8 8 8 8 8 8 8 8

-2 -2

4 4 4 4

5 5 5 5 5

37
——————————————————-
64

-4

16

25

8 8 8 8 8 8 8 8

-2 -2

4 4 4 4

5 5 5 5 5

-8 -8 -8 -8 -8 -8 -8 -8

A

Correct answer:
8 8 8 8 8 8 8 8

-2 -2

4 4 4 4

5 5 5 5 5

-8 -8 -8 -8 -8 -8 -8 -8

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

Which of the following is the correct method of accessing the 2nd value in an array:

i. value = array[2];
ii. value = array{2};
iii. value = array[1];

A

iii. indexing starts from 0, therefore the second object in the array will be at index 1. You will also specify the index in brackets.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
int[ ] x = {0, 1, 2, 3};
int temp;
int i = 0;
int j = x.length - 1;
while (i < j)
{
   temp = x[i];
   x[i] = x[j];
   x[j] = 2 * temp;
   i++;
   j--;
}

After the following code is executed which of the following are the values in x?

A. {3,2,2,0}
B. {0,1,2,3}
C. {3,2,1,0}
D. {0,2,4,6}
E. {6,4,2,0}
A

A. {3,2,2,0}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
Given the code segment: int[] arr = {1, 2, 3, 4, 5}; whic of the following would set the first two elements of array arr to 10, making the new value of array arr {10, 10, 3, 4, 5} ?
A. arr [0, 1] = 10
B. arr[1] = 10; arr [2] = 10;
C. arr = 10, 10, 3, 4, 5
D. arr[0] = 10; arr [1] = 10;
A

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

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

When arrays are declared, what are the elements initialized for automatically (default values)?
A. primitives: 1 boolean: true objects: null
B. primitives: 1 boolean: false objects: null
C. primitives: 0 boolean: false objects: null
D. primitives: 0 boolean: true objects: null

A

C. primitives: 0 boolean: false objects: null

17
Q

Is this the correct way of traversing an array? What is wrong with the for loop?

for(int i = 0; i < array.size; i++)
{
       // do stuff
}
A

No. You would use “.size()” to traverse an ArrayList, not an Array.
The correct method would be “array.length”.

18
Q

How do you determine the number of elements in an array?
int buses[] = new int[5];

A. buses.length

B. buses.length()

C. buses.size

D. buses.size()

A

A. buses.length

19
Q

int[] arrayOne = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

How do we access the even numbers in arrayOne from above?

A
2 = arrayOne[1]
4 = arrayOne[3]
6 = arrayOne[5]
8 = arrayOne[7]
10 = arrayOne[9]
20
Q

What import statement do you need to use before using an array?

A

import java.util.Arrays;