Chapter 9 - Reading and Understanding Code Flashcards

1
Q

For Questions 14 to 24, consider the following two-dimensional array declaration and initialization:

String [ ][ ] cities = {
{ “New York”, “LA”, “San Francisco”, “Chicago”}, { “Munich”, “Stuttgart”, “Berlin”, “Bonn” }, { “Paris”, “Ajaccio”, “Lyon” }, { “Montreal”, “Ottawa”, “Vancouver” } };

  1. How many rows are in the array cities?
A

4

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

For Questions 14 to 24, consider the following two-dimensional array declaration and initialization:

String [ ][ ] cities = {
{ “New York”, “LA”, “San Francisco”, “Chicago”}, { “Munich”, “Stuttgart”, “Berlin”, “Bonn” }, { “Paris”, “Ajaccio”, “Lyon” }, { “Montreal”, “Ottawa”, “Vancouver” } };

  1. What is the value of the expression cities[2][1]?
A

Ajaccio

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

For Questions 14 to 24, consider the following two-dimensional array declaration and initialization:

String [ ][ ] cities = {
{ “New York”, “LA”, “San Francisco”, “Chicago”}, { “Munich”, “Stuttgart”, “Berlin”, “Bonn” }, { “Paris”, “Ajaccio”, “Lyon” }, { “Montreal”, “Ottawa”, “Vancouver” } };

  1. What is the index of the last row in the array cities?
A

3

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

For Questions 14 to 24, consider the following two-dimensional array declaration and initialization:

String [ ][ ] cities = {
{ “New York”, “LA”, “San Francisco”, “Chicago”}, { “Munich”, “Stuttgart”, “Berlin”, “Bonn” }, { “Paris”, “Ajaccio”, “Lyon” }, { “Montreal”, “Ottawa”, “Vancouver” } };

  1. What are the row and column indexes of Chicago in the array cities?
A

row 0 and column 3

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

For Questions 14 to 24, consider the following two-dimensional array declaration and initialization:

String [ ][ ] cities = {
{ “New York”, “LA”, “San Francisco”, “Chicago”}, { “Munich”, “Stuttgart”, “Berlin”, “Bonn” }, { “Paris”, “Ajaccio”, “Lyon” }, { “Montreal”, “Ottawa”, “Vancouver” } };

  1. What is the output of this code sequence?
    System.out.println( cities[3][2] );
A

Vancouver

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

For Questions 14 to 24, consider the following two-dimensional array declaration and initialization:

String [ ][ ] cities = {
{ “New York”, “LA”, “San Francisco”, “Chicago”}, { “Munich”, “Stuttgart”, “Berlin”, “Bonn” }, { “Paris”, “Ajaccio”, “Lyon” }, { “Montreal”, “Ottawa”, “Vancouver” } };

  1. What is the output of this code sequence?

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

A

Munich
Stuttgart
Berlin
Bonn

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

For Questions 14 to 24, consider the following two-dimensional array declaration and initialization:

String [ ][ ] cities = {
{ “New York”, “LA”, “San Francisco”, “Chicago”}, { “Munich”, “Stuttgart”, “Berlin”, “Bonn” }, { “Paris”, “Ajaccio”, “Lyon” }, { “Montreal”, “Ottawa”, “Vancouver” } };

  1. What is the output of this code sequence?

for ( int i = 0; i < cities.length; i++ )
System.out.println( cities[i][1] );

A

LA
Stuttgart
Ajaccio
Ottawa

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

For Questions 14 to 24, consider the following two-dimensional array declaration and initialization:

String [ ][ ] cities = {
{ “New York”, “LA”, “San Francisco”, “Chicago”}, { “Munich”, “Stuttgart”, “Berlin”, “Bonn” }, { “Paris”, “Ajaccio”, “Lyon” }, { “Montreal”, “Ottawa”, “Vancouver” } };

  1. What is the output of this code sequence?

for ( int i = 0; i < cities.length; i++ )
{
for ( int j = 0; j < cities[i].length; j++ )
System.out.print( cities[i][j] + “\t” );
System.out.println( );
}

A

New York LA San Francisco Chicago
Munich Stuttgart Berlin Bonn
Paris Ajaccio Lyon
Montreal Ottawa Vancouver

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

For Questions 14 to 24, consider the following two-dimensional array declaration and initialization:

String [ ][ ] cities = {
{ “New York”, “LA”, “San Francisco”, “Chicago”}, { “Munich”, “Stuttgart”, “Berlin”, “Bonn” }, { “Paris”, “Ajaccio”, “Lyon” }, { “Montreal”, “Ottawa”, “Vancouver” } };

  1. What is the output of this code sequence?

for ( int i = 0; i < cities.length; i++ )
{
for ( int j = 0; j < cities[i].length; j++ )
{
if ( cities[i][j].length( ) == 6 )
System.out.println( cities[i][j] );
}
}

A

Munich
Berlin
Ottawa

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

For Questions 14 to 24, consider the following two-dimensional array declaration and initialization:

String [ ][ ] cities = {
{ “New York”, “LA”, “San Francisco”, “Chicago”}, { “Munich”, “Stuttgart”, “Berlin”, “Bonn” }, { “Paris”, “Ajaccio”, “Lyon” }, { “Montreal”, “Ottawa”, “Vancouver” } };

  1. What is the output of this code sequence?

int count = 0;
for ( int i = 0; i < cities.length; i++ )
{
for ( int j = 0; j < cities[i].length; j++ )
{
if ( cities[i][j].length( ) == 7 )
count++;
}
}
System.out.println( “count is “ + count );

A

count is 2

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

For Questions 14 to 24, consider the following two-dimensional array declaration and initialization:

String [ ][ ] cities = {
{ “New York”, “LA”, “San Francisco”, “Chicago”}, { “Munich”, “Stuttgart”, “Berlin”, “Bonn” }, { “Paris”, “Ajaccio”, “Lyon” }, { “Montreal”, “Ottawa”, “Vancouver” } };

  1. What is the output of this code sequence?

for ( int i = 0; i < cities.length; i++ )
{
for ( int j = 0; j < cities[i].length; j++ )
{
if ( cities[i][j].charAt( 0 ) == ‘S’ )
System.out.println( cities[i][j] );
}
}

A

San Francisco

Stuttgart

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. What does this method do?
public static int foo( double [ ][ ] a )
{
      int b = 0;
      for ( int i = 0; i < a.length; i++ )
      {
         for ( int j = 0; j < a[i].length; j++ )
                 b++;
      }
      return b;
}
A

. It counts and returns the number of elements in the parameter array a

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. What does this method do?
public static boolean foo( char [ ][ ] a )
{
   int b = a[0].length;
   for ( int i = 1; i < a.length; i++ )
   {
         if ( a[i].length != b )
            return false;
   }
   return true;
}
A

It returns true if all the rows of the argument array a have the same number of
columns; otherwise, it returns false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. What does this method do?
public static int foo( String [ ][ ] a )
{
      int b = 0;
      for ( int i = 0; i < a.length; i++ )
            b++;
      return b;
}
A

. It returns the number of rows in the argument array a

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. What does this method do?

public static int [ ] foo( float [ ][ ] a )
{
int [ ] temp = new int [a.length];
for ( int i = 0; i < a.length; i++ )
temp[i] = a[i].length;
return temp;
}

A

It returns an int array of the same length as the length of the array parameter a.
Each element of the returned array stores the number of columns of the
corresponding row in a.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  1. What does this method do?
public static int foo( ArrayList a )
{
      int b = 0;
      for ( Integer i : a )
          b++;
      return b;
}
A

It returns the number of elements in the ArrayList argument a

17
Q
  1. After the following code sequence is executed, what are the contents and index of each element of a?

ArrayList a = new ArrayList( );

a. add( 7 );
a. add( 4 );
a. add( 21 );

A

7 (at index 0) 4 (at index 1) 21 (at index 2)

18
Q
  1. After the following code sequence is executed, what are the contents and index of each element of a?

ArrayList a = new ArrayList( );

a. add( 7 );
a. add( 4 );
a. add( 21 );
a. set( 1, 45 );

A

7 (at index 0) 45 (at index 1) 21 (at index 2)

19
Q
  1. After the following code sequence is executed, what are the contents and index of each element of a?

ArrayList a = new ArrayList( );

a. add( 7 );
a. add( 4 );
a. add( 21 );
a. add( 1, 45 );

A

7 (at index 0) 45 (at index 1) 4 (at index 2) 21 (at index 3)