Knowee
Questions
Features
Study Tools

Which of the following declarations is correct?int arr[3] = {1, 2, 3, 4};int arr[] = {1, 2, 3};int arr[3] = {1, 2};Both B and C

Question

Which of the following declarations is correct?int arr[3] = {1, 2, 3, 4};int arr[] = {1, 2, 3};int arr[3] = {1, 2};Both B and C

🧐 Not the exact question you are looking for?Go ask a question

Solution

Both B and C are correct.

In option B, int arr[] = {1, 2, 3};, the size of the array is not explicitly defined but it is implicitly determined by the number of elements in the initialization list.

In option C, int arr[3] = {1, 2};, the size of the array is explicitly defined as 3. The array is initialized with two elements and the third element will be automatically initialized to zero.

Option A, int arr[3] = {1, 2, 3, 4};, is incorrect because it tries to initialize an array of size 3 with 4 elements, which is not allowed.

This problem has been solved

Similar Questions

Which of the following statements correctly accesses the third element of the array int arr[5]; in C?int arr[5] = {1, 2, 3, 4, 5};arr[2]arr[3]arr(2)arr(3)array[2]

Which of the following declaration is illegal?a.int a = 0, b = 1, c = 2; int array[3] = {a, b, c};b.int size = 3; int array[size];c.int size = 3; int array[size] = {1, 2, 3};d.In appropriate

How do you initialize an array in C?a.int arr[3] = (1,2,3);b.int arr(3) = {1,2,3};c.int arr[3] = {1,2,3};d.int arr(3) = (1,2,3);

Which of the following is the correct way to initialize an array in C++?int arr[3] = {1, 2, 3};int arr = {1, 2, 3};int arr[3] = 1, 2, 3;int arr[] = {1, 2, 3};

Given the array declaration int arr[5] = {1, 2, 3};, what are the values of arr[3] and arr[4]?Both arr[3] and arr[4] are 0Both arr[3] and arr[4] are uninitializedarr[3] is 3 and arr[4] is 3arr[3] is 2 and arr[4] is 2

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.