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.
8 Antworten
+ 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};
+ 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/
+ 1
The question itself contains int a[5].
I could not understand why the answer is None.
+ 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.
+ 1
Understood.
Thank You
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];
0
Please read the question first.
0
I understood but I cannot figure why are you saying None is an incorrect option.