Back to marketplace

Unit 6: Array

No description yet. This curated deck is ready for daily study.

Total cards: 20•Content version: v4•Public deck•Updated: 8/1/2026

Card preview (20)

#1
Front (Question)

How do you initialize an array?

Back (Answer)

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

#2
Front (Question)

What is the defining characteristic of an array?

Back (Answer)

An array is of a fixed size.

#3
Front (Question)

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

Back (Answer)

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.

#4
Front (Question)

How do you find the size of an Array?

Back (Answer)

Array.length

#5
Front (Question)

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

Back (Answer)

(E) I, II, and III

#6
Front (Question)

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.

Back (Answer)

(E) A run-time error will occur.

#7
Front (Question)

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

Back (Answer)

(C) k - 1

#8
Front (Question)

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

Back (Answer)

(C) 1 3

#9
Front (Question)

True or False? Arrays are mutable.

Back (Answer)

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

#10
Front (Question)

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

Back (Answer)

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

#11
Front (Question)

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

Back (Answer)

False. An array holds multiple values of the SAME type.

#12
Front (Question)

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

Back (Answer)

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

#13
Front (Question)

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

Back (Answer)

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.

#14
Front (Question)

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}

Back (Answer)

A. {3,2,2,0}

#15
Front (Question)

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;

Back (Answer)

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

#16
Front (Question)

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

Back (Answer)

C. primitives: 0 boolean: false objects: null

#17
Front (Question)

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 }

Back (Answer)

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

#18
Front (Question)

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

Back (Answer)

A. buses.length

#19
Front (Question)

int[] arrayOne = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; How do we access the even numbers in arrayOne from above?

Back (Answer)

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

#20
Front (Question)

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

Back (Answer)

import java.util.Arrays;