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.
Federer said:
1 decade ago
Memory is not allocated at the time of declaration.
Eg: int[10] arr; -- not allowed.
Instead we can use new while using array size
Therefore int[] arr= new int[0]; is valid.
Eg: int[10] arr; -- not allowed.
Instead we can use new while using array size
Therefore int[] arr= new int[0]; is valid.
Thasnim said:
1 decade ago
Please explian why an array can't declare with size?
Bharathraj said:
1 decade ago
Why 5 option is wrong?
Govardhan said:
1 decade ago
Even option 4 is wrong it gives compilation error saying that Dog can not resolve array type.
Guru said:
1 decade ago
4 is not correct you can verify by making program and declare like this.
Nidhin said:
1 decade ago
Please explain in detail why 4 & 5 are wrong.
Disha said:
1 decade ago
3) and (5) are wrong because you can't declare an array with a size. Please explain why?
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?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers