Java Programming - Language Fundamentals - Discussion

Discussion Forum : Language Fundamentals - General Questions (Q.No. 6)
6.
Which three are legal array declarations?
  1. int [] myScores [];
  2. char [] myChars;
  3. int [6] myScores;
  4. Dog myDogs [];
  5. Dog myDogs [7];
1, 2, 4
2, 4, 5
2, 3, 4
All are correct.
Answer: Option
Explanation:

(1), (2), and (4) are legal array declarations. With an array declaration, you can place the brackets to the right or left of the identifier. Option A looks strange, but it's perfectly legal to split the brackets in a multidimensional array, and place them on both sides of the identifier. Although coding this way would only annoy your fellow programmers, for the exam, you need to know it's legal.

(3) and (5) are wrong because you can't declare an array with a size. The size is only needed when the array is actually instantiated (and the JVM needs to know how much space to allocate for the array, based on the type of array and the size).

Discussion:
40 comments Page 2 of 4.

Neha said:   10 years ago
Should not the size of the column in the multidimensional array be specified?

It is not specified in option 1.

Vignesh said:   9 years ago
Why 4 is correct?

It does not have any data type.

Sarita goswami said:   9 years ago
Dog is not variable. So how can you say 4 option is correct?

Prameela said:   8 years ago
Can anyone explain the creation of own data type in Java?

Bhasker said:   1 decade ago
An array is used to store elements in specific fixed size but what we are using here is an array of Dynamic size. So that's why declaration is not possible in the array element.

Arjunkulothungan said:   8 years ago
Can anyone please explain how to create an own datatype?

Jit said:   8 years ago
4 is valid because Dog is a user data type created by the user.

Madhava said:   5 years ago
Dog is a reference data type.

Anil said:   3 years ago
Here we have to initialize the sign of an array while declaring.

That's why 3 is wrong.

Ramesh Gupta said:   3 years ago
Here option A declares a variable named myScores as a two-dimensional array of int type. The [][] denotes the dimensions of the array.


Post your comments here:

Your comments will be displayed after verification.