5)Consider this static method:
public static String check( String[] a, String[] b )
{
int i;
for ( i = 0 ; i < a.length && i < b.length ; i++ )
{
if ( a[ i ].equals( b[ i ] ) )
return a[ i ];
}
return "None";
}
{
int i;
for ( i = 0 ; i < a.length && i < b.length ; i++ )
{
if ( a[ i ].equals( b[ i ] ) )
return a[ i ];
}
return "None";
}
- "None", if s and t have different lengths; otherwise, the index of the first element of s that is the same as the corresponding element of t.
- "None", if s and t have different lengths; otherwise, the first element of s that is the same as the corresponding element of t.
- The first element of s that is the same as the corresponding element of t, or "None" if s has no such element.
- The last element of s that is the same as the corresponding element of t, or "None" if s has no such element.
- An error, if s and t have different lengths; otherwise, the first element of s that is the same as the corresponding element of t, or "None", if s has no such element.
now in this one I thought there would be an error if the lengths were diffrent because the if statement would make an out of bounds error if it tryed to test outside of the bounds but apparently I was wrong.
9)Consider this static method:
public static boolean checkIntArray( int[] a )
{
int i = 1;
while ( i < a.length )
{
if ( a[ i - 1 ] > a[ i ] )
return false;
i++;
}
return true;
}
{
int i = 1;
while ( i < a.length )
{
if ( a[ i - 1 ] > a[ i ] )
return false;
i++;
}
return true;
}
- a = { -1, -2, -3 }
- a = { 1, 11, 111, 2, 22, 222 }
- a = { 1, -2, 3, -4, 5 }
- a = { 5, 4, 3, 2, 1 }
- a = { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4 }
the answer I got for this was 4 when in reality it was 5 because I did not notice that the if statement was > and believed that then the numbers had to be lesser than the last to get a true when they had to be greater than or equal to.
10)The static method atLongLast takes an array a of non-empty Strings and returns a String containing just the last character of the longest String in a. (If there are more than one equally longest Strings in a, use the one with the smallest index in a. If a has no elements, return the empty String "".) For example:
10)atLongLast( { "quite", "a", "short", "array" } )
has the value "e", and
atLongLast( { "has", "one", "humongously", "long", "string" } )
has the value "y". Which of the following are suitable definitions for atLongLast?
public static String atLongLast( String[] a )
{ if ( a.length == 0 ) return ""; int maxIdx = 0; for ( int i = 1 ; i < a.length ; i++ ) { if ( a[ maxIdx ].length() < a[ i ].length() ) maxIdx = i; } return a[ maxIdx ].substring( a[ maxIdx ].length() - 1 ); } | |
public static String atLongLast( String[] a )
{ String s = ""; for ( int i = 0 ; i < a.length ; i++ ) { if ( s.length() < a[ i ].length() ) s = a[ i ]; } if ( s.equals( "" ) ) return ""; else return s.substring( s.length() - 1 ); } | |
public static String atLongLast( String[] a )
{ String s = ""; int maxIdx = 0; for ( int i = 1 ; i < a.length ; i++ ) { if ( a[ maxIdx ].length() < a[ i ].length() ) { s = a[ i ].substring( a[ i ].length() - 1 ); maxIdx = i; } } return s; } |
- I only
- II only
- I and II only
- I and III only
- I, II, and III