Chapter 9 - Fill In The Code Flashcards
For Questions 33 to 37, consider the following statement:
String [ ][ ] geo = { { “MD”, “NY”, “NJ”, “MA”, “ME”, “CA”, “MI”, “OR” },{ “Detroit”, “Newark”, “Boston”, “Seattle” } };
- This code prints the element at row index 1 and column index 2 of the two-dimensional array geo.
// your code goes here
System.out.println( geo[1][2] );
For Questions 33 to 37, consider the following statement:
String [ ][ ] geo = { { “MD”, “NY”, “NJ”, “MA”, “ME”, “CA”, “MI”, “OR” },{ “Detroit”, “Newark”, “Boston”, “Seattle” } };
- This code prints the element of the array geo whose value is “CA.”
// your code goes here
System.out.println( geo[0][5] );
For Questions 33 to 37, consider the following statement:
String [ ][ ] geo = { { “MD”, “NY”, “NJ”, “MA”, “ME”, “CA”, “MI”, “OR” },{ “Detroit”, “Newark”, “Boston”, “Seattle” } };
- This code prints all the states (i.e., the first row) that start with an M in the array geo.
for ( int j = 0; j < geo[0].length; j++ ) { // your code goes here }
for ( int j = 0; j < geo[0].length; j++ ) { if ( geo[0][j].charAt( 0 ) == 'M' ) System.out.println( geo[0][j] ); }
For Questions 33 to 37, consider the following statement:
String [ ][ ] geo = { { “MD”, “NY”, “NJ”, “MA”, “ME”, “CA”, “MI”, “OR” },{ “Detroit”, “Newark”, “Boston”, “Seattle” } };
- This code prints all the cities (i.e., the second row) in the array geo.
for ( int j = 0; j < geo[1].length; j++ ) { // your code goes here }
for ( int j = 0; j < geo[1].length; j++ )
{
System.out.println( geo[1][j] );
}
For Questions 33 to 37, consider the following statement:
String [ ][ ] geo = { { “MD”, “NY”, “NJ”, “MA”, “ME”, “CA”, “MI”, “OR” },{ “Detroit”, “Newark”, “Boston”, “Seattle” } };
- This code prints all the elements of the array geo.
for ( int i = 0; i < geo.length; i++ ) { // your code goes here }
for ( int i = 0; i < geo.length; i++ ) { for ( int j = 0; j < geo[i].length; j++ ) System.out.println( geo[i][j] ); }
For Questions 38 to 41, consider the following statement:
int [ ][ ] a = { { 9, 6, 8, 10, 5 },
{ 7, 6, 8, 9, 6 },
{ 4, 8, 10, 6, 6 } };
- This code calculates and prints the sum of all the elements in the array a.
int sum = 0; for ( int i = 0; i < a.length; i++ ) { // your code goes here } System.out.println( "sum is " + sum );
int sum = 0; for ( int i = 0; i < a.length; i++ ) { for ( int j = 0; j < a[i].length; j++ ) sum += a[i][j]; } System.out.println( "The sum is " + sum );
For Questions 38 to 41, consider the following statement:
int [ ][ ] a = { { 9, 6, 8, 10, 5 },
{ 7, 6, 8, 9, 6 },
{ 4, 8, 10, 6, 6 } };
- This code counts and prints the number of times the value 8 appears in the array a.
int count = 0; for ( int i = 0; i < a.length; i++ ) { // your code goes here } System.out.println( "# of 8s in a: " + count );
int count = 0; for ( int i = 0; i < a.length; i++ ) { for ( int j = 0; j < a[i].length; j++ ) { if ( a[i][j] == 8 ) count++; } } System.out.println( "# of 8s in a: " + count );
For Questions 38 to 41, consider the following statement:
int [ ][ ] a = { { 9, 6, 8, 10, 5 },
{ 7, 6, 8, 9, 6 },
{ 4, 8, 10, 6, 6 } };
- This code counts and prints the number of times the value 6 appears in the second row (i.e., the row whose index is 1) of array a.
int count = 0;
// your code for the for loop header goes here
{
if ( a[1][j] == 6 )
count++;
}
System.out.println( “# of 6s in the 2nd row: “ + count );
int count = 0; for ( int j = 0; j < a[1].length; j++ ) { if ( a[1][j] == 6 ) count++; } System.out.println( "# of 6s in the 2nd row: " + count );
For Questions 38 to 41, consider the following statement:
int [ ][ ] a = { { 9, 6, 8, 10, 5 },
{ 7, 6, 8, 9, 6 },
{ 4, 8, 10, 6, 6 } };
- This code calculates the sum of the elements in the second column (i.e, the column with index 1) of array a.
int sum = 0; for ( int i = 0; i < a.length; i++ ) { // your code goes here
}
System.out.println( “sum is “ + sum );
int sum = 0; for ( int i = 0; i < a.length; i++ ) { if ( a[i].length >= 2 ) sum += a[i][1]; } System.out.println( "sum is " + sum );
- This method returns true if an element in an array of Strings is equal to “Java”; otherwise, it returns false.
public static boolean foo( String [ ][ ] a ) { // your code goes here }
public static boolean foo( String [][] a ) { for( int i = 0; i < a.length; i++ ) { for ( int j = 0; j < a[i].length; j++ ) { if ( a[i][j].equals( "Java" ) ) return true; } } return false; }
- This method returns the product of all the elements in an array.
public static int foo( int [ ][ ] a ) { // your code goes here }
public static int foo( int [][] a ) { int product = 1; for ( int i = 0; i < a.length; i++ ) { for ( int j = 0; j < a[i].length; j++ ) { product *= a[i][j]; } } return product; }
- This method returns true if there is at least one row in the array that has exactly five columns; otherwise, it returns false.
public static boolean foo( char [ ][ ] a ) { // your code goes here }
public static boolean foo( char [][] a ) { for ( int i = 0; i < a.length; i++ ) { if ( a[i].length == 5 ) return true; } return false; }
- This method takes an array of ints as a parameter and returns a single-dimensional array of booleans. The length of the array returned should be equal to the number of rows in the two-dimensional array parameter. The element at index i of the returned array will be true if there is a 0 in the corresponding row of the parameter array; otherwise, it will be false. Assume that every row in a has the same number of columns.
public static boolean [ ] foo( int [ ][ ] a ) { // your code goes here // every row has the same number of columns }
public static boolean [] foo( int [][] a ) { boolean [] temp = new boolean[a.length]; for ( int i = 0; i < a.length; i++ ) { for ( int j = 0; j < a[i].length; j++ ) { if ( a[i][j] == 0 ) temp[i] = true; } } return temp; }
For Questions 46 to 49, consider the following statements:
ArrayList languages = new ArrayList( );
languages. add( “SQL” );
languages. add( “Java” );
languages. add( “HTML” );
languages. add( “PHP” );
languages. add( “Perl” );
- This code prints the number of elements in languages.
// your code goes here
System.out.println( languages.size( ) );
For Questions 46 to 49, consider the following statements:
ArrayList languages = new ArrayList( );
languages. add( “SQL” );
languages. add( “Java” );
languages. add( “HTML” );
languages. add( “PHP” );
languages. add( “Perl” );
- This code retrieves the String “HTML” from languages (without deleting it) and assigns it to the String variable webLanguage.
// your code goes here
String webLanguage = languages.get( 2 );