DSA SheetLesson · no judge

DSA FUNDAMENTALSARRAY BASICS

Print Each Array Element with Its Index

Reading · 6 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 25

Every array slot has an index and a value

The array [4, 2, 7, 1] has four slots. Each slot has a position called its index and a piece of stored data called its element value. Array indices start at 0, so the first slot has index 0 and the last slot has index 3. The value 7 is stored in the slot at index 2.

INDEXELEMENT VALUE
04
12
27
31
The index locates a slot, while the value is stored inside it.

The index and the element value can be different numbers. In this array, index 0 locates the value 4, and index 2 locates the value 7. The number below a slot tells you where the slot is. The number inside the slot is the data you can read.

CHECKPOINT 1Not answered

Which description correctly identifies the slot containing 7?

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

The loop variable names the current index, not the current value

When a loop variable i moves through this array, it takes the index values 0, 1, 2, and 3. For each value of i, arr[i] reads the element value in the slot at that index. The pair changes together as the loop moves: i = 0 gives arr[0] = 4, i = 1 gives arr[1] = 2, i = 2 gives arr[2] = 7, and i = 3 gives arr[3] = 1.

ISLOT LOCATEDARR[I]
0index 04
1index 12
2index 27
3index 31
Each loop variable value identifies one slot and arr[i] reads that slot's value.

At i = 2, the loop variable is not the element value 7. It is the location used inside arr[i]. The expression arr[2] evaluates to 7 because index 2 points to the slot where 7 is stored. Printing i would print 2, while printing arr[i] would print 7.

The condition i < n visits every valid slot exactly once

For this array, n is 4, so the loop starts with i = 0 and tests i < 4. The test is true, the loop reads index 0, and then i increases by 1. The same steps read indices 1, 2, and 3. After index 3, the increment makes i equal to 4, and the test i < 4 becomes false.

The loop stops before trying to read arr[4]. The valid indices end at 3 because the array has four slots and indexing starts at 0. This boundary gives exactly four iterations, one for each slot.

the array [4, 2, 7, 1] with i moving across its four slotsFour adjacent array boxes contain 4, 2, 7, and 1. Beneath them are indices 0, 1, 2, and 3. A path shows i taking those four index values from left to right, producing the pairs 0 with 4, 1 with 2, 2 with 7, and 3 with 1. After the last box, i becomes 4; i < 4 is false, so arr[4] is never accessed.42710 → 41 → 22 → 73 → 10123i = 0i = 1i = 2i = 3i = 4condition falseno arr[4] access
The index moves across the slots, and arr[i] reads each matching value.
CHECKPOINT 2Not answered

Replace the incorrect condition i <= n with the condition that safely visits all four slots of [4, 2, 7, 1].

for (int i = 0; i <= n; i++) {
    print(arr[i]);
}

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

One loop iteration prints one index-value pair

The loop prints two expressions on each iteration. The first expression is i, which supplies the current index. The second expression is arr[i], which supplies the element value stored at that index. Combining them with the required text produces one line for each slot.

CPPIn C++, i prints the index and arr[i] prints the matching value.
int arr[] = {4, 2, 7, 1};
int n = 4;

for (int i = 0; i < n; i++) {
    cout << "Index " << i << ": " << arr[i] << endl;
}
JAVAIn Java, i prints the index and arr[i] prints the matching value.
int[] arr = {4, 2, 7, 1};
int n = 4;

for (int i = 0; i < n; i++) {
    System.out.println("Index " + i + ": " + arr[i]);
}
TEXTThe four iterations produce four index-value lines in order.
Index 0: 4
Index 1: 2
Index 2: 7
Index 3: 1
Next part · Quiz