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:
Java Interview question Set-3:
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
13. How to find the maximum and minimum in an ARRAY?
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?
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?
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?
- How to remove a SubList from a List in Java ?
Ans: List.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 21 to 30:
19. How to Add/Join/Combine two list?
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 n= arr.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:
Java Interview question Set-3:
*************************************************
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