Chapter 9 - Identifying Errors Flashcards

1
Q
  1. Where is the error in this code sequence?

double [ ][ ] a = { 3.3, 26.0, 48.4 };

A

A list representing a single dimensional array cannot be assigned to a twodimensional array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. Where is the error in this code sequence?

int [ ][ ] a = { { 3, 26, 4 }, { 14, 87 } };
System.out.println( a[1][2] );

A

Index [1][2] is out of bounds; row 1 has only 2 elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. Where is the error in this code sequence?

double [ ][ ] a = new double [ ][10];

A

The row size is missing in new double [][10]

Example of correct code is: double [][] a = new double [4][10];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. Where is the error in this code sequence?

int [ ][ ] a = { { 1, 2 },
{ 10.1, 10.2 } };

A

Need ints in array initialization list, found doubles (error because of possible loss
of precision)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. 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 );

A

The array variable ‘a’ holds a memory address, not data. We need to output the
array elements one at a time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. Where is the error in this code sequence?

ArrayList al;

A

Cannot declare an ArrayList of a primitive data type; the type needs to be a class.
The correct code is ArrayList a1;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. Where is the error in this code sequence?

ArrayList a1 = new ArrayList( );

A

Syntax error. Should be new ArrayList( )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. Where is the error in this code sequence? (The compiler may ask you to recompile.)
ArrayList a;
a = new ArrayList( );
A

Incompatible types. Need to change Float to Double.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. Where is the error in this code sequence?
// a is an ArrayList of Strings
// a has already been declared and instantiated
a.size( ) = 10;
A

Correct syntax is variable = expression; Because a.size( ) is not a variable,
we cannot assign a value to it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly