Unit 6: Array Flashcards
How do you initialize an array?
type[] name = new type[int];
or
type[] name = {type, type, …};
What is the defining characteristic of an array?
An array is of a fixed size.
When would you use a for-each loop instead of a for loop?
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 do you find the size of an Array?
Array.length
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
(E) I, II, and III
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.
(E) A run-time error will occur.
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
(C) k - 1
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
(C) 1 3
True or False?
Arrays are mutable.
True.
Arrays in Java are mutable; values at a specifc index can be changed. However, the size of an array cannot be altered.
How would you declare and initialize a String array called “numbers” holding the values “one”, “two”, and “three”?
String[] numbers = {“one”, “two”, “three”};
OR
String[] numbers = new String[3];
numbers[0] = “one”;
numbers[1] = “two”;
numbers[2] = “three”;
True or False? You can add objects of different types in an array.
False.
An array holds multiple values of the SAME type.
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
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
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];
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.
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. {3,2,2,0}
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;
D. arr[0] = 10; arr [1] = 10;