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:
39 comments Page 3 of 4.

Bhasker said:   10 years 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.

Swetha said:   10 years ago
Explain the option 4 and 5? What is the purpose of dog in option 4 and 5?

BMont said:   9 years ago
4 is right because if there's a data type named "Dog", a "Dog array" will have to be declared in that manner. 5 is wrong, the arrays are now being declared, so including size will "confuse" java. Size is a property so can only be introduced after the main array has been introduced or declared.

MurugenthiranJeya.N said:   9 years ago
Anyone, Please explain the 5th option.

Ashutosh MIshra said:   9 years ago
Can you explain how 4th is correct?

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

It is not specified in option 1.

Sargam said:   8 years ago
Why can't we declare the size of the array?

But while applying the practical programs of java. Sometimes there is a dire need of allocating the array size as besides to that the garbage value is returned as the answer. Please explain.
(1)

Vignesh said:   8 years ago
Why 4 is correct?

It does not have any data type.

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

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


Post your comments here:

Your comments will be displayed after verification.