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

On arrays and nested loops 1. What happens in Java if you try to access an eleme

ID: 3830308 • Letter: O

Question

On arrays and nested loops

1. What happens in Java if you try to access an element that is outside the dimensions of the array?

2. Use the following declarations . You may declare any other variables that you need

      final int NUM_STUDENTS = 100;

      private boolean [ ] failing = new boolean[ NUM_STUDENTS] ;

      private boolean [ ]   passing = new boolean [ NUM_STUDENTS];

      private int grade;

      private int [ ] score = new int [NUM_STUDENTS];

  
Assume that all the test grades have been read into score.

   a.    Write a void method that sets the components of failing to true when the corresponding value in the score array is less than 60.

b. Write a void method that sets the components of passing to true when the corresponding value in the score array is greater than or equal to 60.

c. Write a value-returning method passTally that reports how many components in the array passing are true.

3. What is the output?

a)    for ( i = 3; i < 5; i ++)

          for ( j = 4; j < 6; j++)

                  System.out.println ( i + “   “   + j);

b)     sum = 0;

        for ( i = 3; i < 5; i ++)

            for ( j = 4; j < 6; j++)

                   sum = sum + i * j;

         System.out.println ( “sum of the products is “ + sum);

c)        sum = 0;

        for ( i = 3; i < 5; i ++)

          {

              for ( j = 4; j < 6; j++)

                   sum = sum + i * j;

              System.out.println ( “sum of the products is “ + sum);

           }

Explanation / Answer

1. If we try to access an element beyond the dimensions of an array in Java, an ArrayIndexOutOfBoundsException is thrown. It is a RuntimeException and the compiler doesnot check for it.

2. a. Method that sets failing:

b. Method that sets passing:

c. Method that returns pass tally:

3. a. Output:

3 4 3 5 4 4 4 5

b. Output: sum of the products is 63

c. Output: sum of the products is 27 sum of the products is 63

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