Pages

Monday, 15 November 2021

Top 21 Java Coding Interview Question and Answers for QA - SDET - Testers - SDE ?

 



 Pre-requisite: How to install JDK and set Java Home path in Windows and MacOs ?

Ans: Click here for Steps

Java Interview question Set-1:
SET-01 1-10

Java Interview question Set-2:

SET-02 10-20

Java Interview question Set-3:

SET-03 21-30

11. How to reverse the first half of Array?

public class revfirsthalfIntegerArray {

    public static void main(String[] args) {

        int[] arr= {10,2,5,4,6,9,5,3};


        int len=arr.length;


        for(int i=0; i<len/4;i++)

        {

            int t = arr[i]; 

            arr[i] = arr[len/2 - i - 1]; 

            arr[len/2 - i - 1] = t; 

        }


        for(int j=0; j<len; j++)

        {

            System.out.println(arr[j]);

        }

    }

}


12. What is ModeOfArray and write the code for it?


public static void modeofarray() {


int arr[] = {1,2,3,3,4,5};

int maxnumber=-1;

int maxapperance=-1;

int result=0;


for (int i=0;i<arr.length;i++) {

int count=0;

for (int j=0;j<arr.length;j++)

                             {

if (arr[i]==arr[j]) {

count++;

}

}

if (count > maxapperance) {

maxnumber=arr[i];

maxapperance=count;

}

}

System.out.println("+" +maxnumber);

}

==> Click=Here> Pipeline Script Fundamentals

==> Click Here for Installation=> How to install Jenkins with steps?
==> Click Here :   DevOps Related Posts


13.  How to find the maximum and minimum in an ARRAY?


Ans: Click Here For Answer




14. How to find the sum of all the digits?


public static int sumofdigits(int n) {

if (n==0) {

return 0;

}

return n%10+sumofdigits(n/10);

}


15. What is median in array, define the logic and write the code?



To calculate the median first we need to sort the list in ascending or descending order. If the number of elements are even, then the median will the average of two numbers in the middle. But the number is odd then the middle element of the array after sorting will be considered as the median










 public static void medianofarray() {


int arr[]= {1,2,3,4}; //array is already sorted so no need to sort again

int arr3[]= {1,2,3,4};


if (arr.length % 2 != 0)  //if number of elements are odd

                {

System.out.println("median of array is: " + arr[arr.length/2]);


}else {


System.out.println("median of array is: " + (arr[(arr.length/2)]+arr[(arr.length/2-1)])/2.0);


}

}


16. How to merge two array in Java?


public static void mergetwoarray() {

int arr1[]= {1,5,2,4};

int arr2[]= {5,3,8,7,9};


//sort both arrays

Arrays.sort(arr1);

Arrays.sort(arr2);

int resultarray[] = new int[arr1.length+arr2.length];


int i = 0,j = 0,k=0;


while(i<arr1.length && j<arr2.length) {

if (arr1[i]<arr2[j]) {

resultarray[k++]=arr1[i++];

}

else{

resultarray[k++]=arr2[j++];

}

}

while(i<arr1.length) {

resultarray[k++]=arr1[i++];

}

while(j<arr2.length) {

resultarray[k++]=arr2[j++];

}

for (int l=0;l<resultarray.length;l++) {

System.out.println(resultarray[l]);

}

}

17.  How to find duplicates element in an array?


Click Here For Answer


18. How to find the leader in an Array?


An element is leader if it is greater than all the elements to its right side.


public static void leaderinarray() {


int arrl[]= {1,2,4,3,8,6,7,1};


for (int i=0;i<arrl.length;i++) {

int j;

for ( j=i+1;j<arrl.length;j++) {


if (arrl[i]<arrl[j]) 

                                {

break;

}

}

if (j==arrl.length) {


System.out.println(arrl[i]);

}

}

}

  •    How to convert List to array and array to list?

          Click here for Answer

  •   How to remove a SubList from a List in Java ?

    AnsList.subList(int fromIndex, int toIndex).clear()

  • How to do Sorting of An Array?

    Click Here For Answer 

**********************

Check Selenium Interview Questions from 1 to 10 below:


Check Selenium-SDET-UIAutomation Questions set 11 to 20:
Qus Set - 11 to 20

Check Selenium-SDET-UIAutomation Questions set 21 to 30:
**********************

19.  How to Add/Join/Combine two list?


Click Here For Answers


20.  TASK: Let you have an input as "aabbBCccD" then the output should be "a2b3c3d1"

Try to find the logic and share in comments.

21. Revision :Find the second largest in an ARRAY?


public class firstsecodlargest {

public static void main(String[] args) {

int[] arr = {1,2,3,4,5,6};

int firstlargest=0;

int secondlargest=0;

int i=0;

int narr.length;

for (i=0;i<n;i++) 

{

if (arr[i]>firstlargest){

secondlargest=firstlargest;

firstlargest=arr[i];

}

}

System.out.println(firstlargest + "  " + secondlargest);


}}


Coding Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Coding%20Questions

**********

Java Interview question Set-1:
SET-01 1-10

Java Interview question Set-2:

SET-02 10-20

Java Interview question Set-3:

SET-03 21-30


*************************************************

API Testing Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Rest-API

 
Kubernetes Interview Question Set
https://automationreinvented.blogspot.com/search/label/Kubernetes

 
Docker Interview Question Set
https://automationreinvented.blogspot.com/2020/02/top-18-docker-commands-for-aytomation.html

 
Linux Interview question Set
https://automationreinvented.blogspot.com/search/label/Linux

 
Automation Testing/SDET Framework Design
https://automationreinvented.blogspot.com/search/label/FrameworkDesign


Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java


GIT Interview Question Set:
https://automationreinvented.blogspot.com/search/label/GIT


Coding Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Coding%20Questions

No comments:

Post a Comment