Java Programming - Language Fundamentals - Discussion

Discussion :: Language Fundamentals - General Questions (Q.No.3)


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};

Answer: Option D


The only legal array declaration and assignment statement is Option D

Option A is wrong because it initializes an int array with String literals.

Option B is wrong because it use something other than curly braces for the initialization.

Option C is wrong because it provides initial values for only one dimension, although the declared array is a two-dimensional array.

Malarvizhi.V said: (Aug 12, 2010)  
How you said {4,3,7} is one dimensions array?

One dimensions array meants only one value will be declare but you repersend 3 values how?

Disha said: (Oct 1, 2010)  
No it means that the array has both rows and columns while in 1d arrays, we have only rows. So, it represent 3 elements in 1 row.

Shah Reza said: (Jun 1, 2011)  
Its answer is D means oneDimensionalArray because the declaration of oneDimensionalArray is type var-name[];

and in D it is clear that int myList[], where int is the type of data and myList is array variable.

in case of C, twoDArray but value provided are only oneDArray.

Abhishek Bhardwaj said: (Nov 23, 2011)  
Answer b is wrong because of absence of curly braces in array element declaration.

Vikasku652 said: (Feb 17, 2012)  
How to initialize a 2 dimensional array ?

Cosmicguypradeep said: (May 4, 2012)  
2-d array initialization

int MyList[][] = {{1,2,3},{4,5,6},{7,8,9}};

Which means a[i][j]
Rows and columns

1 2 3
4 5 6
7 8 9

The same can be declared as 1-d arrays as
int MyList[]= {1,2,3};
Only one row

1 2 3

Krishna said: (May 22, 2012)  
Other type of array initialize:

int a[]= new int[10];

in this array what is capacity and size?

Capacity is : 10 (nothing but length of array)
size is : 3(filled memory locations)

Sagar Gaikwad said: (Jun 27, 2012)  
Answer D is correct.

Because the declaration of 1d array for compile time is,

Datatype array_name[]={values};

Hence D is correct.

Arun said: (Aug 19, 2013)  
Above option(C) would have been correct, if we had declared it this way.

int myList [] [] = {{4,9,7,0}};

One more opening and closing curly braces would be required for 2 dimensional array.

Sonee Gupta said: (Oct 3, 2013)  
2-d initialisation of array:

int ar[2][3]={1,1,1,2,2,2};


int ar[][]={{1,2,3},{3,4,5}};


int ar[][]={

Khushbu Ahuja said: (Jan 18, 2014)  
I think option D should be correct as,

int a[3][3] = {1,2,3,4}

Than by default all other elements will be null.

Pooja said: (May 15, 2014)  
@Sonee Guptha.

int ar[2][3]= {1,1,1,2,2,2};

Is this 2d? 2d requires double braces right?

Zany said: (Mar 5, 2015)  
int ar[][] = {{1,2,3},{3,4,5}}; is 2d array initialization.

Puru said: (May 29, 2015)  
I don't understand why C is incorrect?

Deep Vakharia said: (Aug 26, 2015)  

C is Incorrect because 2d Array is not being initialized as the way they have give.

Aslam said: (Jan 6, 2016)  
Frustrated with your answers, is it a correct syntax for array declaration and initialization. Completely wrong I thought. My known declaration is:

DataType [] ReferenceVariable = {Elements Separated by Comma}

Mohammadi said: (Aug 10, 2016)  
@Sonee Gupta.

I agree with your answer. That's the right method.

Hasan said: (Jan 14, 2017)  
dataType[] arrayRefVar; // preferred way.
dataType arrayRefVar[]; // works but not preferred way.
Note :\' The style dataType[] arrayRefVar is preferred. The style dataType arrayRefVar[] comes from the C/C++ language and was adopted in Java to accommodate C/C++ programmers.

So we can say that D is correct.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.