### C++ Array MCQ

Arrays are a foundational concept in C++ programming, allowing you to store a collection of items (typically of the same type) at contiguous memory locations. As you start with C++ or look to refresh your knowledge, this quiz will test your understanding of arrays in the language.

Try your hand at these ten multiple-choice questions, and check your answers below for explanations. Let's jump in!

## 1. How do you declare an array of 5 integers in C++?

a) int array(5);
b) int array;
c) int array;
d) array

b) int array;

### Explanation:

Arrays in C++ are declared by specifying the type, followed by the array name and its size in square brackets.

## 2. Which index refers to the first element of an array?

a) 0
b) 1
c) -1
d) None of the above

a) 0

### Explanation:

In C++, array indices start at 0. So, the first element of an array is accessed using index 0.

## 3. What is the size of an array int arr;?

a) 10 bytes
b) 40 bytes
c) 100 bytes
d) Cannot be determined

b) 40 bytes

### Explanation:

Each int typically occupies 4 bytes. Therefore, an array of 10 integers will occupy 10 x 4 = 40 bytes.

## 4. How do you determine the number of elements in an array named data?

a) sizeof(data)
b) data.length
c) sizeof(data) / sizeof(data)
d) data.size()

c) sizeof(data) / sizeof(data)

### Explanation:

To get the number of elements, you divide the total size of the array by the size of a single element.

## 5. What will be the value of arr after the following code is executed?

``  int arr = {10, 20};``
a) 10
b) 20
c) 0
d) Undefined

c) 0

### Explanation:

If an array is initialized with fewer values than its size, the remaining elements get automatically initialized to zero.

## 6. Which of the following is NOT a correct way to initialize an array?

a) int arr = {1, 2, 3};
b) int arr[] = {1, 2, 3};
c) int arr = (1, 2, 3);
d) int arr = {1, 2};

c) int arr = (1, 2, 3);

### Explanation:

Arrays are initialized using curly braces { }, not parentheses ( ).

## 7. Which of the following will give a compilation error?

a) int arr;
b) int arr[-1];
c) int arr[];
d) All of the above

d) All of the above

### Explanation:

You cannot declare an array of size 0, a negative size, or without a size if it's not being initialized immediately.

## 8. What happens if you try to access an array element using an index that is out of bounds?

a) A compilation error occurs.
b) The program will definitely crash.
c) Undefined behavior.
d) The element will be automatically added to the array.

c) Undefined behavior.

### Explanation:

Accessing an element out of bounds does not cause a compilation error in C++, but it results in undefined behavior, which could be anything, including a crash or incorrect results.

## 9. Which of the following is a two-dimensional array?

a) int arr;
b) int arr;
c) int arr[][];
d) int arr;