31.6k views
17 votes
Consider this static method:

public static int[] blend( int[] a, int[] b )

{
int[] c = new int[ a.length + b.length ];
int i = 0;
while ( i < a.length && i < b.length )
{
c[ 2 * i ] = a[ i ];
c[ 2 * i + 1 ] = b[ i ];
i++;
}
if ( a.length < b.length )
{
for ( int j = i; j < b.length; j++ )
c[ i + j ] = b[ j ];
}
else
{
for ( int j = i; j < a.length; j++ )
c[ i + j ] = a[ j ];
}
return c;
}

If a and b are initialized arrays of ints, and if the elements of both arrays are initialized, then which of the following best describes the value of blend( a, b )?

a. An array formed by inserting all the elements of b (in order) after the last element of a.
b. An array formed by inserting all the elements of a (in order) after the last element of b.
c. An array formed by alternating elements from a and b, until one of them is exhausted, followed by any remaining elements of the other array (in order).
d. If a and b have the same length, an array formed by alternating elements from a and b. Otherwise, an array made up of all the elements (in order) of the longer array that extend beyond the length of the shorter array.

User Grisel
by
7.4k points

1 Answer

2 votes

Answer:

The answer is "Option C".

Step-by-step explanation:

In this question, the choice c is correct because, in this, the choice all the alternate value of the set c array of a and b values. Except the counter is larger than the array length, its loop would exit and this will test the bigger number. Then all values from its bigger array were added to the c array.

User Smartexpert
by
6.8k points