Chapter 9 - Fill In The Code Flashcards

1
Q

For Questions 33 to 37, consider the following statement:

String [ ][ ] geo = { { “MD”, “NY”, “NJ”, “MA”, “ME”, “CA”, “MI”, “OR” },{ “Detroit”, “Newark”, “Boston”, “Seattle” } };

  1. This code prints the element at row index 1 and column index 2 of the two-dimensional array geo.

// your code goes here

A

System.out.println( geo[1][2] );

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

For Questions 33 to 37, consider the following statement:

String [ ][ ] geo = { { “MD”, “NY”, “NJ”, “MA”, “ME”, “CA”, “MI”, “OR” },{ “Detroit”, “Newark”, “Boston”, “Seattle” } };

  1. This code prints the element of the array geo whose value is “CA.”

// your code goes here

A

System.out.println( geo[0][5] );

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

For Questions 33 to 37, consider the following statement:

String [ ][ ] geo = { { “MD”, “NY”, “NJ”, “MA”, “ME”, “CA”, “MI”, “OR” },{ “Detroit”, “Newark”, “Boston”, “Seattle” } };

  1. 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
}
A
for ( int j = 0; j < geo[0].length; j++ )
{
if ( geo[0][j].charAt( 0 ) == 'M' )
 System.out.println( geo[0][j] );
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

For Questions 33 to 37, consider the following statement:

String [ ][ ] geo = { { “MD”, “NY”, “NJ”, “MA”, “ME”, “CA”, “MI”, “OR” },{ “Detroit”, “Newark”, “Boston”, “Seattle” } };

  1. 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
}
A

for ( int j = 0; j < geo[1].length; j++ )
{
System.out.println( geo[1][j] );
}

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

For Questions 33 to 37, consider the following statement:

String [ ][ ] geo = { { “MD”, “NY”, “NJ”, “MA”, “ME”, “CA”, “MI”, “OR” },{ “Detroit”, “Newark”, “Boston”, “Seattle” } };

  1. This code prints all the elements of the array geo.
for ( int i = 0; i < geo.length; i++ )
{
      // your code goes here
}
A
for ( int i = 0; i < geo.length; i++ )
{
for ( int j = 0; j < geo[i].length; j++ )
System.out.println( geo[i][j] );
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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 } };

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

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 } };

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

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 } };

  1. 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 );

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

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 } };

  1. 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 );

A
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 );
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. 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
}
A
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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. This method returns the product of all the elements in an array.
public static int foo( int [ ][ ] a )
{
   // your code goes here
}
A
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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. 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
}
A
public static boolean foo( char [][] a )
{
for ( int i = 0; i < a.length; i++ )
{
 if ( a[i].length == 5 )
return true;
}
return false;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. 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
}
A
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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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” );

  1. This code prints the number of elements in languages.

// your code goes here

A

System.out.println( languages.size( ) );

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

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” );

  1. This code retrieves the String “HTML” from languages (without deleting it) and assigns it to the String variable webLanguage.

// your code goes here

A

String webLanguage = languages.get( 2 );

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

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” );

  1. This code replaces “HTML” with “C++” in languages.

// your code goes here

A

languages.set( 2, “C++” );

17
Q

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” );

  1. This code prints all the elements of languages that start with the letter P.
for ( String s : languages )
{  
  // your code goes here
}
A
for ( String s : languages )
{
if ( s.charAt( 0 ) == 'P' )
 System.out.println( s );
}