0

[SOLVED] Java MCQ Question

Indicate the error message which displays, if the following statement is executed : int a[5] = {28,32,45,68,12}; Insufficient cells Array index out of bounce Elements exceeding cells None I could not understand why the answer is None. Anyone please explain this.

20th Oct 2020, 4:17 AM
Prince Gupta
Prince Gupta - avatar
8 Réponses
+ 4
I got 3 error messages testing that line in Playground, but none of them were listed above, more like saying syntax error. The line runs just fine if I edit it like this int[] a = {23, 32, 45, 68, 12};
20th Oct 2020, 4:35 AM
Ipang
+ 3
In Java the size of an array is set via the constructor (to the right of the assignment operator =). Giving both a type and an index to the identifier would be a syntax type error. "Insufficient cells" is not an error/exception type in java Neither is "Elements exceeding cells" This leaves only "Array index out of bounds" and "None". For "Array index out of bounds", the array must already by declared and initialized (must have a size, which is set via the constructor). In other words if this line of code is both intended to declare and initialize the int array, then the error cannot occur here. This leaves "None" as the only remaining (yet incorrect IMO) option. More info on initializing arrays in java: https://www.google.com/amp/s/www.geeksforgeeks.org/arrays-in-java/amp/
20th Oct 2020, 5:09 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
The question itself contains int a[5]. I could not understand why the answer is None.
20th Oct 2020, 4:39 AM
Prince Gupta
Prince Gupta - avatar
+ 1
I mean that it's not what I would consider to be a good answer, because it's not really 100% true, but out of the options is the most correct and what I believe is the intended answer. A better correct answer would state that it would result in a compile time error.
20th Oct 2020, 8:53 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Understood. Thank You
20th Oct 2020, 2:31 PM
Prince Gupta
Prince Gupta - avatar
0
In Java you cannot declare an array like this int a[5]. This will give you a compile time error. If you want to initialize the array at compile time then you must say int a[] = {28,32,45,68,12}; or if you want to initialize it at run time then you must declare it as int a[] = new int[5];
20th Oct 2020, 4:58 AM
Avinesh
Avinesh - avatar
0
Please read the question first.
20th Oct 2020, 4:59 AM
Prince Gupta
Prince Gupta - avatar
0
I understood but I cannot figure why are you saying None is an incorrect option.
20th Oct 2020, 8:43 AM
Prince Gupta
Prince Gupta - avatar