Skip to main content

Intermediate Java Questions

1. Reverse a String

public String reverse(String input) {
return new StringBuilder(input).reverse().toString();
}

2. Check if a number is prime

public boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}

3. Fibonacci series (using recursion)

public int fib(int n) {
if (n <= 1) return n;
return fib(n-1) + fib(n-2);
}

4. Factorial using recursion

public int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}

5. Check palindrome string

public boolean isPalindrome(String str) {
return str.equals(new StringBuilder(str).reverse().toString());
}

6. Find duplicate characters in a string

public Set<Character> findDuplicates(String str) {
Set<Character> chars = new HashSet<>();
Set<Character> duplicates = new HashSet<>();
for (char c : str.toCharArray()) {
if (!chars.add(c)) duplicates.add(c);
}
return duplicates;
}

7. Find missing number in an array

public int findMissing(int[] nums, int n) {
int total = n * (n + 1) / 2;
for (int num : nums) total -= num;
return total;
}

8. Swap two numbers without temp

public void swap(int a, int b) {
a = a + b;
b = a - b;
a = a - b;
}

9. Check Armstrong number

public boolean isArmstrong(int n) {
int sum = 0, temp = n;
while (n > 0) {
int digit = n % 10;
sum += digit * digit * digit;
n /= 10;
}
return temp == sum;
}

10. Count vowels in a string

public int countVowels(String str) {
return (int) str.toLowerCase().chars()
.filter(c -> "aeiou".indexOf(c) != -1)
.count();
}

11. Check Anagram

public boolean isAnagram(String s1, String s2) {
char[] a = s1.toCharArray(), b = s2.toCharArray();
Arrays.sort(a); Arrays.sort(b);
return Arrays.equals(a, b);
}

12. Find 2nd largest number in array

public int secondLargest(int[] nums) {
int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
for (int num : nums) {
if (num > first) {
second = first;
first = num;
} else if (num > second && num != first) {
second = num;
}
}
return second;
}

13. Remove duplicates from array

public int[] removeDuplicates(int[] arr) {
return Arrays.stream(arr).distinct().toArray();
}

14. Find all pairs with given sum

public List<int[]> findPairs(int[] arr, int sum) {
Set<Integer> set = new HashSet<>();
List<int[]> result = new ArrayList<>();
for (int num : arr) {
if (set.contains(sum - num)) {
result.add(new int[]{num, sum - num});
}
set.add(num);
}
return result;
}

15. Reverse a LinkedList

public ListNode reverse(ListNode head) {
ListNode prev = null, curr = head;
while (curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}

16. Detect loop in LinkedList (Floyd’s Cycle)

public boolean hasCycle(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) return true;
}
return false;
}

17. Merge two sorted arrays

public int[] mergeSorted(int[] a, int[] b) {
int[] res = new int[a.length + b.length];
int i=0, j=0, k=0;
while (i < a.length && j < b.length) {
res[k++] = (a[i] < b[j]) ? a[i++] : b[j++];
}
while (i < a.length) res[k++] = a[i++];
while (j < b.length) res[k++] = b[j++];
return res;
}

18. Find GCD of two numbers

public int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}

19. Check if string is numeric

public boolean isNumeric(String str) {
return str.matches("-?\\d+(\\.\\d+)?");
}

20. Find frequency of characters

public Map<Character, Integer> freq(String s) {
Map<Character, Integer> map = new HashMap<>();
for (char c : s.toCharArray())
map.put(c, map.getOrDefault(c, 0) + 1);
return map;
}

21. Java 8: Filter even numbers

List<Integer> evens = list.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());

22. Sort Map by values

map.entrySet().stream()
.sorted(Map.Entry.comparingByValue())
.forEach(System.out::println);

23. Sum of elements in array using streams

int sum = Arrays.stream(arr).sum();

24. Java Singleton class

public class Singleton {
private static final Singleton instance = new Singleton();
private Singleton() {}
public static Singleton getInstance() {
return instance;
}
}

25. Interface default method

interface Demo {
default void show() {
System.out.println("Default method");
}
}

26. Convert List to Map

Map<Integer, String> map = list.stream()
.collect(Collectors.toMap(e -> e.id, e -> e.name));

27. Optional Usage

Optional<String> opt = Optional.ofNullable(value);
opt.ifPresent(System.out::println);

28. Check leap year

public boolean isLeap(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

29. Convert int to binary

public String toBinary(int n) {
return Integer.toBinaryString(n);
}

30. Count words in a string

public int wordCount(String str) {
return str.trim().split("\\s+").length;
}