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 4 of 4.
Prathamesh said:
1 decade ago
We can't declare array with size, when we declare array, we are just giving reference, size given at run time with the help of 'new' operator.
Arrays are dynamically created objects in JAVA.
Hence, its compilation error if we declare array with size.
Arrays are dynamically created objects in JAVA.
Hence, its compilation error if we declare array with size.
Satya cheemarla said:
1 decade ago
How the 4th answer is correct and why 5 is wrong?
Gaurav said:
1 decade ago
Please explain why 4 is correct and 5 is incorrect .In C and C++ we have learned that array is static and it is necessary to declare its size before its initialization...please clarify this
Thasnim said:
1 decade ago
Please explian why an array can't declare with size?
Kedar said:
1 decade ago
Even though we declare the array just shown in option
A) we can't use it further it shows an error.
A) we can't use it further it shows an error.
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
Annon said:
1 decade ago
As we create our own data type in c, here also we can create the same.
Shilpa said:
1 decade ago
3) and (5) are wrong because you can't declare an array with a size. Please explain why? also please describe (4) (5) option.
Is option (4) right? can we create our own datatype for array?.
Is option (4) right? can we create our own datatype for array?.
Disha said:
1 decade ago
3) and (5) are wrong because you can't declare an array with a size. Please explain why?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers