Which of the following is the correct way to initialize all the elements of array with 0?
Question
Which of the following is the correct way to initialize all the elements of array with 0?
Solution
In C++, you can initialize all elements of an array to 0 in the following way:
int array[5] = {0};
In this case, the array named 'array' has 5 elements, all of which are initialized to 0.
In Python, you can do it like this:
array = [0]*5
In this case, the list named 'array' has 5 elements, all of which are initialized to 0.
In Java, you can do it like this:
int[] array = new int[5];
In Java, when you create an array of primitive types, it is automatically initialized to the default value for the type, which is 0 for integers.
In JavaScript, you can do it like this:
let array = new Array(5).fill(0);
In this case, the array named 'array' has 5 elements, all of which are initialized to 0.
Similar Questions
Which of the following is not a correct way to initialize an array?a.int n[ 5 ] = { 0, 7, 0, 3, 8, 2 };b.int n[] = { 0, 7, 0, 3, 8, 2 };c.int n[ 5 ] = { 7 };d.int n[ 5 ] = { 9, 1, 9 };
Which of the given options correctly initializes a 1D array with the first three natural numbers?
Which of these is an incorrect Statement?ans.None of the mentionedArray can be initialized when they are declared.Array can be initialized using comma separated expressions surrounded by curly braces. It is necessary to use new operator to initialize an array. Previous Marked for Review Next
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};
What is the correct way to declare and initialize a one-dimensional array in C?
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.