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?
Array a = new Array(5);
int [] a = {23,22,21,20,19};
int a [] = new int[5];
int [5] array;
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 1 of 3.

Shiwam said:   1 decade ago
char c1 = 064770; is an octal representation of the integer value 27128, which is legal because it fits into an unsigned 16-bit integer.

What is means to deceleration? is it related to range ?

Hi_hello said:   1 decade ago
int a [] = new int[5]; why is t wrong?

Amrita said:   1 decade ago
Exactly.

int a [] = new int[5]; // Why is wrong ?

Can anyone please ans ?

Nuzhat said:   1 decade ago
int a[]=new int[5] is a declaration of array with 5 elements not initialization.

Narendra Mahankale said:   1 decade ago
Question is about declare array & initialize with 5 members.so

int a[]=new int[5] only create space in memory for 5 elements

but not actually store any element.

int [] a = {23,22,21,20,19} this create space in memory as well as store 5 elements viz. 23,22,21,20,19 in memory...

Hope everyone will understand...

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.

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

Saranya said:   1 decade ago
Option B only declares & initializes the array but option C doesn't.

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.

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.


Post your comments here:

Your comments will be displayed after verification.