Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Static method question...?

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 )? Choices are below...

An array formed by inserting all the elements of b (in order) after the last element of a.

An array formed by inserting all the elements of a (in order) after the last element of b.

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).

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.

An ArrayIndexOutOfBoundsException occurs.

2 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    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).

    I simply tested the program. The code confuses me o.0

    Sorry, good luck.]

    EDIT: Actually I re-read it, it is actually quite easy. I don't know whether the poster above said the same thing ( I'm too lazy to read so much text ) but basically:

    c[2*i]=a[i];

    c[2*i+1]=b[i];

    This sets the c array with alternate values from a and b. When the counter is larger than an array length it exits that loop and checks which one is bigger. Then it adds all the values from the larger array to the c array. Very simple.

  • 1 decade ago

    I may be wrong on this one, I'm tired and ... look it is again...

    while ( i < a.length && i < b.length )

    {

    c[ 2 * i ] = a[ i ];

    c[ 2 * i + 1 ] = b[ i ];

    i++;

    }

    I believe the exception will show because of this part. Give the arrays a fixed length then ran that through your mind.

    c[ 2 * i ]

    c[ 2 * i + 1 ]

    These parts are stating that whatever index that happens to be will be equal to what value of array a or array b at whatever index the loop is currently on. The only differences is these arrays will not be on the same indexes, because you see in array c, the indexes are multiplied and/or added with various numbers. c could be filling in index 10 while the value is from index 5 of a and b.

    So it's easy to reach out of bounds once you get close to the indexes of arrays a and b.

    Say it's 9 for a and 12 for b for example, then the length of array c is 21 from the 3rd line of code

    int[] c = new int[ a.length + b.length ];

    So we hit the while loop, which runs as long as i is less than 9 AND 12 (but really IN THIS CASE ONLY anything just less than 9 since anything bigger will disqualify the AND condition.)

    variable i will go from 0 to whatever value, let's say it's 8 (I don't now when it'd hit it an exception, just making an example) at 8, the index being filled in for c is 16 for copying values off array a, and 17 for copying off array b.

    Look at that, no exception since c can go up to 21. Besides after 8 it stops, since the condition is anything LESS THAN 9 AND 12, that includes 9.

    So I was wrong, well you've got one off the list, no?

    I will say after some thought, I MAY BE WRONG, I believe it's either c or d, or w/e the 2 before the last one is. I've given some clues as to why, the rest is on your own (WITH SOME HELP OFFLINE) or until I come back in some hours/days lol

Still have questions? Get your answers by asking now.