Java Programming - Language Fundamentals - Discussion

Discussion :: Language Fundamentals - General Questions (Q.No.8)

8. 

Which one of the following will declare an array and initialize it with five numbers?

[A]. Array a = new Array(5);
[B]. int [] a = {23,22,21,20,19};
[C]. int a [] = new int[5];
[D]. int [5] array;

Answer: Option B

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.


Shiwam said: (Sep 6, 2011)  
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: (Oct 12, 2011)  
int a [] = new int[5]; why is t wrong?

Amrita said: (Nov 20, 2011)  
Exactly.

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

Can anyone please ans ?

Nuzhat said: (Dec 13, 2011)  
int a[]=new int[5] is a declaration of array with 5 elements not initialization.

Narendra Mahankale said: (Dec 28, 2011)  
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: (Mar 5, 2012)  
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: (Aug 19, 2013)  
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: (Oct 15, 2013)  
Option B only declares & initializes the array but option C doesn't.

Xyz said: (Nov 7, 2013)  
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: (Dec 3, 2013)  
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.

Vinit Katti said: (Jan 17, 2014)  
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.

Amit said: (Mar 22, 2014)  
How can you say that option C wrong please anybody explain me?

Arun said: (Apr 6, 2014)  
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.

Jyoti said: (May 8, 2014)  
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.

Amit said: (Aug 7, 2014)  
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**/

Harsh said: (Jan 13, 2015)  
Awesome. Option C describes only array declaration not initialization.

Karthick said: (Feb 23, 2016)  
Can anybody tell me, what is the difference between instantiate and initialize?

Cutie said: (Mar 17, 2016)  
Difference b/w instantiate and initialize ?

Anyone ?

Shankpossible said: (Apr 3, 2016)  
In short option B and C both are correct.

Option C would have become incorrect if we were talking about the local variables.

Sagar Singh said: (Dec 20, 2016)  
Instantiate is to create an instant of any class or create an instance of the class.
eg : Class1 obj = new Class1(); // here we instantiate the the class by creating its object.

Initialize means to involve value in any data member or integer.
eg : Array1[] new = {1,2,3,4,5}; // here we intialieze the array with 5 new integers.

Saki said: (Jun 26, 2017)  
Why option C is false? I don't know this. It's not true? Please explain.

Jagdish Singh said: (Jul 25, 2017)  
In c option, we declare an array with size 5 it means we can store 5 elements in this but according to the question, we need to initialize the value at the time of array declaration.

Ravi said: (Jan 28, 2018)  
My point of view option :C also correct because by default it can initialize with 5 numbers that is all five numbers are 0s.

Sudheer said: (Jun 11, 2018)  
New operator creates object and initializes variables with default values.

As, it is initializing with default values we can consider option C also as an answer.

Vivek Singh said: (Jun 13, 2020)  
@All.

C is incorrect.

Declare an array and initialize it with five numbers.

declare an array and it should be initialized with 5 numbers..
Here we just saying size 5 i.e int a [] = new int[5];
but we haven't initialized or assign a value.
Hence, option C is wrong.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.