L21 Arrays – Problem Solving – 1

L21 Arrays Java Programming Questions and Solutions

Q1. 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.

Assumptions:

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.

Q2. Write a class ReversePrint with a public method reversePrint that takes one parameter arr of type int[] and returns the elements of arr in reverse order. The return type of ReversePrint should be int.

Assumptions:

1. arr is never null

Here is an example:

Cmd Args : 32 56 85 1
Array in reverse order is :
1
85
56
32

Q3.Write a class ElementCheck with a public method checkFirstOrLast that takes two parameters one is arr of type int[] and second one is arg of type int and returns true if the arg is first or last element in the arr else returns false. The return type of checkFirstOrLast is boolean.

Assumption:

1. arr is never null

Here is an example:

Enter no of elements in the array: 4
Enter elements in the array seperated by space:1 2 3 6
Enter the search element:6
true

Q4. write a class ElementCount with a main method which passes an arr of type int[] and an element of type int. Print number of times the element is present in the arr.

Here is an example:

int[] arr = {1, 12, 9, 3, 5, 3, 78, 4, 3, 9, 18, 56, 1, 5}
Cmd Args : 1
2

Q5. Write a class ElementCheck with a public method checkFirstOrLast that takes two parameters arr1 and arr2 are of type int[] and returns true if first or last elements of arr1 and arr2 are same. The return type of checkFirstOrLast should be boolean.

Assumptions:

1. arr1 and arr2 will never null
2. arr1 and arr2 lengths are not equal

Here is an example:

Enter no of elements in the array1:
5
Enter elements in the array1 seperated by space:
3 6 8 7 4
Enter no of elements in the array2:
3
Enter elements in the array2 seperated by space:
6 5 4
true

Q6. Write a class ElementCheck with a public method elementFinder that takes two parameters one is arr of type int[] second one is element of type int that returns true if the element present in the arr only one time.

Assumptions:

1. arr is never null

These are examples for your understanding:

Enter no of elements in the array:
5
Enter elements in the array seperated by space:
9 5 12 35 6
Enter the search element:
5
true

Enter no of elements in the array:
4
Enter elements in the array seperated by space:
1 2 2 3
Enter the search element:
2
false

Q7. Write a class ElementCheck with a public method elementFinder that takes one parameter arr of type int[] and return true if the first and last elements of the arr are same else return false. The return type of elementFinder should be boolean.

Assumptions:

1. arr is never null

Here are examples for your understanding:

Cmd Args : 33 25 12 5 33
true

Cmd Args : 1 2 3 4
false

Q8. Write a class ElementCheck with a public method elementFinder that takes one parameter arr of type int[] and returns true if the first four elements in the arr contains number 4 else returns false.

Assumptions:

arr is never null

Length of arr may be less than four

These are examples for understanding:

Cmd Args : 36 51 42 4
true

Cmd Args : 1 2
false

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

Assumptions:

arr is never null

arr may contain -ve numbers

These are examples:

Cmd Args : -35 -52 -12 -99
Sum of all positive elements in the array is : 0

Cmd Args : 36 12 -11 10
Sum of all positive elements in the array is : 58

Q10. Write a class SwapFirstAndLast with a public method swap that takes one parameter arr of type int[]. Write a code to swap the first and last elements of the array and print all the elements of the array.

For example:

Cmd Args : 1 5 6 7 8
8
5
6
7
1

Q11. Write a class SequenceCheck with a public method sequenceCheck that takes one parameter arr of type int[] and returns true if 6, 9, 12 present consecutively in the arr. The return type of sequenceCheck should be boolean.

Assumptions:

1. arr is never null
2. Elements 6, 9, 12 are appear consecutiviely

Here are examples:

Cmd Args : 62 32 6 9 12
true

Cmd Args : 99 36 6 12 56 9
false

Q12. Write a class SequenceCheck with a public method sequenceCheck that takes one parameter arr of type int[] and returns true if the elements 1,2,3 are present in the arr.The returen type of sequenceCheck should be boolean.

Assumptions:

1. arr is never null
2. The elements need not be in consecutive order

Here are examples:

Cmd Args : 1 6 3 2
true

Cmd Args : 3 6 4 7 8
false

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/)