11. Find the GCD (Greatest Common Divisor) of two numbers
Required Input:
8, 12
Expected Output:
4
Code In Java
public class Main {
public static int findGCD() {
int a = 8;
int b = 12;
// Your code here
return 0;
}
}
Output
Click Run Button to view compiled output
12. Find the LCM (Least Common Multiple) of two numbers
Required Input:
4, 5
Expected Output:
20
Code In Java
public class Main {
public static int findLCM() {
int a = 4;
int b = 5;
// Your code here
return 0;
}
}
Output
Click Run Button to view compiled output
13. Check if a number is prime
Required Input:
7
Expected Output:
true
Code In Java
public class Main {
public static boolean isPrime() {
int n = 7;
// Your code here
return false;
}
}
Output
Click Run Button to view compiled output
14. Find the length of the longest word in a sentence
Required Input:
Java programming is fun
Expected Output:
11
Code In Java
public class Main {
public static int longestWordLength() {
String sentence = "Java programming is fun";
// Your code here
return 0;
}
}
Output
Click Run Button to view compiled output
15. Convert a list of strings to uppercase
Required Input:
["java", "python", "cpp"]
Expected Output:
[JAVA, PYTHON, CPP]
Code In Java
import java.util.*;
public class Main {
public static List toUpperCase() {
List lst = Arrays.asList("java", "python", "cpp");
// Your code here
return lst;
}
}
Output
Click Run Button to view compiled output
16. Remove duplicates from a list of integers
Required Input:
[1, 2, 2, 3, 3, 3, 4]
Expected Output:
[1, 2, 3, 4]
Code In Java
import java.util.*;
public class Main {
public static List removeDuplicates() {
List lst = Arrays.asList(1, 2, 2, 3, 3, 3, 4);
// Your code here
return lst;
}
}
Output
Click Run Button to view compiled output
17. Sort a list of integers in ascending order
Required Input:
[5, 3, 8, 1, 2]
Expected Output:
[1, 2, 3, 5, 8]
Code In Java
import java.util.*;
public class Main {
public static List sortList() {
List lst = Arrays.asList(5, 3, 8, 1, 2);
// Your code here
return lst;
}
}
Output
Click Run Button to view compiled output
18. Find the common elements in two lists
Required Input:
[1, 2, 3, 4], [3, 4, 5, 6]
Expected Output:
[3, 4]
Code In Java
import java.util.*;
public class Main {
public static List commonElements() {
List lst1 = Arrays.asList(1, 2, 3, 4);
List lst2 = Arrays.asList(3, 4, 5, 6);
// Your code here
return lst1;
}
}
Output
Click Run Button to view compiled output
19. Check if two strings are anagrams
Required Input:
listen, "silent"
Expected Output:
true
Code In Java
import java.util.*;
public class Main {
public static boolean areAnagrams() {
String s1 = "listen";
String s2 = "silent";
// Your code here
return false;
}
}
Output
Click Run Button to view compiled output
20. Find the intersection of two sets
Required Input:
{1, 2, 3}, {3, 4, 5}
Expected Output:
[3]
Code In Java
import java.util.*;
public class Main {
public static Set findIntersection() {
Set set1 = new HashSet<>(Arrays.asList(1, 2, 3));
Set set2 = new HashSet<>(Arrays.asList(3, 4, 5));
// Your code here
return set1;
}
}
Output
Click Run Button to view compiled output