Chapter 9 - Reading and Understanding Code Flashcards
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” } };
- How many rows are in the array cities?
4
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” } };
- What is the value of the expression cities[2][1]?
Ajaccio
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” } };
- What is the index of the last row in the array cities?
3
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” } };
- What are the row and column indexes of Chicago in the array cities?
row 0 and column 3
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” } };
- What is the output of this code sequence?
System.out.println( cities[3][2] );
Vancouver
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” } };
- What is the output of this code sequence?
for ( int j = 0; j < cities[1].length; j++ )
System.out.println( cities[1][j] );
Munich
Stuttgart
Berlin
Bonn
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” } };
- What is the output of this code sequence?
for ( int i = 0; i < cities.length; i++ )
System.out.println( cities[i][1] );
LA
Stuttgart
Ajaccio
Ottawa
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” } };
- 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( );
}
New York LA San Francisco Chicago
Munich Stuttgart Berlin Bonn
Paris Ajaccio Lyon
Montreal Ottawa Vancouver
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” } };
- 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] );
}
}
Munich
Berlin
Ottawa
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” } };
- 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 );
count is 2
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” } };
- 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] );
}
}
San Francisco
Stuttgart
- 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; }
. It counts and returns the number of elements in the parameter array a
- 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; }
It returns true if all the rows of the argument array a have the same number of
columns; otherwise, it returns false
- 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; }
. It returns the number of rows in the argument array a
- 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;
}
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.