1). Where is the error in this code sequence? double [] a = new double[4]; for (
ID: 3714229 • Letter: 1
Question
1). Where is the error in this code sequence?
double [] a = new double[4];
for ( int i = 0; i <= a.length; i++ )
{
System.out.println( a[i] );
} _____________________________
2). Where is the error in this code that attempts to output all the values in an array?
int [] a = { 3, 26, 48, 5 };
System.out.println( a );
_____________________________
3). Where is the error in this code sequence?
int [] a = new int[3];
a[0] = 12;
a[1] = 24;
a[2] = 23.5; _______________________________
Explanation / Answer
1.
While iterating , we are iterating till the index a.length, which will give ArrayIndexOutOfBoundsException
2.
printing array object will not print all the elements. We should use Arrays.toString(a) to print contents of the array
3.
Here array is of int type. But, we are assigning a float value to a[2] which will throw error
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.