unit 6 Flashcards

1
Q

What is an array in Java?

A

An object that stores multiple values of the same type.

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

How do you declare an array?

A

dataType[] arrayName;

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

How do you initialize an array with size 5?

A

new int[5];

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

How do you initialize an array with specific values?

A

int[] arr = {1

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

What is the index of the first element in an array?

A

0

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

What is the index of the last element in an array?

A

array.length - 1

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

How do you find the length of an array?

A

arrayName.length

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

How do you access the 3rd element in an array?

A

arrayName[2]

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

Can you change the value of an element in an array?

A

Yes

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

What happens if you access an index outside array bounds?

A

ArrayIndexOutOfBoundsException

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

How do you traverse an array?

A

Use a for loop.

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

Write a basic array traversal loop.

A

for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); }

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

What is an enhanced for loop?

A

A loop that automatically goes through each element of an array.

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

Write an enhanced for loop example.

A

for (int x : arr) { System.out.println(x); }

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

Can you modify an array element with an enhanced for loop?

A

No

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

What is array traversal?

A

Visiting each element of an array in sequence.

17
Q

What is a common algorithm when traversing arrays?

A

Finding maximum

18
Q

How do you sum elements in an array?

A

Initialize a sum variable and add each element in a loop.

19
Q

How do you find the maximum value in an array?

A

Initialize max to arr[0]

20
Q

How do you find how many times a value appears in an array?

A

Use a loop with an if-statement and a counter.

21
Q

How do you copy an array manually?

A

Create a new array and copy elements one by one.

22
Q

What is array aliasing?

A

Two references pointing to the same array object.

23
Q

How can array aliasing cause problems?

A

Changes made through one reference affect the other.

24
Q

How do you prevent aliasing?

A

Create a new array and copy each element.

25
How can you reverse an array?
Use a loop that swaps elements from ends moving toward the center.