Chapter 9 - Identifying Errors Flashcards
- Where is the error in this code sequence?
double [ ][ ] a = { 3.3, 26.0, 48.4 };
A list representing a single dimensional array cannot be assigned to a twodimensional array
- Where is the error in this code sequence?
int [ ][ ] a = { { 3, 26, 4 }, { 14, 87 } };
System.out.println( a[1][2] );
Index [1][2] is out of bounds; row 1 has only 2 elements.
- Where is the error in this code sequence?
double [ ][ ] a = new double [ ][10];
The row size is missing in new double [][10]
Example of correct code is: double [][] a = new double [4][10];
- Where is the error in this code sequence?
int [ ][ ] a = { { 1, 2 },
{ 10.1, 10.2 } };
Need ints in array initialization list, found doubles (error because of possible loss
of precision)
- Where is the error in this code sequence? (This code compiles and runs, but does not output the array values.)
int [ ][ ] a = { { 3, 26, 48 }, { 5, 2, 9 } };
System.out.println( “The array elements are “ + a );
The array variable ‘a’ holds a memory address, not data. We need to output the
array elements one at a time
- Where is the error in this code sequence?
ArrayList al;
Cannot declare an ArrayList of a primitive data type; the type needs to be a class.
The correct code is ArrayList a1;
- Where is the error in this code sequence?
ArrayList a1 = new ArrayList( );
Syntax error. Should be new ArrayList( )
- Where is the error in this code sequence? (The compiler may ask you to recompile.)
ArrayList a; a = new ArrayList( );
Incompatible types. Need to change Float to Double.
- Where is the error in this code sequence?
// a is an ArrayList of Strings // a has already been declared and instantiated a.size( ) = 10;
Correct syntax is variable = expression; Because a.size( ) is not a variable,
we cannot assign a value to it