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
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
What does the following code fragment print? Explain the result? int [] a = new int[10];...
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, 6 }; int what = 0; for ( int index=0; index < array.length; index++ ) { what = what + array[ index ] ; } System.out.println( what );
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 (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 {...
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, 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 int; *ptr = 10; cout << ptr << ' ' << &ptr << ' ' << *ptr; delete ptr; cout << ptr << ' ' << &ptr << ' ' << *ptr;
EX 1) Determine the outcome of the following code fragment without writing and running the program. int num = 0, max = 20; while (num <= max) { System.out.println(num); num += 4; } EX 2) Write a code fragment that reads and prints integer values entered by user until a particular sentinel value (stored in constant SENTINEL) is entered. Do not print the sentinel value. Ex 3) Write a method called averageLargestTwo that accepts three int parameters and returns the average...
10) What is the output when the following code fragment is executed? void exam(int i) {cout « i; } void main() { int n; for (n = 3; n <= 4; n++) exam(n); } A) 1234 B) 234 C) 34 D) 4
What is the output of the following code sequence? int i = 10; i--; System.out.println(i); --i; System.out.println(i); System.out.println(++i); System.out.println(i++); System.out.println(i); System.out.println(--i); System.out.println(i--); System.out.println(i); if (i++ == 8) System.out.println("Eight"); if (++i == 9) System.out.println("Nine"); System.out.println("Final value " + i); 2. Show the change in the code above so that it also prints out Nine