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 3 of 3.
Saki said:
8 years ago
Why option C is false? I don't know this. It's not true? Please explain.
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)
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)
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.
As, it is initializing with default values we can consider option C also as an answer.
(1)
Vivek Singh said:
5 years ago
@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.
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.
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers