Question 6

Which will legally declare, construct and initialize an array?

A. int [] mylist = { "1", "2", "3"};
B. int [] mylist = (5, 8, 2);
C. int mylist [] [] = {4, 9, 7, 0};
D. int mylist [] = { 4, 3, 7};
E. int [] myList = [3, 5, 6];
F. int myList [] = {4; 6; 5};

Answer: D

The rest are wrong because

A: The array is of type int but the values assigned are String literals.
B: Curly braces should be used instead of ( ).
C: Provides initial values for one dimension, even though the array is two dimensional.
E: Curly braces should be used instead of [].
F. Commas should be used to seperate the values.

No comments: