Beginner’s friendly top 5 array questions in java

Here we will discuss top 5 beginner’s friendly questions and answers in java. Most popular problems on arrays for off campus recruitments.

5 Array Problems for Beginners

Question 1:

Write a java program to find sum of all elements in a array.

Example:

input:

array1={1 2 3 4 5}

output:

15

explanation:

1+2+3+4+5= 15 (sum of all array elements )

Java Code

package arrayproblems;
import java.lang.reflect.Array;
import java.util.*;

class problem2 {

	public static void main(String[] args) {
		//sum of all array elements 
		
		Scanner sc=new Scanner(System.in);
		
		System.out.println("Enter the length of array N");
		
		int N=sc.nextInt();
		
		int array1[]= new int[N];
		
		for(int i=0;i<N;i++) {
			
			array1[i]=sc.nextInt();
		}
		
		int sum=0;
		for(int i=0;i<array1.length;i++) {
			
			sum=sum+array1[i];
			
		}
		
		System.out.println("The sum of array elements is : " +sum);
		
		
		
		

	}

}

Question 2:

Write a java program to find sum of even elements in a given array.

Example:

input:

array1=[1 2 3 4 5 6]

output:

12

Explanation:

2+4+6= 12 that is sum of all even elements

Java Code

package arrayproblems;
import java.lang.reflect.Array;
import java.util.*;

class problem3 {

	public static void main(String[] args) {
		// sum of even elements in array 
		Scanner sc=new Scanner(System.in);
		
		System.out.println("Enter the length of array N");
		
		int N=sc.nextInt();
		
		int array1[]= new int[N];
		
		for(int i=0;i<N;i++) {
			
			array1[i]=sc.nextInt();
		}
		
		int evensum=0;
		
		for(int i=0;i<array1.length;i++) {
			
			if(array1[i]%2==0) {
				
				evensum=evensum+array1[i]; 
			}
		}
		System.out.println("Sum of all even elements is : " + evensum);
		
		
		
	}

}

Question 3:

Write a program to find sum of even indexed elements in array.

Example:

input:

array1={1 2 3 4 5 6 7}

output:

16

explanation:

Here we want to find the sum of all even index elements, as index number is starting from zero in arrays, the first even element is 1 (zero is considered as even here), after that next element is at index position 2 which is 3 , next one is 5, next one is 7

so when we add them together we get 1+3+5+7= 16

Java Code

package arrayproblems;
import java.lang.reflect.Array;
import java.util.*;

class problem4 {

	public static void main(String[] args) {
		// sum of all even indexed elements in array 
		
		Scanner sc=new Scanner(System.in);
		
		System.out.println("Enter the length of array N");
		
		int N=sc.nextInt();
		
		int array1[]= new int[N];
		
		for(int i=0;i<N;i++) {
			
			array1[i]=sc.nextInt();
		}
		int sum=0;
		
		for(int i=0;i<array1.length;i++) {
			
			if(i%2!=0) {
				 sum=sum+array1[i];
				
			}
		}
		
		System.out.println("The sum of all even indexed array elements is : " + sum);
		
		
		
		
		
		
		
		

	}

}

Question 4:

Write a java program to find difference between even indexed elements and odd elements in array.

Example:

input:

array1={1 2 3 4 5 6 7 8}

output:

zero

explanation:

sum of even indexed elements : 1+3+5+7= 16

sum of odd elements : 1+3+5+7 = 16

Difference is : 16-16 =0 (Zero)

Java Code

package arrayproblems;
import java.lang.reflect.Array;
import java.util.*;

class problem5 {

	public static void main(String[] args) {
		// difference of even index and odd elements in array 
		
		Scanner sc=new Scanner(System.in);
		
		System.out.println("Enter the length of array N");
		
		int N=sc.nextInt();
		
		int array1[]= new int[N];
		
		for(int i=0;i<N;i++) {
			
			array1[i]=sc.nextInt();
		}
                // to find even indexed elements sum 

		int evensum=0;
		
		for(int i=0;i<array1.length;i++) {
			
			if(i%2==0) {
				
				evensum=evensum+array1[i];
			}
		}
		
                // sum of odd elements in array 

		int oddsum=0;
		
		for(int i=0;i<array1.length;i++) {
			
			if(array1[i]%2!=0) {
				
				oddsum=oddsum+array1[i];
			}
		}
		
		int z=0;
		if(evensum>oddsum) {
			
			int res = evensum-oddsum;
			z=res;
			
		}
		else {
			int res=oddsum-evensum;
			z=res;
		}
		
		System.out.println("The difference between even index elements and odd elements is : " + z);
		
		
			

	}

}

Question 5:

Write a java program to copy array elements from one array to new array.

example:

input:

array1={1 2 3 4 5 6}

output:

array2={1 2 3 4 5 6}

Explanation:

Here we just want to copy all element’s from rray1 to array2

java code

package arrayproblems;
import java.lang.reflect.Array;
import java.util.*;

class problem6 {

	public static void main(String[] args) {
		
		// copy onr string to new string 
		
		Scanner sc=new Scanner(System.in);
		
		System.out.println("Enter the length of array N");
		
		int N=sc.nextInt();
		
		int array1[]= new int[N];
		
		for(int i=0;i<N;i++) {
			
			array1[i]=sc.nextInt();
		}
		
		int array2[]=new int[N];
		
		for(int i=0;i<N;i++) {
			
			array2[i]=array1[i];
		}
		
		for(int i:array2) {
			System.out.println(i);
		}
		
			

	}

}

Off Campus Coding Questions Advanced Level

L&T off campus coding question solved

TCS Off Campus Coding Questions

Wipro Elite Coding Question Solved

TATA ELXSI off campus coding question

1 thought on “Beginner’s friendly top 5 array questions in java”

  1. Pingback: Maximum Work Hacker Earth Problem solved - Is It Actually

Leave a Comment

Your email address will not be published. Required fields are marked *