arrays Flashcards

(10 cards)

1
Q

Define an array in programming.

A

An array is a variable that can store multiple values of the same data type, identified by an index or subscript.

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

What is meant by the term ‘index’ in arrays?

A

The index or subscript is the zero-based position reference of elements in an array.

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

Declare an array called marks to hold marks for 10 students.

A

int[] marks = new int[10];

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

Assign the following values to an array: 58, 72, 77, 83, 90, 64, 76, 80, 69, 71

A

Use marks[0] = 58;, marks[1] = 72; … until marks[9] = 71;

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

Write a code segment to calculate the sum of an array using a loop.

A

int sum = 0;
for(int k=0; k<marks.Length; k++) {
sum += marks[k];
}

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

What does marks.Length return?

A

It returns the number of elements in the array — in this case, 10.

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

Declare and initialize an array with the first four months of the year.

A

string[] months = { “January”, “February”, “March”, “April” };

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

What error exists in the loop: for (int k = 0; k <= arrValues.Length; k–)?

A

k– causes an infinite loop. It should be k++ and condition should be k < arrValues.Length.

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

Identify and fix this line: sum + arrValues[k];

A

Should be sum += arrValues[k];

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

What does String[] arrValues = st.Split(‘,’); do?

A

Splits the string st into an array using , as the delimiter.

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