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.
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
Ramesh Gupta said:
2 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.
Vinoth.g said:
1 decade ago
int [][]arr = new [2][2];
int []arr[] = new [2][2];
int arr[][] = new [2][2];
int var[][] = new int [][]{{1,2},{3,4}};
int []arr[] = new [2][2];
int arr[][] = new [2][2];
int var[][] = new int [][]{{1,2},{3,4}};
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.
It is not specified in option 1.
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.
Avinash said:
1 decade ago
1st is correct because in 2D or multi dimensional array, we can place the square bracket like this.
Govardhan said:
1 decade ago
Even option 4 is wrong it gives compilation error saying that Dog can not resolve array type.
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.
Disha said:
1 decade ago
3) and (5) are wrong because you can't declare an array with a size. Please explain why?
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