Java Coding For Interview

SQL INTERVIEW QUESTION PART-2

Java Coding For Interview

Below is the list of java coding for interview

For every software tester, java coding for interview is a must, find below the list of frequently asked java coding questions, it will help you to prepare for the next interview.

1. Write a program to find the sum of even and odd numbers in the given array.

For Example,

Input [1,2,3,0,4,6]

Output evenSum= 12, oddSum=1

int a[] = { 1, 2, 3, 0, 4, 6 };
System.out.println(""Even Sum :"" + Arrays.stream(a).filter(x -> x % 2 == 0).sum());
System.out.println(""Odd sum :"" + Arrays.stream(a).filter(x -> x % 2 != 0).sum());

 

2. Write a Java program to form the largest number from a given list of non-negative integers.

For Example,

Input [1,2,3,0,4,6]

Output [643210]

public class LargestExample {
public static String ln (int[] num) {
String[] array_nums = Arrays.stream(num).mapToObj(String::valueOf).toArray(String[]::new);
Arrays.sort(array_nums, (String str1, String str2) -> (str2 + str1).compareTo(str1 + str2));
return Arrays.stream(array_nums).reduce((a, b) -> a.equals(""0"") ? b : a + b).get();
}
public static void main(String[] args)
{
int[] nums = {1, 2, 3, 0, 4, 6};
System.out.printf(""\nOriginal array: ""+Arrays.toString(nums));
System.out.printf(""\nLargest number using the said array numbers: ""+ln(nums));
}
}

 

3. Return a rearranged array, so that all the even numbers come before all the odd numbers.

For Example,

Input [1, 0, 1, 0, 0, 1, 1]

Output  [0, 0, 0, 1, 1, 1, 1]

public class Example{
public static void main(String []args)
{
int a[] = {2,5,6,7,0,1,9};
int j=-1,temp;
for (int i=0; i<a.length; i++)
{
if (a[i]%2==0)
{
j++;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
System.out.println(Arrays.toString(a));
}
}

 

4. Given an array of integers, return true if the number of 1’s is greater than the number of 4’s.

For Example,

Input [1, 1, 1, 4]

Output true

public class Example {
public boolean test(int[] ar) {
int a = 0;
int b = 0;
for (int i = 0; i < ar.length; i++) {
if (ar[i]==1) {
a++;
}
else if (ar[i]==4) {
b++;
}
}
if (a>b) {
System.out.println(a);
return true;
}
else {
System.out.println(b);
return false;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Example ex = new Example();
int[] nar = {1,1,1,4};
System.out.println(ex.test(nar));
}
}

 

5. Return an array where each zero value in the array is replaced by the largest odd value to the right of the zero in the array.

For Example,

Input [0, 5, 0, 3]

Output [5, 5, 3, 3]

public static void main(String[] args)
{
// array of integers
int p[] = { 0, 5, 0, 3 };
int index = -1, oddHigh = 0;
for (int i = 0; i < p.length; i++) {
for (int j = i; j < p.length; j++) {
if (p[j] == 0 && index == -1) {
index = j;
}
if (index >= 0 && p[j] % 2 != 0) {
if (oddHigh < p[j]) {
oddHigh = p[j];
}
}
}
if (oddHigh != 0) {
p[index] = oddHigh;
}
oddHigh = 0;
index = -1;
}
for (int i = 0; i < p.length; i++)
System.out.println(p[i]);
}

 

6. Given an array length of 1 or more integers, return the difference between the largest and smallest values in the array.

Take the below examples,

Input: [10, 3, 5, 6] Output → 7
Input : [7, 2, 10, 9] Output 8

public class Example {

public static void main(String[] args) {
// TODO Auto-generated method stub
// int[] arr = {10,3,5,6};
// int[] arr = {7,2,10,9};
int[] arr = { 2, 10, 7, 2 };

Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
int diff = arr[arr.length - 1] - arr[0];
System.out.println(""The difference between largest and smallest number of array is ""+diff);
}
}

 

7. Ask the user to input the length of an array, for example, start=1 and end=5.

1: Construct an array of 4 elements i.e. 1,2,3,4
2: Return a new String[] array as stated above, except
a: for multiples of 3, use “JUMP” instead of the number 3,
b: for multiples of 5 “RUN”,
c: for multiples of both 3 and 5 use “HOP”

Examples,

Ex1: (1, 2) → [“1”]
Ex2: (1, 3) → [“1”, “2”]
Ex3: (1, 4) → [“1”, “2”, “JUMP” ]
Ex4: (1, 5) → [“1”, “2”, “JUMP”, “4” ]
Ex5: (1, 6) → [“1, “2”, “JUMP”, “4”, “RUN”]
Ex6: (1, 8) → [“1”, “2”, “JUMP”, “4”, “RUN”, “JUMP”, “7”]
Ex7: (1, 11) → [“1”, “2”, “JUMP”, “4”, “RUN”, “JUMP”, “7”, “8”, “JUMP”, “RUN”]
Ex8: (1, 16) → [“1”, “2”, “JUMP”, “4”, “RUN”, “JUMP”, “7”, “8”, “JUMP”, “RUN”, “11”, “JUMP”, “13”, “14”, “HOP”]

public class Example {
public static void main(String[] args) {
// TODO Auto-generated method stub
int start=1;
int end=16;
ArrayList arrayList = new ArrayList();
for(int i=start;i<end;i++)
{
if(i%3==0 && i%5==0)
{
arrayList.add(""HOP"");
}
else if(i%3==0)
{
arrayList.add(""JUMP"");
}
else if(i%5==0)
{
arrayList.add(""RUN"");
}
else
{
arrayList.add(String.valueOf(i));
}
}
System.out.println(arrayList);
}
}

 

8. Return an array that is left-shifted by one. You may modify and return the given array, or return a new array.

For Example,

Ex1:[6, 2, 5, 3] → [2, 5, 3, 6]
Ex2:[9, 8, 7, 6, 5] → [8, 7, 6, 5, 9]
Ex3:[1, 2] → [2, 1]
Ex4:[1] → [1]

public class Take_example {
public static void main(String[] args) {
Object[] arr = { 6, 3,null, 2, 7, 1 };
//Object[] arr = {9,8,7,null,6,5};
//Object[] arr = {1,2};
//Object [] arr= {1};
int length=arr.length;

System.out.println(Arrays.toString(arr));

Object first;
int j;
first = arr[0];
for (j = 0; j < length-1; j++) {
// Shift element of array by one
arr[j] = arr[j + 1];
}
arr[j] = first;
System.out.println(Arrays.toString(arr));
}
}

 

9. Given an array of integers, return true if the array contains either 3 even or 3 odd values.

The odd or even values must be occurring in a sequence i.e. next to each other.

Consider the below example,

[2, 1, 3, 5] → true
[2, 4, 2, 5] → true
[5, 7, 9, 1] → true
[2, 4, 2, 2] → true
[1, 4, 2, 3] → false
[2, 1, 2, 5] → false
[2, 1, 3, 4, 1, 7, 6] → false
[2, 1, 3, 4, 2, 6, 9] → true

public class Practice { 
public static void main(String[] args) {
int a[] = {1,3,9,3,5};
int isTrue = 0;

for (int i = 0; i < a.length - 2; i++) {
int even = 0;
int odd = 0;

for (int x = i; x < i + 3; x++) {
if (a[x] % 2 == 0) {
even++;
} else {
odd++;
}
if (even == 3 || odd == 3) {
isTrue = 1;
}
}
}
if (isTrue == 1) {
System.out.println("True");
} else {
System.out.println("False");
}
}
}
// Output: True

 

10. Given a number n, create and return a new integer array of length n, containing the numbers 0, 1, 2, … n-1.


The given n maybe 0, in which case just return a length 0 array.

Example,
(4) → [0, 1, 2, 3]
(1) → [0]
(10) → [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
(0) → [0]

public static int sumArray(int[] a) { 
int sum=0;
for (int i = 0; i < a.length; i++) {
if (a[i] == 13 || i > 0 && a[i - 1] == 13) {
continue;
} else {
sum=sum+a[i];
}
}
return sum;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[]a={};
int sum=sumArray(a);
System.out.println(sum);
}
}

Check Java Interview Questions

Wikipedia Java