Chapter 8 - Understanding Code Flashcards
- What is the output of this code sequence?
double [ ] a = { 12.5, 48.3, 65.0 };
System.out.println( a[1] );
48.3
- What is the output of this code sequence?
int [ ] a = new int [6];
System.out.println( a[4] )
0
- What is the output of this code sequence?
double [ ] a = { 12.5, 48.3, 65.0 };
System.out.println( a.length );
3
- What is the output of this code sequence?
int [ ] a = { 12, 48, 65 };
for ( int i = 0; i < a.length; i++ )
System.out.println( a[i] );
Output of code:
12
48
65
- What is the output of this code sequence?
int [ ] a = { 12, 48, 65 };
for ( int i = 0; i < a.length; i++ )
System.out.println( “a[” + i + “] = “ + a[i] );
Output of code:
a[0] = 12
a[1] = 48
a[2] = 65
- What is the output of this code sequence?
int s = 0;
int [ ] a = { 12, 48, 65 };
for ( int i = 0; i < a.length; i++ )
s += a[i];
System.out.println( “s = “ + s );
Output of code:
s = 125
- What is the output of this code sequence?
int [ ] a = new int[10];
for ( int i = 0; i < a.length; i++ )
a[i] = i + 10;
System.out.println( a[4] );
Output of code:
14
- What is the output of this code sequence?
double [ ] a = { 12.3, 99.6, 48.2, 65.8 };
double temp = a[0];
for ( int i = 1; i < a.length; i++ ) { if ( a[i] > temp ) temp = a[i]; } System.out.println( temp );
Output of code:
99.6
- What is the output of this code sequence?
int [ ] a = { 12, 48, 65, 23 };
int temp = a[1];
a[1] = a[3];
a[3] = temp;
for ( int i = 0; i < a.length; i++ )
System.out.print( a[i] + “ “ );
Output of code: 12 23 65 48
- What does this method do?
public int foo( int [ ] a )
{
int temp = 0;
for ( int i = 0; i < a.length; i++ ) { if ( a[i] == 5 ) temp++; } return temp; }
It counts how many elements in the argument array have the value 5
- What does this method do?
public int foo( int [ ] a ) { for ( int i = 0; i < a.length; i++ ) { if ( a[i] == 10 ) return i; } return -1; }
It returns the index of the first element in the argument array with value 10. If no
element in the argument array has value 10, it returns –1.
- What does this method do?
public boolean foo( int [ ] a ) { for ( int i = 0; i < a.length; i++ ) { if ( a[i] < 0 ) return false; } return true; }
If the argument array has at least one element that is negative, it returns false, otherwise it returns
true.
- What does this method do?
public String [ ] foo( String [ ] a )
{
String [ ] temp = new String[a.length];
for ( int i = 0; i < a.length; i++ )
{
temp[i] = a[i].toLowerCase( );
}
return temp;
}
It returns an array of Strings identical to the argument array except that the Strings
are all in lowercase.
- What does this method do?
public boolean [ ] foo( String [ ] a )
{
boolean [ ] temp = new boolean[a.length];
for ( int i = 0; i < a.length; i++ ) { if (a[i].indexOf( "@" ) != -1 ) temp[i] = true; else temp[i] = false; } return temp; }
It returns an array of booleans as follows: if an element in the argument array
contains the character @, then the corresponding element in the returned array is
set to true, otherwise it is set to false.