Java Code-Based Interview Questions with Answers
![]() |
Top Java Code-Based Interview Questions for Experienced Developers with 5 years of experience. |
1. Detect Duplicates in an Array
Question: Write a Java program to detect duplicates in an array and print the duplicates.
import java.util.HashSet;
public class DuplicateDetector {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5, 2, 4, 6};
HashSet<Integer> set = new HashSet<>();
System.out.println("Duplicate elements:");
for (int num : numbers) {
if (!set.add(num)) {
System.out.println(num);
}
}
}
}
Output:
Duplicate elements:
2
4
Explanation: The HashSet
does not allow duplicate values. Each element is checked if it can be added to the set; if not, it means it’s a duplicate and is printed.
2. String Reverse Without Using Built-In Functions
Question: Write a Java method to reverse a string without using any built-in functions.
public class StringReverse {
public static void main(String[] args) {
String str = "Java";
System.out.println("Reversed: " + reverseString(str));
}
public static String reverseString(String s) {
char[] chars = s.toCharArray();
int left = 0, right = chars.length - 1;
while (left < right) {
char temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
left++;
right--;
}
return new String(chars);
}
}
Output:
Reversed: avaJ
Explanation: This solution uses a two-pointer approach to swap characters from both ends of the string until they meet in the middle.
3. Find the Second Largest Element in an Array
Question: Write a Java program to find the second largest element in an array.
public class SecondLargestFinder {
public static void main(String[] args) {
int[] arr = {10, 20, 4, 45, 99};
System.out.println("Second largest element is: " + findSecondLargest(arr));
}
public static int findSecondLargest(int[] array) {
int largest = Integer.MIN_VALUE, secondLargest = Integer.MIN_VALUE;
for (int num : array) {
if (num > largest) {
secondLargest = largest;
largest = num;
} else if (num > secondLargest && num != largest) {
secondLargest = num;
}
}
return secondLargest;
}
}
Output:
Second largest element is: 45
Explanation: This code iterates through the array, updating the largest and second largest numbers based on conditions, ensuring secondLargest
has the second-highest value at the end.
4. FizzBuzz Program
Question: Write a program that prints numbers from 1 to 15. For multiples of 3, print "Fizz" instead of the number, and for multiples of 5, print "Buzz." For numbers that are multiples of both 3 and 5, print "FizzBuzz."
public class FizzBuzz {
public static void main(String[] args) {
for (int i = 1; i <= 15; i++) {
if (i % 3 == 0 && i % 5 == 0) {
System.out.println("FizzBuzz");
} else if (i % 3 == 0) {
System.out.println("Fizz");
} else if (i % 5 == 0) {
System.out.println("Buzz");
} else {
System.out.println(i);
}
}
}
}
Output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
Explanation: This program checks each number to see if it’s divisible by 3, 5, or both, and prints the appropriate response.
5. Count Occurrences of Each Character in a String
Question: Write a Java program to count the occurrences of each character in a given string.
import java.util.HashMap;
public class CharacterCounter {
public static void main(String[] args) {
String str = "programming";
countCharacters(str);
}
public static void countCharacters(String str) {
HashMap<Character, Integer> map = new HashMap<>();
for (char ch : str.toCharArray()) {
map.put(ch, map.getOrDefault(ch, 0) + 1);
}
System.out.println(map);
}
}
Output:
{p=1, r=2, o=1, g=2, a=1, m=2, i=1, n=1}
Explanation: The program uses a HashMap
to store each character as a key and its occurrence count as the value. The getOrDefault
method provides a default value of 0 if the character is not already in the map.
These questions cover basic to intermediate Java concepts, such as collections, loops, and string manipulation, which are helpful for a Java developer with 5 years of experience.