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: 2247681 • 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();

3. 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();

Given the following declaration of a method

public int method1( int... values )

{

   int mystery = 1;

    for ( int i : values )

       mystery *= i;

    return mystery;

}

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

System.out.println( method1( 1, 2, 3, 4, 5 ) );

Given the following declaration of a method

public int method1( int values[][] )

{

    int mystery = 1;

    for ( int i[] : values )

        for ( int j : i )

            mystery *= j;

    return mystery;

}

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

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

System.out.println( mystery( array ) );

Explanation / Answer

1)Answer:

output:

Index Value

0 8

1 6

2 9

3 7

4 6

5 4

6 4

7 5

8 8

9 10

2)Answer:

output:

The sentence is: H o w a r e y o u

3)Answer:

Output:

*

**

***

****

*****

******

*******

********

*********

**********

4)Answer:

output:

6 4 10

5)Answer:

output:

120

6)Answer:

output:

14400