Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. If you try to access the array by using a negative index value, ____. 2. Arra

ID: 3912021 • Letter: 1

Question

1. If you try to access the array by using a negative index value, ____.

2. Arrays in C# are zero based.

3. Given the following code, what value writes to the console?

int[] test = new int[4];

test[0] = 0;

test[1] = 2;

test[2] = 4;

Console.WriteLine(test[3]);

4. Given the following code, which is a valid example of setting the fifth element of the array?

int[] testArray = new int[10];

5. Given the following code, how many times would the loop below execute?

int[] array = { 5, 10, 15 };

foreach(int x in array)

{

    Console.WriteLine(x);

}

6. Which of the following could be a method heading for a member method that returns an array?

7. What happens when you assign one array to another using the assignment operator?

8. Given the following code, how many times would the loop below execute?

int[] array = { 5, 10, 15, 20 };

for(int i = 0; i < array.Length; i++)

{

    Console.WriteLine(array[i]);

}

9. If an array is sent to a method, which of the following is a valid heading?

10. A limitation of the foreach loop structure when used with arrays is ____.

a compilation error generates a run-time error generates the indexed location is used, and the computer accesses memory to the left of the array the smallest indexed location is accessed

Explanation / Answer

1. A run-time error generates because array index will start from 0 to some range that is given by user.

Ex: Let array size is 4 then index range will be 0 to 3. If we try to access less than 0 or more than 3 it will throw run time error.

2. Arrays in C# are zero based because index will start from 0 only.

3. test[3] will be 0, because every array element will be initialized to 0 at the time of declaration of size.

Here, for test[0], test[1] and test[2], we are giving the values again. But for test[4], 0 will be there.

4. testArray[4]=10; is the correct format to set the fifth element of the array. Because index will start from 0.

5. The loop will execute 3 times, because foreach loop will execute based on the size of the array. Here array size is 3.

6. int[] doSomething() is the method heading. This method is going to return some values so we should not use void. And it is returning array, so we have to give [].

7. Both arrays reference the same memory location.  

Ex: Array1 = Array2 . Here array is a reference because to access the values in array we will use index as Array1[0] or Array2[2] etc.

Array1 is a reference for some variables and Array2 is also a reference for some other variables. So if we assign as shown above, only reference of Array2 will be copied to Array1 not the values.

8. Loop will execute 4 times because in for loop i start from 0 till (size of array - 1) that is 3.

9. void DisplayOutput(double [] anArray) is the valid heading to sent an array to a method.

10. Foreach loop offers read-only access. We cannot modify the values in array.

Ex: foreach(int num in Array){ num*=2; //here only num modified not element of Array }