C - Arrays - Discussion

You Are Here :: Home > C > Arrays - Discussion

 



Q.

Which of the following correctly initializes an array arr to contain four elements each with value 0?

I int [] arr = {0, 0, 0, 0};
II int [] arr = new int [4];
III int [] arr = new int [4];
for (int i = 0; i < arr. length; i ++)
arr [i] = 0;

A. I only B. III only
C. II and III only D. I, II, and III

Answer: Option D
Explaination:

 


Segment l is an initializer list which is equivalent to int () arr = new int(4);

arr(0) = 0;
arr(1) = 0;
arr(2) = 0;
arr(3) = 0;
Segment II creates four interger, which by are initialized to 0. The for loop in segment III is therefore unnecessary. It is not, however, incorrect.



Discussion

Your Comments Goes here...
NameDiscussion