Java Programming - Language Fundamentals - Discussion

Discussion Forum : Language Fundamentals - General Questions (Q.No. 6)
6.
Which three are legal array declarations?
  1. int [] myScores [];
  2. char [] myChars;
  3. int [6] myScores;
  4. Dog myDogs [];
  5. Dog myDogs [7];
1, 2, 4
2, 4, 5
2, 3, 4
All are correct.
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 1 of 4.

Janhavi said:   2 years ago
As it seems to be class if we see option 4th and 5th carefully, D is capital
First will create a class named Dog.
class Dog{
String name;
int age;
Dog(String name, int age){
this.name=name;
this.age=age;
}}
public class Info{
main(){
Scanner Sc = new Scanner ();
int age= Sc.nextInt();
String name= Sc.nextLine();
Dog D = new Dog(name,age);

If we create an array to store information of numbers of dogs then we can write as;
Dog[] dog = new dog[size];
So, whenever we create any of our own data types we need to create class of it first.
(3)

Akshay Hegde said:   1 decade ago
4 is not wrong because it represents an array of user-defined objects (objects of 'Dog' class). You can try initializing as "Dog myDog[] = new Dog[10];" this will create an array of size ten which can store ten 'Dog' objects in it.

5 is wrong because you can't declare array size during declaration. You can do that only during initialization "Dog myDog[]" is declaration and "new Dog[10]" is initialization.
(1)

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 de ned for an array.

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.

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.

Sargam said:   8 years ago
Why can't we declare the size of the array?

But while applying the practical programs of java. Sometimes there is a dire need of allocating the array size as besides to that the garbage value is returned as the answer. Please explain.
(1)

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?.

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

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.

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.


Post your comments here:

Your comments will be displayed after verification.