Java Programming - Language Fundamentals - Discussion

Discussion Forum : Language Fundamentals - General Questions (Q.No. 3)
3.
Which will legally declare, construct, and initialize an array?
int [] myList = {"1", "2", "3"};
int [] myList = (5, 8, 2);
int myList [] [] = {4,9,7,0};
int myList [] = {4, 3, 7};
Answer: Option
Explanation:

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.

Discussion:
18 comments Page 1 of 2.

Malarvizhi.v said:   1 decade ago
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:   1 decade ago
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:   1 decade ago
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:   1 decade ago
Answer b is wrong because of absence of curly braces in array element declaration.

Vikasku652 said:   1 decade ago
How to initialize a 2 dimensional array ?

Cosmicguypradeep said:   1 decade ago
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:   1 decade ago
Other type of array initialize:

int a[]= new int[10];
a[0]=6;
a[1]=8;
a[2]=4;

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:   1 decade ago
Answer D is correct.

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

Datatype array_name[]={values};

Hence D is correct.

Arun said:   1 decade ago
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.
(1)

Sonee gupta said:   1 decade ago
2-d initialisation of array:

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

or

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

or

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


Post your comments here:

Your comments will be displayed after verification.