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 3 of 4.
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.
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?.
Raushan said:
1 decade ago
Why 4th option is wrong ? Explain.
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
Satya cheemarla said:
1 decade ago
How the 4th answer is correct and why 5 is wrong?
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.
Sonia said:
1 decade ago
You can specify the range of subscripts in an array with the
notation start:finish. For example, the declaration:
array income{1997:2000} in1 - in4;
would allow you to refer to income{1997}, income{1998}, etc.
The functions lbound and hbound will return the lowest and
highest indices dened for an array.
notation start:finish. For example, the declaration:
array income{1997:2000} in1 - in4;
would allow you to refer to income{1997}, income{1998}, etc.
The functions lbound and hbound will return the lowest and
highest indices dened for an 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}};
Rajendra Verma said:
1 decade ago
Why first is correct ? int [] myScores [];
Avinash said:
1 decade ago
1st is correct because in 2D or multi dimensional array, we can place the square bracket like this.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers