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

Java - Array concept question What is the output of the following code fragment:

ID: 3716143 • Letter: J

Question

Java - Array concept question

What is the output of the following code fragment:

d.    2 6 10 3 7 0

I don't get why the answer is C

7. Examine the following program fragment:

d.    8

Answer is D.

Please explain simply like you're teaching a 5 year old.

a.    2 4 6 8 10 1 3 5 7 9 b.    4 8 1 5 9 c.    2 6 10 3 7

d.    2 6 10 3 7 0

I don't get why the answer is C

7. Examine the following program fragment:

    int[] array = { 1, 4, 3, 6, 8, 2, 5};    int what = array[0];      // scan the array    for ( int index=0; index < array.length; index++ )    {       if ( array[ index ] > what )           what = array[ index ];      }    System.out.println( what );   
What does the fragment write to the monitor? a.    1 b.    5 c.    1 4 3 6 8 2 5

d.    8

Answer is D.

Please explain simply like you're teaching a 5 year old.

Explanation / Answer

What is the output of the following code fragment:

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

    for ( int index= 0 ; index < egArray.length ; index = index + 2 )

      System.out.print( egArray[ index ] + " " );

Answer:

Output will be : Option C 2 6 10 3 7

Index

0

1

2

3

4

5

6

7

8

9

egArray[index]

2

4

6

8

10

1

3

5

7

9

Reason: The loop will execute as follows

The loop start with index=0

For index 0 the program will print the value of egArray[index], i.e. egArray[0] which is 2

The line index = index + 2 will increase the value of index by 2

For next iteration the value of index = index + 2

Index = 0 + 2

Index = 2

egArray[index] i.e. egArray[2] =6 and so on

7. Examine the following program fragment:

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

int what = array[0];

// scan the array

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

{

    if ( array[ index ] > what )  

      what = array[ index ];

}

System.out.println( what );

What does the fragment write to the monitor?

Answer : 8

Reason:

Index

0

1

2

3

4

5

6

array[index]

1

4

3

6

8

2

5

The progam will print the largest value in the array.

int what = array[0];// this line of code will assume that the largest value is at 0th index of array.

what = 1

Loop starts

For index =0

                    If(array[0] > what) i.e. if(1>1) false

For index=1

                    If(array[1] > what) i.e. if(4>1) true, so now what = 4

For index=2

                    If(array[2] > what) i.e. if(3>4) false

For index=3

                    If(array[3] > what) i.e. if(6>4) true, so now what = 6

For index=4

                    If(array[4] > what) i.e. if(8>6) true, so now what = 8

For index=5

                    If(array[5] > what) i.e. if(2>8) false

For index=6

                    If(array[6] > what) i.e. if(5>8) false

After for loop what variable will store 8

Hence the output

Index

0

1

2

3

4

5

6

7

8

9

egArray[index]

2

4

6

8

10

1

3

5

7

9

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote