DUET PG Score Card 2021
NHAI Recruitment 2021
Bihar Board 12th Dummy Admit Card 2022
RBI Assistant Notification 2021
Jee Advanced 2021 Result
NIACL Assistant Recruitment 2021
SBI PO Online Form 2021

Write a class SumOfElements with a public method sum that takes one parameter arr of type int[] and returns the sum of all elements in arr. The return type of sum should be long.

Write a class SumOfElements with a public method sum that takes one parameter arr of type int[] and returns the sum of all elements in arr. The return type of sum should be long.


  1. arr is never null
  2. return 0 in case arr is empty

Here is an example:

Cmd Args : 3 5 3 2 0
Sum of all elements in the given array is : 13

Note how the return type of the function is long and not int. The reason for this is to overcome errors due to data overflow while adding multiple int values.

Note: Please don’t change the package name.


package q11046;
public class SumOfElements {
    /** * Computes the sum of all the elements in the given array *
     * @return the sum */
	public long sum(int[] arr) {
	    // Write the code
	    long sum=0;
	    for(int i = 0; i<arr.length; i++){ 
	        sum += arr[i];
	    } return sum;

Candidates having any kind of query so he/she can ask in our comment section. Our Panel will b reverting back as soon as possible. Thank You. For more details visit our website (https://speedjobalert.com/)

Leave a Comment