Question

What does the following code fragment print? Explain the result? int [] a = new int[10];...

What does the following code fragment print? Explain the result?

int [] a = new int[10];

for (int i = 0; i < 10; i++)

   a[i] = 9 - i;

for (int i = 0; i < 10; i++)

   a[i] = a[a[i]];

for (int i = 0; i < 10; i++)

   System.out.println(a[i]);

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

0
1
2
3
4
4
3
2
1
0

int [] a = new int[10];     // initializes an array of 10 integers
for (int i = 0; i < 10; i++)    // loop through each element in array
    a[i] = 9 - i;       // set 9-i for all elements
// array a after first for loop is
// 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
for (int i = 0; i < 10; i++)    // loop through each element again
    a[i] = a[a[i]]; // set a[i] to a[a[i]]
// a[0] => a[a[0]] = a[9] = 0
// a[1] => a[a[1]] = a[8] = 1
// a[2] => a[a[2]] = a[7] = 2
// a[3] => a[a[3]] = a[6] = 3
// a[4] => a[a[4]] = a[5] = 4
// a[5] => a[a[5]] = a[4] = 4
// a[6] => a[a[6]] = a[3] = 3
// a[7] => a[a[7]] = a[2] = 2
// a[8] => a[a[8]] = a[1] = 1
// a[9] => a[a[9]] = a[0] = 0
for (int i = 0; i < 10; i++)
    System.out.println(a[i]);
// finally, printed array is 0 1 2 3 4 4 3 2 1 0
Add a comment
Know the answer?
Add Answer to:
What does the following code fragment print? Explain the result? int [] a = new int[10];...
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
  • What is the output of the following code fragment? int i = 1; while( i <=...

    What is the output of the following code fragment? int i = 1; while( i <= 5 ) if(i == 2 || i == 4) System.out.println(i + ":" + " is an even index) System.out.println("i: " + i); i++;

  • What is the result of the following program fragment: int[] array = { 1, 4, 3,...

    What is the result of the following program fragment: int[] array = { 1, 4, 3, 6 }; int what = 0; for ( int index=0; index < array.length; index++ ) { what = what + array[ index ] ; } System.out.println( what );

  • What is the likely results of running the following code fragment? Why? int *ptr = (int...

    What is the likely results of running the following code fragment? Why? int *ptr = (int *) 0xfeedbeef ; *ptr = 0 ; Suppose a class uses a dynamically-allocated integer array, pointed to by an int* variable named data. The current capacity of the array is stored in the integer variable capacity. The following code fragment is meant to dyamically resize the array, doubling its capacity. What is the major memory error and how would you detect it? int *tmp...

  • Given the following Java code fragment, what is output? int a, b; String c, d, e;...

    Given the following Java code fragment, what is output? int a, b; String c, d, e; String x = new String(“I LOVE”); String y = “java!”; a = x.length( ); System.out.println(“1) “ + a); b = y.length( ); System.out.println(“2) “ + b); c = y.toUpperCase( ); System.out.println(“3) “ + c); d = x.toLowerCase( ); System.out.println(“4) “ + d); e = x.concat(y); System.out.println(“5) “ + e);

  • Question 28 (1 point) ✓ Saved What does the code fragment print? double x = Math.sqrt...

    Question 28 (1 point) ✓ Saved What does the code fragment print? double x = Math.sqrt (3); double y = 4; if (x + x == y) { System.out.println(x); } else { System.out.println(y); } 4 1

  • (a)How many times does the code snippet given below display "Hello"? int x = 1; while...

    (a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) {    System.out.println ("Hello");    x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) {    sum = sum + i;    i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...

  • What does the following code fragment print? char c[20] = "ABC"; cout << sizeof(c[2]) << endl;...

    What does the following code fragment print? char c[20] = "ABC"; cout << sizeof(c[2]) << endl; 2. What does the following code fragment print? char c[20] = "ABC"; c[3] = 'x'; cout << c << endl; 3. What does the following code fragment print? char c[20] = "ABC"; c[3] = 'x'; strcat(c, "ZZ"); cout << c << endl;

  • Question 31 -Given the following code fragment, what value is contained in myArr[O]? const int ARRAYSIZE...

    Question 31 -Given the following code fragment, what value is contained in myArr[O]? const int ARRAYSIZE 3 double() myArr new double [ARRAYSIZE] myArr [0]1.1 myArr [1]2.2; myArr [2] myArr [0] myArr[1 O3.3 2.2 O 1.1 0 Question 32 - Given the following code fragment, what is displayed in IblResult? const int ARRAYSIZE 3 double(] myArr - new double [ARRAYSIZE] myArr [0] - 1.1 myArr [1] 2.2 lblResult.Text - Convert.Tostring (myArr (01) 3.3 O 2.2 1.1 0 Question 33 - Given...

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

  • Explain what each line of the following snippet of code does: int *ptr; ptr = new...

    Explain what each line of the following snippet of code does: int *ptr; ptr = new int; *ptr = 10; cout << ptr << ' ' << &ptr << ' ' << *ptr; delete ptr; cout << ptr << ' ' << &ptr << ' ' << *ptr;

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