Question

This is java. I need help with my computer science class. Question 11 Assume that you...

This is java. I need help with my computer science class.

Question 11

Assume that you have an array of integers named arr. The following program segment is intended to sum arr [0]through arr[n−1], where n = arr.length:
         sum = 0;
         i = 0;
         n = arr.length;
         while (i != n)
         {
            i++;
            sum += arr[i];
         }

In order for this segment to perform as intended, which of the following modifications, if any, should be made?

1.No modification is necessary

2. sum = 0; i = 0; should be changed to sum = arr[1]; i = 1;

3. while (i != n) should be changed to while (i <= n)

4. sum += arr[i]; should be changed to sum += arr[i+1];

5. i++; should be interchanged with sum += arr[i];

Question 13

Assume arr2 is declared as a two-dimensional array of integers. Which of the following segments of code successfully calculates the sum of all elements arr2?

  1. int sum = 0;
    for(int j = 0; j < arr2.length; j++)
    {
       for(int k = 0; k < arr2[j].length; k++)
       {
          sum += arr2[k][j];
       }
    }
  2. int sum = 0;
    for(int j = arr2.length − 1; j >= 0; j−−)
    {
       for(int k = 0; k < arr2[j].length; k++)
       {
          sum += arr2[j][k];
       }
    }
  3. int sum = 0;
    for(int[] m : arr2)
    {
       for(int n : m)
       {
          sum += n;
       }
    }

1. I only

2. II only

3. I and II only

4. II and III only

5. I and III only.

Question 18

When must the method myMethod be invoked using dot notation?

1. When myMethod is private.

2. When myMethod is invoked by a method in the same class as myMethod.

3. When myMethod is public.

4. When myMethod is both private and static.

5. When myMethod is invoked by a method in a different class than myMethod.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

11.

  • 5th option is the correct option.
  • In while loop, i is incremented first and then sum = arr[1] and then each time i is incremented and sum becomes a[1]+a[2]+a[3]...a[n-1].
  • So, if we interchange the line such that i is incremented later so, sum = arr[0] in the first iteration and then i becomes =1 and then add all the values till i=n-1.

13.

  • 4rth option is correct.
  • Ist part will fail in case if there is an uneven matrix where a number of rows and columns are equal.
  • 2nd part will take one row at a time and add each element of a column. For j=0, k=0,1,2 all the columns are added. In the next iteration, j=1, k=0,1,2, and all the column value for row 1 will be added and in the third iteration, j=2, k=0,1,2, and all the column value for row 3 will be added.
  • 3rd part will also compute sum as it will take one row at a time and then take each element of the row and add the values.

18.

  • The fifth option is correct.
  • Other classes can invoke the function written in different classes using the dot notation.
  • Option 1 is incorrect as private methods are not invoked outside the class and inside the class, we don't need dot notation.
  • Option 2 is incorrect as in the same class, we can call directly by function name.
  • Option 3 is incorrect if the method is public it can be accessed throughout.
  • Option 4 is incorrect as the private methods are not accessed outside the class and static methods are always called by the static functions only.

Friend, this was a really nice question to answer. If you find my answer helpful, please like it. Thanks.

Add a comment
Know the answer?
Add Answer to:
This is java. I need help with my computer science class. Question 11 Assume that you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.util.Arrays; public class lab {    public static void main(String args[])    {    int...

    import java.util.Arrays; public class lab {    public static void main(String args[])    {    int arr[] = {10, 7, 8, 9, 1, 5,6,7};    int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};    int arr3[] = {1, 3, 5, 3, 2, 6, 20};    quicksort(arr,0,arr.length-1);    quicksort(arr2,0,arr2.length-1);    quicksort(arr3,0,arr3.length-1);    System.out.println(Arrays.toString(arr));    System.out.println(Arrays.toString(arr2));    System.out.println(Arrays.toString(arr3));       }    private static int partition(int[] items,int low, int high)    {    int i=0;    int j=0;...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • Assume arr2 is declared as a two-dimensional array of integers. Which of the following segments of...

    Assume arr2 is declared as a two-dimensional array of integers. Which of the following segments of code successfully calculates the sum of all elements arr2? int sum = 0; for(int j = 0; j < arr2.length; j++) {    for(int k = 0; k < arr2[j].length; k++)    {       sum += arr2[k][j];    } } int sum = 0; for(int j = arr2.length − 1; j >= 0; j−−) {    for(int k = 0; k < arr2[j].length; k++)    {       sum += arr2[j][k];    }...

  • Question 9 0 Consider the following method. public static String[] strArrMethod(String] arr) String[] result = new...

    Question 9 0 Consider the following method. public static String[] strArrMethod(String] arr) String[] result = new String(arr.length]; for (int j - 0; j < arr.length; j++) String sm = arr[i]; for (int k = j + 1; k < arr.length; k++) if (arr[k].length() < sm.length) sm - arr[k]; // Line 12 result[j] = SM; return result; Consider the following code segment. String[] test Two - {"last", "day" of "the", school","year"); String[] resultTrostrar Method(test Two) How many times is the line...

  • I have a multithreaded java sorting program that works as follows: 1. A list of double...

    I have a multithreaded java sorting program that works as follows: 1. A list of double values is divided into two smaller lists of equal size 2. Two separate threads (which we will term sorting threads) sort each sublist using a sorting algorithm of your choice 3. The two sublists are then merged by a third thread merging thread that merges the two sublists into a single sorted list. SIMPLE EXECUTION >java SortParallel 1000 Sorting is done in 8.172561ms when...

  • Prove Big O in terms of nₒ and C? There are 5 examples: class Exercise {...

    Prove Big O in terms of nₒ and C? There are 5 examples: class Exercise { public static int example1(int[] arr) { int n = arr.length, total = 0; for (int j=0; j < n; j++) // loop from 0 to n-1 total += arr[j]; return total; } public static int example2(int[] arr) { int n = arr.length, total = 0; for (int j=0; j < n; j += 2) // note the increment of 2 total += arr[j]; return...

  • I'm trying to find out what is wrong with my code? package DriverClass; public class DriveClass...

    I'm trying to find out what is wrong with my code? package DriverClass; public class DriveClass { public static void main(String[] args) { int a[] = {3, 2, 5, 6, 1}; InsertionSortClass insertion = new InsertionSortClass(); System.out.print("The original list : "); System.out.println(); insertion.printArray(a); System.out.println(); System.out.println("The list after insertionSort : "); System.out.println(); insertion.insertionSort(a); } } package DriverClass; public interface SortADTInterface { public void insertionSort(int arr[]); public void printArray(int arr[]); } package DriverClass; public class InsertionSortClass implements SortADTInterface{ public void insertionSort(int[] arr)...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • 8.         R-4.8 Order the following functions by asymptotic growth rate. 4nlogn + 2n 2^10    2^logn 3n...

    8.         R-4.8 Order the following functions by asymptotic growth rate. 4nlogn + 2n 2^10    2^logn 3n + 100logn      4n     2^n n^2 + 10n        n^3       nlogn 9.         R-4.9 Give a big-Oh characterization, in terms of n, of the running time of the example 1 method shown in Code Fragment 4.12. 10.       R-4.10 Give a big-Oh characterization, in terms of n, of the running time of the example 2 method shown in Code Fragment 4.12. 11.       R-4.11 Give a big-Oh characterization, in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT