Chapter 9 - Debugging Flashcards
- You coded the following on line 14 of the Test.java class:
int a[2][ ] = { { 2, 7 }, { 9, 2 } }; // line 14
When you compile, you get the following message:
Test.java:14: error: ‘]’ expected
int a[2][ ] = { { 2, 7 }, { 9, 2 } }; // line 14
^
Test.java:14: error: not a statement
int a[2][ ] = { { 2, 7 }, { 9, 2 } }; // line 14
^
Test.java:14: error: ‘;’ expected
int a[2][ ] = { { 2, 7 }, { 9, 2 } }; // line 14
^
Test.java:14: error: illegal start of expression
int a[2][ ] = { { 2, 7 }, { 9, 2 } }; // line 14
^
Test.java:14: error: not a statement
int a[2][ ] = { { 2, 7 }, { 9, 2 } }; // line 14
^
Test.java:14: error: ‘;’ expected
int a[2][ ] = { { 2, 7 }, { 9, 2 } }; // line 14
^
6 errors
Explain what the problem is and how to fix it.
You cannot use dimensions when declaring an array
Example of correct code: int a[][] = { { 2, 7 }, { 9, 2 } };
- You coded the following in the Test.java class:
int [ ][ ] a = { { 1, 2, 3, 4 },
{ 10, 20, 30 } };
for ( int i = 0; i < a.length; i++ )
{
for ( int j = 0; j < a[0].length; j++ )
{
System.out.println( a[i][j] ); // line 14
}
}
The code compiles properly but when you run, you get the following output:
1 2 3 4 10 20 30 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at Test.main(Test.java: 14) Explain what the problem is and how to fix it.
Correct code is a[i].length, not a[0].length.
Row 1 only has 3 elements, not 4
- You coded the following in the Test.java class in order to output the smallest element in the array a:
int [ ][ ] a = { { 9, 8, 7, 6 }, { 10, 20, 30, 40 } }; int min = a[0][0]; for ( int i = 1; i < a.length; i++ ) { for ( int j = 0; j < a[i].length; j++ ) { if ( a[i][j] < min ) min = a[i][j]; } } System.out.println( "The minimum is " + min );
The code compiles properly, but when you run, you get the following output:
The minimum is 9
You expected the value of min to be 6. Explain what the problem is and how to fix it.
Other than a[0][0], the first row is not taken into account because i is initialized to
1 in the outer loop. It should be int i = 0; not int i = 1;
- You coded the following in file Test.java:
int [ ][ ] a = { { 9, 8, 7, 6 },
{ 10, 20, 30, 40 } };
for ( int j = 0; j <= a[1].length; j++ )
{
if ( a[1][j] == 20 ) // line 14
{
System.out.println( “Found 20 at column index “ + j
+ “ of second row” );
}
}
The code compiles properly, but when you run, you get the following output:
Found 20 at column index 1 of second row
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 4
at Test.main(Test.java:14)
Explain what the problem is and how to fix it.
It should be j < a[1].length, not j <= a[1].length. When j is equal to
a[1].length, the index [1][j] is out of bounds.
- You coded the following in the Test.java class:
// cars is an ArrayList of Auto objects
// cars has already been declared and instantiated
for ( Auto a ; cars ) // line 12
{
System.out.println( a.toString( ) );
} // line 15
When you compile, you get the following message :
Test.java:12: error: ';' expected for ( Auto a ; cars ) // line 12 ^ 1 error Explain what the problems are and how to fix them.
Syntax error (semicolon used instead of a colon). It should be Auto a : cars, not Auto ; cars
- You coded the following in the Test.java class:
ArrayList a = new ArrayList( );
a.add( “Cloudy” );
a.add( “Snowy” );
a.add( “Cloudy” );
System.out.println( “Weather is “ + a.get( 3 ) ); // line 14
The code compiles properly, but when you run, you get the following output:
Exception in thread “main” java.lang.IndexOutOfBoundsException: Index 3 out-of-bounds for length 3
Explain what the problem is and how to fix it.
Index 3 is out of bounds. There are only 3 elements in a; the last index is 2.
- You coded the following in the file Test.java:
ArrayList a = new ArrayList( ); When you compile (using Xlint), you get the following warning message:
Test.java:10: warning: [rawtypes] found raw type: ArrayList ArrayList a = new ArrayList( ); ^ missing type arguments for generic class ArrayList where E is a type-variable: E extends Object declared in class ArrayList
Test.java:10: warning: [unchecked] unchecked conversion
ArrayList a = new ArrayList( );
^
required: ArrayList
found: ArrayList
2 warnings
Explain what the problem is and how to fix it.
The type of elements must be specified when calling the constructor. The statement should be: ArrayList a = new ArrayList( );
- You coded the following in the file Test.java:
ArrayList a = new ArrayList( ); a.add( 2.3 ); a.add( 8.4 ); a.add( 5 ); // line 11 When you compile, you get the following message:
Test.java:11: error: no suitable method found for add(int)
a.add( 5 ); // line 11
^
Explain what the problem is and how to fix it.
An int cannot be converted to a Double. Change line 11 to a.add( 5.0 );
- You coded the following in the file Test.java:
ArrayList a = new ArrayList( ); a.add( 'X' ); a.add( 'A' ); a.add( 'V' ); a.add( 'A' ); a.set( 1, 'J' ); for ( Character c : a ) System.out.print( c + " " ); The code compiles properly, but when you run, you get the following output:
X J V A
when you expected:
J A V A
Explain what the problem is and how to fix it.
Because ArrayList elements begin at index 0, the statement
a.set( 1, ‘J’ );
sets the value of the second element of the ArrayList. To set the value
of the first element, use this statement:
a.set( 0, ‘J’ );