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

Jagdish singh said:   8 years ago
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.
(1)

Sudheer said:   7 years ago
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.
(1)

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.

ShankPossible said:   9 years ago
In short option B and C both are correct.

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

Ravi said:   8 years ago
My point of view option :C also correct because by default it can initialize with 5 numbers that is all five numbers are 0s.
(1)

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

Karthick said:   9 years ago
Can anybody tell me, what is the difference between instantiate and initialize?
(1)

Amrita said:   1 decade ago
Exactly.

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

Can anyone please ans ?

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


Post your comments here:

Your comments will be displayed after verification.