Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. What is the output of the following code segment? int array[] = { 8, 6, 9, 7,

ID: 3789574 • Letter: 1

Question

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 ", 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 is: ";

for ( int i = 0; i < sentence.length; i++ )

System.out.printf( "%c ", sentence[ i ] );

System.out.println();

. What is the output of the following code segment?

int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

for ( int i = 0; i < array.length; i++ )

{

   for ( int j = 0; j < array [ i ]; j++ )

        System.out.print( "*" );

   System.out.println();

4. What is the output of the following code segment?

int array[] = { 3, 2, 5 };

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

      array[ i ] *= 2;

for ( int j : array )

      System.out.print( "%d ", j );

System.out.println();

Explanation / Answer

1)

int array[] = { 8, 6, 9, 7, 6, 4, 4, 5, 8, 10 }; //initialize array values

System.out.println( "Index Value" ); // it will pring index value

for ( int i = 0; i < array.length; i++ ) // Iterate the the loop till array length ends

System.out.printf( "%d %d ", i, array[ i ] ); //It will print i value and array value like i=0 and array =8

OUTPUT- ----------------------------------------

Index Value
0 8
1 6
2 9
3 7
4 6
5 4
6 4
7 5
8 8
9 10

---------------------------------------------------

2 ) char sentence[] = {'H', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u' }; // initiate char array
       String output = "The sentence is: ";
         
       for ( int i = 0; i < sentence.length; i++ ) //iterate till sentence lenght
       System.out.printf( "%c ", sentence[ i ] ); // print the char value
         
       System.out.println();

OUTPUT- H o w a r e y o u

3)

int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; //initialize an array
         
       for ( int i = 0; i < array.length; i++ ) // iterate the loop till array length
       {
       for ( int j = 0; j < array [ i ]; j++ ) // iterate the j value and check condition j < array [ i ]; suppose if i=0 then j will iterate till 1 because the array[0]=1 and will print only one * for next condition if =2 then j will iterate till 2 because array[1]=2 so will print two ** and so on.
       System.out.print( "*" ); // print *
         
       System.out.println();
   }

OUTPUT ---------------------------

*
**
***
****
*****
******
*******
********
*********
**********

----------------------------------

4) int array[] = { 3, 2, 5 }; //Initialize an array
         
       for ( int i = 0; i < 3; i++ ) // iterate the loop till 3
       array[ i ] *= 2; // muliply each array value by 2 and store value 6 4 10
       for ( int j : array ) // iteate the loop and here array = 6 4 10
       System.out.printf( "%d ", j ); // will print the array values
       System.out.println();

OUTPUT

-----------------------

6 4 10

---------------------------------