Java Programming - Language Fundamentals - Discussion
Discussion Forum : Language Fundamentals - General Questions (Q.No. 8)
8.
Which one of the following will declare an array and initialize it with five numbers?
Answer: Option
Explanation:
Option B is the legal way to declare and initialize an array with five elements.
Option A is wrong because it shows an example of instantiating a class named Array, passing the integer value 5 to the object's constructor. If you don't see the brackets, you can be certain there is no actual array object! In other words, an Array object (instance of class Array) is not the same as an array object.
Option C is wrong because it shows a legal array declaration, but with no initialization.
Option D is wrong (and will not compile) because it declares an array with a size. Arrays must never be given a size when declared.
Discussion:
25 comments Page 2 of 3.
Amit said:
1 decade ago
If we done it as then its right.
int[] a = new int[5];/** this line deceleration of array**/
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
a[4] = 5; /** and its initialization of array and it must with deceleration**/
int[] a = new int[5];/** this line deceleration of array**/
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
a[4] = 5; /** and its initialization of array and it must with deceleration**/
Jyoti said:
1 decade ago
Because question is you have to initialize 5 numbers, its not only creating space in memory with by default values 00000. Question is rite. We have to create 5 memory space and give some value for each of the space. So option B is doing this only. Option C is creating 5 spaces in memory but not giving value to each space. So by default its taking 00000.
Arun said:
1 decade ago
Because they assume its not a field variable, since java won't initialize member variables, they are right. But question seems wrong still. What if its an instance variable, C is perfectly right.
Amit said:
1 decade ago
How can you say that option C wrong please anybody explain me?
Vinit katti said:
1 decade ago
int a[] = new a[5]
It creates an array of 5 elements and by default all the elements will be initialized to the default value of the datatype of the array. Since this array is of type int, the default value will be initialized to zero.
It creates an array of 5 elements and by default all the elements will be initialized to the default value of the datatype of the array. Since this array is of type int, the default value will be initialized to zero.
Lokanath Behera said:
1 decade ago
int a[] = new a[5].
When we are creating array at that time it is initialized to zero. So can any one explain why it is wrong option.
When we are creating array at that time it is initialized to zero. So can any one explain why it is wrong option.
XYZ said:
1 decade ago
According to question the Answer is right as the questions suggests the initialization and declaration the most closest is the option B.C is also not wrong but the zero initialization are not considered here. The Question of five number initialization is most closely matched with option B, So it is answer according to me.
Saranya said:
1 decade ago
Option B only declares & initializes the array but option C doesn't.
Arun said:
1 decade ago
int a[] = new int[5], as per me it is not wrong, as a[] will be initialized with 0, if we iterate over a[] and print the values, 0 will be displayed 5 times.
int a[] = new int[5];
for(int s : a){
System.out.println(s);
}
o/p will be:
0
0
0
0
0
int a[] = new int[5];
for(int s : a){
System.out.println(s);
}
o/p will be:
0
0
0
0
0
Rafael said:
1 decade ago
Wrong.
int a[] = new int[5];
is a declaration followed by the implicit initialization of all array values to 0. This is different to e.g. C++ where an array construction would remain with those values formerly stored in the memory the array directs to. However, Java overwrites these values upon array construction.
int a[] = new int[5];
is a declaration followed by the implicit initialization of all array values to 0. This is different to e.g. C++ where an array construction would remain with those values formerly stored in the memory the array directs to. However, Java overwrites these values upon array construction.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers