21. Merge two sorted lists
Required Input:
[1, 3, 5], [2, 4, 6]
Expected Output:
[1, 2, 3, 4, 5, 6]
Code In Java
import java.util.*;
public class Main {
public static List mergeSortedLists() {
List lst1 = Arrays.asList(1, 3, 5);
List lst2 = Arrays.asList(2, 4, 6);
// Your code here
return lst1;
}
}
Output
Click Run Button to view compiled output
22. Find the most frequent element in a list
Required Input:
[1, 2, 3, 3, 2, 2]
Expected Output:
2
Code In Java
import java.util.*;
public class Main {
public static int mostFrequentElement() {
List lst = Arrays.asList(1, 2, 3, 3, 2, 2);
// Your code here
return 0;
}
}
Output
Click Run Button to view compiled output
23. Flatten a nested list
Required Input:
[[1, 2], [3, 4], [5, 6]]
Expected Output:
[1, 2, 3, 4, 5, 6]
Code In Java
import java.util.*;
public class Main {
public static List flattenList() {
List
- > nestedList = Arrays.asList(
Arrays.asList(1, 2),
Arrays.asList(3, 4),
Arrays.asList(5, 6)
);
// Your code here
return new ArrayList<>();
}
}
Output
Click Run Button to view compiled output
24. Calculate the sum of digits of a number
Required Input:
123
Expected Output:
6
Code In Java
public class Main {
public static int sumOfDigits() {
int n = 123;
// Your code here
return 0;
}
}
Output
Click Run Button to view compiled output
25. Find the transpose of a matrix
Required Input:
[[1, 2], [3, 4]]
Expected Output:
[[1, 3], [2, 4]]
Code In Java
import java.util.*;
public class Main {
public static int[][] transposeMatrix() {
int[][] matrix = {
{1, 2},
{3, 4}
};
// Your code here
return matrix;
}
}
Output
Click Run Button to view compiled output
26. Remove all occurrences of a specified element from a list
Required Input:
[1, 2, 3, 2, 4], 2
Expected Output:
[1, 3, 4]
Code In Java
import java.util.*;
public class Main {
public static List removeElement() {
List lst = Arrays.asList(1, 2, 3, 2, 4);
int element = 2;
// Your code here
return lst;
}
}
Output
Click Run Button to view compiled output
27. Find the second smallest number in a list
Required Input:
[4, 3, 1, 2]
Expected Output:
2
Code In Java
import java.util.*;
public class Main {
public static int secondSmallest() {
List lst = Arrays.asList(4, 3, 1, 2);
// Your code here
return 0;
}
}
Output
Click Run Button to view compiled output
28. Check if a number is a perfect square
Required Input:
25
Expected Output:
true
Code In Java
public class Main {
public static boolean isPerfectSquare() {
int n = 25;
// Your code here
return false;
}
}
Output
Click Run Button to view compiled output
29. Find the sum of even numbers in a list
Required Input:
[1, 2, 3, 4, 5]
Expected Output:
6
Code In Java
import java.util.*;
public class Main {
public static int sumOfEven() {
List lst = Arrays.asList(1, 2, 3, 4, 5);
// Your code here
return 0;
}
}
Output
Click Run Button to view compiled output
30. Rotate a list to the right by k positions
Required Input:
[1, 2, 3, 4, 5], 2
Expected Output:
[4, 5, 1, 2, 3]
Code In Java
import java.util.*;
public class Main {
public static List rotateList() {
List lst = Arrays.asList(1, 2, 3, 4, 5);
int k = 2;
// Your code here
return lst;
}
}
Output
Click Run Button to view compiled output