Preview Questions

6.3. Preview Questions#

Complete these questions before class to prepare for the chapter. Use the dropdowns to check your answers.

6.3.1. True or False#

1. Arrays in C# have a fixed size once created.

Answer

True — Array size is set at creation; use List<T> for dynamic sizing.

2. The first element of a C# array is at index 1.

Answer

False — C# arrays are zero-indexed; the first element is at index 0.

3. A jagged array in C# can have different numbers of columns per row.

Answer

True — Jagged arrays (int[][]) allow rows of different lengths.

4. array.Length returns the total number of elements in a 1D array.

Answer

TrueLength gives the total element count.

5. You can store mixed data types (e.g., int and string) in a single typed array.

Answer

False — A typed array is homogeneous; use object[] for mixed types.

6.3.2. Multiple Choice#

1. How do you declare an integer array of 5 elements in C#?

a) int array[5]
b) int[] array = new int[5]
c) array int[5]
d) int array = new[5]

Answer

b) int[] array = new int[5] — C# uses int[] for the type and new int[5] to allocate.

2. What index accesses the last element of an array with 8 elements?

a) arr[8]
b) arr[7]
c) arr[-1]
d) arr[9]

Answer

b) arr[7] — Last index = Length - 1 = 7.

3. Which loop construct is most natural for iterating all array elements?

a) while
b) do-while
c) foreach
d) goto

Answer

c) foreachforeach iterates elements without managing an index variable.

4. In int[,] grid = new int[3, 4], how many elements does the grid hold?

a) 7
b) 3
c) 4
d) 12

Answer

d) 12 — 3 rows × 4 columns = 12 elements.

5. What happens when you access an array index outside its bounds?

a) Returns 0
b) Returns null
c) Throws IndexOutOfRangeException
d) Wraps around to the other end

Answer

c) Throws IndexOutOfRangeException — Out-of-bounds access throws IndexOutOfRangeException at runtime.

Footnotes