Java Programming - Language Fundamentals - Discussion
Discussion Forum : Language Fundamentals - General Questions (Q.No. 6)
6.
Which three are legal array declarations?
- int [] myScores [];
- char [] myChars;
- int [6] myScores;
- Dog myDogs [];
- Dog myDogs [7];
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 2 of 4.
Vignesh said:
8 years ago
Why 4 is correct?
It does not have any data type.
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?
Haftu M said:
7 years ago
A is correct, according to the fundamental array declaration.
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.
Jit said:
7 years ago
4 is valid because Dog is a user data type created by the user.
Thulasi said:
1 decade ago
type var-name[ ];or type [ ]var-name;
array-var = new type[size];or
type var-name[]=new type[size];
In this way we are declaring the arrays
therefore 3 and 5 are wrong
array-var = new type[size];or
type var-name[]=new type[size];
In this way we are declaring the arrays
therefore 3 and 5 are wrong
Madhava said:
4 years ago
Dog is a reference data type.
Annon said:
1 decade ago
As we create our own data type in c, here also we can create the same.
Anil said:
2 years ago
Here we have to initialize the sign of an array while declaring.
That's why 3 is wrong.
That's why 3 is wrong.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers