Question

JAVA: Suppose you have the following array declaration and initialization:      int [ ] values;     ...

JAVA:

Suppose you have the following array declaration and initialization:

     int [ ] values;

     values = new int[18];

Suppose there is a static method called sumAll that is sent an integer array. The sumAll method computes and returns the sum of all the integers in the array that it is sent (assuming the array is filled). Choose the best example of calling this method:

Group of answer choices

a) sumAll( values[0], values.length )

b) sumAll( values[0 .. 17] )

c) sumAll( values[18] )

d) sumAll( values )

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

Dear Student,

The option D is correct.

d) sumAll( values )

during method calling we just pass the array name as an argument.

below i have given an example.

==================================================================

//This is the public class
public class Test
{
    // method for sum of elements in an array
    public static int sumAll(int values[])
    {
        int sum = 0; // initialize sum
        int i;
      
        // Iterate through all elements and add them to sum
        for (i = 0; i < values.length; i++)
            sum += values[i];   
        return sum;
    }
   

    // This is the main method
    public static void main(String[] args)
    {
        int[] values = new int[] {1,2,3,4};
    
     System.out.println("Sum of given array is " + sumAll(values));//this bold highlighted is the method calling
        }
}


==================================================================

Sample Output:

Sum of given array is 10

==================================================================

Kindly Check and Verify Thanks..!!!

Add a comment
Know the answer?
Add Answer to:
JAVA: Suppose you have the following array declaration and initialization:      int [ ] values;     ...
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
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
ADVERTISEMENT