Level 4 - Arrays Flashcards

1
Q

Declare an array of integers called ‘values’ with room to store 10 integers

A

int[] values = new int[10];

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

What value displays?

int[] nums = {10, 3, 4, 5, 6, 7, 5};

System.out.println( nums[2]);

A

4

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

What is output?

int[] list = new int[3];

list[1] = 2;

System.out.println(list.length);

A

3

//list[0] and list[2] both have a value of 0

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

What value displays?

int[] nums = {5, 1, 34, 12, 11, 71, 5};

System.out.println( nums[7]);

A

Array out of bounds runtime exception - the last element is nums[6]

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

Describe what the following code segment does:

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

nums[i] = Math.abs(nums[i]);

A

makes everything in the array nums positive

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

Write a loop that locates the first occurrence of a negative integer in an array called ‘list’. When the loop is finished, the variable should contain the index of the negative number. The variable should be -1 if there was no negatives

A

int pos = -1;

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

{

if(list[i] < 0)

pos = i;

}

return pos;

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

Write a loop that displays the length of all the Strings in a string array called ‘someText’

A

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

System.out.println( someText[i].length() );

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

What value displays?

String[] pets = {“dog”, “cat”, “fish”, “pig”, “hamster”}; System.out.println( pets[1]);

A

cat

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

What value displays?

String[] pets = {“dog”, “cat”, “fish”, “pig”, “hamster”}; System.out.println( pets[2].length());

A

4

//the pet at position 2 was ‘fish’ and it had 4 characters

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

What value displays?

String[] pets = {“dog”, “cat”, “fish”, “pig”, “hamster”};

System.out.println( pets.length);

A

5

// .length asks how many spots in the array

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

What value displays?

String[] pets = {“dog”, “cat”, “fish”, “pig”, “hamster”};

System.out.println( pets[3]+pets[0]);

A

pigdog

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

Use a for loop to fill an array called ‘randoms’ with a random number from 1-100. (assume the array has been created)

A

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

{

randoms[i] = (int)(Math.random()*100)+1;

}

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

What is the issue with this array?

int [] numbers = new int [4];

numbers[1] = 3;

numbers[2] = 56;

numbers[3] = 34;

numbers[4] = 2;

A

“numbers[4]” is out of bounds of the array.

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

Write a line of code that displays the length of the array: dogfight

A

System.out.print(dogfight.length);

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

How does this change each element of the array?

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

{

b[i] += 3;

}

A

The loop adds 3 to each element of the array “b.”

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