Question

Consider the following array: int a[] = { 1, 2, 3, 4, 5, 4, 3, 2, 1, ?); What are the contents of the array a after the following loops complete? a. for (int 1-1; 1 < 10; i++ ) { a[i] a[1-1]; } b. for (int i 9; i > 0; i--) a[i]-ali 1 c. for (int i 0; i <9; it+) a[i]ai ; H e. for (int i-1; i <10; i+) aliaiai 1; h f. for (int i = 1; i < 10; i = i + 2) { a[1] = 0; } g. for (int i 0; i <5; i+) ali 5alil; H h. for (int i = 1; i < 5; 1++) { a[i] = a[9-1]; }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answers:

a. Answer: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
b. Answer: [1, 1, 2, 3, 4, 5, 4, 3, 2, 1]
c. Answer: [2, 3, 4, 5, 4, 3, 2, 1, 0, 0]
d. Answer: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
e. Answer: [1, 3, 6, 10, 15, 19, 22, 24, 25, 25]
f. Answer: [1, 0, 3, 0, 5, 0, 3, 0, 1, 0]
g. Answer: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
h. Answer: [1, 1, 1, 1, 1, 4, 3, 2, 1, 0]

Explanation:

All the answers were obtained by executing the statement independently. i.e., each statement occurs without executing previous loops.

import java.util.Arrays;

public class Tester {

public static void main(String[] args) {

int a[] = {1,2,3,4,5,4,3,2,1,0};

/*for(int i=1; i<10; i++){

a[i] = a[i-1];

}

System.out.println("a. Answer: "+Arrays.toString(a));

for(int i=9; i>0; i--){

a[i] = a[i-1];

}

System.out.println("b. Answer: "+Arrays.toString(a));

for(int i=0; i<9; i++){

a[i]= a[i+1];

}

System.out.println("c. Answer: "+Arrays.toString(a));

for(int i=8; i>=0;i-- ){

a[i] = a[i+1];

}

System.out.println("d. Answer: "+Arrays.toString(a));

for(int i=1; i<10; i++){

a[i] = a[i]+a[i-1];

}

System.out.println("e. Answer: "+Arrays.toString(a));

for(int i=1; i<10; i=i+2){

a[i]=0;

}

System.out.println("f. Answer: "+Arrays.toString(a));

for(int i=0; i<5; i++){

a[i+5]= a[i];

}

System.out.println("g. Answer: "+Arrays.toString(a));*/

for(int i=1; i<5; i++){

a[i] = a[9-1];

}

System.out.println("h. Answer: "+Arrays.toString(a));

}

}

Add a comment
Know the answer?
Add Answer to:
Consider the following array: int a[] = { 1, 2, 3, 4, 5, 4, 3, 2,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. What is the output of the following code segment? int array[] = { 8, 6,...

    1. What is the output of the following code segment? int array[] = { 8, 6, 9, 7, 6, 4, 4, 5, 8, 10 }; System.out.println( "Index Value" ); for ( int i = 0; i < array.length; i++ ) System.out.printf( "%d %d\n", i, array[ i ] ); 2. What is the output of the following code segment? char sentence[] = {'H', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u' }; String output = "The sentence...

  • • Apply the MAX-HEAPIFY algorithm to the following array A on node i = 2 and...

    • Apply the MAX-HEAPIFY algorithm to the following array A on node i = 2 and give the resulting array. | i Ali | 1 | 2 | 3 | 4 | 5 6 | 7 | 8 | 9 | 10 81 19 76 62 54 63 66 38 43 22 Answer: 1 2 3 4 5 6 7 8 9 10 Ai

  • What does the following code output? int[] array = { 1, 4, 3, 6, 8, 2,...

    What does the following code output? int[] array = { 1, 4, 3, 6, 8, 2, 5); int sum = array[0]; Il scan the array for (int i=0; i < array.length; i++) { if ( array[i] > sum) sum = array[i]; 3 System.out.println( sum );

  • IN JAVA int[] numbers = new int [4]; numbers [0] = 1; for (int i =...

    IN JAVA int[] numbers = new int [4]; numbers [0] = 1; for (int i = 1; i < numbers.length; ++i) { numbers[i] numbers[i - 1] * 2 + 1; } Enumerate the contents of the array numbers after the above code is executed.

  • this is a while array mystery from java. please help me to write the code for...

    this is a while array mystery from java. please help me to write the code for this question to run it on eclipse !! I want the full code to run this program !! Consider the following method: public static void arrayMystery (int[] a) { for (int i = a. length - 1; i >= 1; i--) { if (a[i] > ali - 1] + 10) { ali - 1] = a[i - 1] + 5; Indicate in the right-hand...

  • Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array...

    Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array double[10]; c. charl charArray "Computer Science"; None of the above Analyze the following code: class Test public static void main(Stringl] args) System.out.println(xMethod(10); public static int xMethod(int n) System.out.println("int"); return n; public static long xMethod(long n) System.out.,println("long"); return n The program displays int followed by 10 The program displays long followed by 10. The program does not compile. None of the above. tions 3-4 are...

  • [5 marks] Using selection sort algorithm to sort the array int array[7]-5, 6, 2, 7, 9,...

    [5 marks] Using selection sort algorithm to sort the array int array[7]-5, 6, 2, 7, 9, 4, 3). Assuming we call the following function using statement selection sort (int array, 7); Please fill the table to show the changes of the array int_array after each iteration. The first column is filled. void selection_sort (int list[], int list_size) for (int i = 0; i < list size - 1; 1++) int current min = list[1]; int current_min_index-i for (int j -...

  • Consider the following matrix transpose routines: typedef int array[4][4]; void transpose (array dst, array src) {...

    Consider the following matrix transpose routines: typedef int array[4][4]; void transpose (array dst, array src) {     int i, j;     for (i=0; i<4; i++) {    for (j=0; j<4; j++) {           dst[i][j] = src[i][j];    }     } } void transpose2 (array dst, array src) {     int i, j;     for (i=0; i<4; i++) {    for (j=0; j<4; j++) {           dst[j][i] = src[j][i];    }     } } Assume this code runs on...

  • Consider the following code segment. int[]arr={1, 2, 3, 4, 5, 6, 7, 8}; for(int k=3; k<arr.length-1;...

    Consider the following code segment. int[]arr={1, 2, 3, 4, 5, 6, 7, 8}; for(int k=3; k<arr.length-1; R++ arr[k]-arr[k+1]; What are the contents of arr as a result of executing the code segment? a. {1, 2, 3, 5, 6, 7, 8, 8) b. {2, 2, 4, 5, 6, 7, 8, 8} C. {2, 4, 6, 5, 6, 7, 8,8} d. {4, 2, 4, 4, 6, 7, 8, 8) e. {6, 6, 4, 5, 6, 7, 8, 8} int) arr={7, 2.5, 3.0,...

  • what is the output of the following code segment? C++ g. int arr[3][4]; for (int i...

    what is the output of the following code segment? C++ g. int arr[3][4]; for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) arr[i][j] =i*4 + j; for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) cout << arr[i][j] << " "; h. int next(int & x) { x= x + 1; return (x + 1); int main() { int y = 10;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT