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 2 of 2.

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.

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)

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

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

Abhishek bhardwaj said:   1 decade ago
Answer b is wrong because of absence of curly braces in array element declaration.

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.

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.

Malarvizhi.v said:   2 decades 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?


Post your comments here:

Your comments will be displayed after verification.