
Table of Contents
Togglecore java interview programs in JNNC Technologies
Here are some core Java interview programs that you might encounter during job interviews for Java development roles. core java interview programs.These problems test your understanding of key Java concepts, including data structures, algorithms, object-oriented principles, and basic Java syntax.
1. Reverse a String
-
Problem: Write a Java program to reverse a given string.
-
Example:
javapublic class ReverseString {
public static void main(String[] args) {
String str = "Java Interview";
String reversed = "";
for (int i = str.length() - 1; i >= 0; i--) {
reversed += str.charAt(i);
}
System.out.println("Reversed String: " + reversed);
}
}
-
Expected Output:
nginxweivretnI avaJ
2. Check for Palindrome
-
Problem: Write a program to check if a string is a palindrome.
-
Example:
javapublic class PalindromeCheck {
public static void main(String[] args) {
String str = "madam";
String reversed = new StringBuilder(str).reverse().toString();
if (str.equals(reversed)) {
System.out.println(str + " is a palindrome");
} else {
System.out.println(str + " is not a palindrome");
}
}
}
-
Expected Output:
csharpmadam is a palindrome
3. Fibonacci Series
-
Problem: Write a Java program to print the Fibonacci series up to
n
terms. -
Example:
javapublic class FibonacciSeries {
public static void main(String[] args) {
int n = 10, first = 0, second = 1;
System.out.print("Fibonacci Series: " + first + ", " + second);
for (int i = 3; i <= n; i++) {
int next = first + second;
System.out.print(", " + next);
first = second;
second = next;
}
}
}
-
Expected Output:
mathematicaFibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
4. Prime Number Check
-
Problem: Write a program to check whether a given number is prime or not.
-
Example:
java
for (int i = 2; i <= Math.sqrt(num); i++) {public class PrimeNumberCheck {
public static void main(String[] args) {
int num = 29;
boolean isPrime = true;
if (num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.println(num + ” is a prime number”);
} else {
System.out.println(num + ” is not a prime number”);
}
}
} -
Expected Output:
csharp29 is a prime number
5. Factorial of a Number
-
Problem: Write a Java program to find the factorial of a number.
-
Example:
java
for (int i = 1; i <= num; i++) {public class Factorial {
public static void main(String[] args) {
int num = 5;
int factorial = 1;
factorial *= i;
}
System.out.println(“Factorial of “ + num + ” is “ + factorial);
}
} -
Expected Output:
csharpFactorial of 5 is 120
6. Count Occurrences of a Character in a String
-
Problem: Write a program to count the number of occurrences of a specific character in a string.
-
Example:
java
for (int i = 0; i < str.length(); i++) {public class CountOccurrences {
public static void main(String[] args) {
String str = "programming";
char ch = 'g';
int count = 0;
if (str.charAt(i) == ch) {
count++;
}
}
System.out.println(“Occurrences of ‘” + ch + “‘: “ + count);
}
} -
Expected Output:
nginxOccurrences of 'g': 2
7. Find the Largest and Smallest Element in an Array
-
Problem: Write a program to find the largest and smallest element in an array.
-
Example:
java
for (int i = 1; i < arr.length; i++) {public class FindLargestSmallest {
public static void main(String[] args) {
int[] arr = {3, 5, 7, 2, 8, 1};
int largest = arr[0];
int smallest = arr[0];
if (arr[i] > largest) {
largest = arr[i];
}
if (arr[i] < smallest) {
smallest = arr[i];
}
}
System.out.println(“Largest: “ + largest);
System.out.println(“Smallest: “ + smallest);
}
} -
Expected Output:
makefileLargest: 8
Smallest: 1
8. Armstrong Number
-
Problem: Write a program to check if a number is an Armstrong number.
-
Example:
java
while (num > 0) {public class ArmstrongNumber {
public static void main(String[] args) {
int num = 153;
int sum = 0;
int originalNum = num;
int digit = num % 10;
sum += Math.pow(digit, 3);
num /= 10;
}
if (sum == originalNum) {
System.out.println(originalNum + ” is an Armstrong number”);
} else {
System.out.println(originalNum + ” is not an Armstrong number”);
}
}
} -
Expected Output:
csharp153 is an Armstrong number
9. Sum of Digits of a Number
-
Problem: Write a program to find the sum of digits of a given number.
-
Example:
java
while (num > 0) {public class SumOfDigits {
public static void main(String[] args) {
int num = 1234;
int sum = 0;
sum += num % 10;
num /= 10;
}
System.out.println(“Sum of digits: “ + sum);
}
} -
Expected Output:
yamlSum of digits: 10
10. Sorting an Array (Bubble Sort)
-
Problem: Write a Java program to implement bubble sort.
-
Example:
java
System.out.print(“Sorted Array: “);public class BubbleSort {
public static void main(String[] args) {
int[] arr = {5, 2, 9, 1, 5, 6};
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int i : arr) {
System.out.print(i + ” “);
}
}
} -
Expected Output:
javascriptSorted Array: 1 2 5 5 6 9
Here are more Java interview programs to help you prepare for your interviews. These problems explore additional Java concepts, including recursion, sorting algorithms, and data structures. Mastering these will enhance your coding proficiency and problem-solving skills.
11. Find the Missing Number in an Array
-
Problem: Given an array of integers from 1 to n with one number missing, find the missing number.
-
Example:
java
for (int i : arr) {public class MissingNumber {
public static void main(String[] args) {
int[] arr = {1, 2, 4, 5, 6};
int n = 6; // Maximum number in the array
int total = n * (n + 1) / 2;
int sum = 0;
sum += i;
}
System.out.println(“Missing number is: “ + (total – sum));
}
} -
Expected Output:
csharpMissing number is: 3
12. Find the Second Largest Number in an Array
-
Problem: Write a Java program to find the second largest element in an array.
-
Example:
java
for (int i = 1; i < arr.length; i++) {public class SecondLargest {
public static void main(String[] args) {
int[] arr = {10, 20, 4, 45, 99};
int largest = arr[0];
int secondLargest = Integer.MIN_VALUE;
if (arr[i] > largest) {
secondLargest = largest;
largest = arr[i];
} else if (arr[i] > secondLargest && arr[i] != largest) {
secondLargest = arr[i];
}
}
System.out.println(“Second Largest: “ + secondLargest);
}
} -
Expected Output:
sqlSecond Largest: 45
13. Armstrong Number for N Digits
-
Problem: Write a program to check if a number is an Armstrong number for
n
digits. -
Example:
java
while (num > 0) {public class ArmstrongNDigits {
public static void main(String[] args) {
int num = 1634;
int originalNum = num;
int numDigits = String.valueOf(num).length();
int sum = 0;
int digit = num % 10;
sum += Math.pow(digit, numDigits);
num /= 10;
}
if (sum == originalNum) {
System.out.println(originalNum + ” is an Armstrong number”);
} else {
System.out.println(originalNum + ” is not an Armstrong number”);
}
}
} -
Expected Output:
csharp1634 is an Armstrong number
14. Merge Two Sorted Arrays
-
Problem: Write a Java program to merge two sorted arrays into one sorted array.
-
Example:
java
while (i < arr1.length && j < arr2.length) {public class MergeSortedArrays {
public static void main(String[] args) {
int[] arr1 = {1, 3, 5, 7};
int[] arr2 = {2, 4, 6, 8};
int[] merged = new int[arr1.length + arr2.length];
int i = 0, j = 0, k = 0;
if (arr1[i] < arr2[j]) {
merged[k++] = arr1[i++];
} else {
merged[k++] = arr2[j++];
}
}
while (i < arr1.length) {
merged[k++] = arr1[i++];
}
while (j < arr2.length) {
merged[k++] = arr2[j++];
}
System.out.print(“Merged Array: “);
for (int num : merged) {
System.out.print(num + ” “);
}
}
} -
Expected Output:
javascriptMerged Array: 1 2 3 4 5 6 7 8
15. Find Duplicate Elements in an Array
-
Problem: Write a Java program to find all duplicate elements in an array.
-
Example:
javaimport java.util.HashSet;
public class FindDuplicates {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 3, 6, 7, 8, 9, 5};
HashSet<Integer> set = new HashSet<>();System.out.print(“Duplicate Elements: “);
for (int i = 0; i < arr.length; i++) {
if (!set.add(arr[i])) {
System.out.print(arr[i] + ” “);
}
}
}
} -
Expected Output:
yamlDuplicate Elements: 3 5
16. Calculate the Factorial Using Recursion
-
Problem: Write a Java program to calculate the factorial of a number using recursion.
-
Example:
java
public static int factorial(int n) {public class FactorialRecursion {
public static void main(String[] args) {
int num = 5;
System.out.println("Factorial of " + num + " is " + factorial(num));
}
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n – 1);
}
}
} -
Expected Output:
csharpFactorial of 5 is 120
17. Find the Sum of Natural Numbers
-
Problem: Write a Java program to find the sum of the first
n
natural numbers. -
Example:
javapublic class SumNaturalNumbers {
public static void main(String[] args) {
int n = 10;
int sum = n * (n + 1) / 2;
System.out.println("Sum of first " + n + " natural numbers is " + sum);
}
}
-
Expected Output:
sqlSum of first 10 natural numbers is 55
18. Count Words in a String
-
Problem: Write a Java program to count the number of words in a given string.
-
Example:
javapublic class CountWords {
public static void main(String[] args) {
String str = "Java is fun to learn";
String[] words = str.split(" ");
System.out.println("Number of words: " + words.length);
}
}
-
Expected Output:
javascriptNumber of words: 5
19. Find the GCD of Two Numbers
-
Problem: Write a Java program to find the GCD (Greatest Common Divisor) of two numbers using recursion.
-
Example:
java
public static int findGCD(int a, int b) {public class GCD {
public static void main(String[] args) {
int a = 56, b = 98;
System.out.println("GCD of " + a + " and " + b + " is: " + findGCD(a, b));
}
if (b == 0) {
return a;
}
return findGCD(b, a % b);
}
} -
Expected Output:
csharpGCD of 56 and 98 is: 14
20. Bubble Sort Implementation
-
Problem: Implement bubble sort algorithm to sort an array.
-
Example:
java
System.out.print(“Sorted array: “);public class BubbleSort {
public static void main(String[] args) {
int[] arr = {64, 25, 12, 22, 11};
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int num : arr) {
System.out.print(num + ” “);
}
}
} -
Expected Output:
phpSorted array: 11 12 22 25 64
-
21. Find the Length of a String Without Using Built-in Methods
-
Problem: Write a Java program to find the length of a string without using the built-in
length()
method. -
Example:
java
for (char c : str.toCharArray()) {public class StringLength {
public static void main(String[] args) {
String str = "Hello, World!";
int length = 0;
length++;
}
System.out.println(“Length of string: “ + length);
}
} -
Expected Output:
csharpLength of string: 13
22. Find the Prime Numbers in a Given Range
-
Problem: Write a Java program to find all prime numbers between two numbers.
-
Example:
java
for (int num = start; num <= end; num++) {public class PrimeInRange {
public static void main(String[] args) {
int start = 10, end = 50;
boolean isPrime = true;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime && num > 1) {
System.out.print(num + ” “);
}
}
}
} -
Expected Output:
11 13 17 19 23 29 31 37 41 43 47
23. Find the Missing Element in an Array (Using XOR)
-
Problem: Find the missing element in an array containing all elements from 1 to
n
, except one. -
Example:
java
int xor1 = 0;public class MissingElementXOR {
public static void main(String[] args) {
int[] arr = {1, 2, 4, 5, 6};
int n = 6; // The maximum number
int xor2 = 0;
for (int i = 1; i <= n; i++) {
xor1 ^= i; // XOR for all numbers from 1 to n
}
for (int num : arr) {
xor2 ^= num; // XOR for all numbers in the array
}
System.out.println(“Missing element: “ + (xor1 ^ xor2));
}
} -
Expected Output:
yamlMissing element: 3
24. Find the Most Frequent Element in an Array
-
Problem: Write a Java program to find the most frequent element in an array.
-
Example:
javaimport java.util.HashMap;
public class MostFrequentElement {
public static void main(String[] args) {
int[] arr = {1, 3, 2, 1, 4, 1, 2, 1, 3};HashMap<Integer, Integer> map = new HashMap<>();
for (int num : arr) {
map.put(num, map.getOrDefault(num, 0) + 1);
}int maxCount = 0;
int mostFrequent = arr[0];for (int key : map.keySet()) {
if (map.get(key) > maxCount) {
maxCount = map.get(key);
mostFrequent = key;
}
}System.out.println(“Most frequent element: “ + mostFrequent);
}
} -
Expected Output:
yamlMost frequent element: 1
25. Find the Sum of Even and Odd Numbers in an Array
-
Problem: Write a Java program to find the sum of even and odd numbers separately in an array.
-
Example:
java
int evenSum = 0;public class EvenOddSum {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int oddSum = 0;
for (int num : arr) {
if (num % 2 == 0) {
evenSum += num;
} else {
oddSum += num;
}
}
System.out.println(“Sum of even numbers: “ + evenSum);
System.out.println(“Sum of odd numbers: “ + oddSum);
}
} -
Expected Output:
yamlSum of even numbers: 20
Sum of odd numbers: 25
26. Implement a Linked List (Basic Operations)
-
Problem: Write a Java program to implement a singly linked list and perform basic operations such as insertion and traversal.
-
Example:
java
public Node(int data) {class Node {
int data;
Node next;
this.data = data;
this.next = null;
}
}
public class LinkedList {
Node head;
public void insert(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
}
public void display() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + ” “);
temp = temp.next;
}
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.insert(10);
list.insert(20);
list.insert(30);
list.insert(40);
System.out.print(“Linked List: “);
list.display();
}
} -
Expected Output:
yamlLinked List: 10 20 30 40
27. Convert a String to an Integer Without Using Integer.parseInt()
-
Problem: Write a program to convert a string to an integer without using
Integer.parseInt()
. -
Example:
java
for (int i = 0; i < str.length(); i++) {public class StringToInt {
public static void main(String[] args) {
String str = "12345";
int result = 0;
result = result * 10 + (str.charAt(i) – ‘0’);
}
System.out.println(“Converted Integer: “ + result);
}
} -
Expected Output:
sqlConverted Integer: 12345
28. Sum of Digits Using Recursion
-
Problem: Write a Java program to find the sum of digits of a number using recursion.
-
Example:
java
public static int sumOfDigits(int n) {public class SumOfDigitsRecursion {
public static void main(String[] args) {
int num = 1234;
System.out.println("Sum of digits: " + sumOfDigits(num));
}
if (n == 0) {
return 0;
}
return n % 10 + sumOfDigits(n / 10);
}
} -
Expected Output:
yamlSum of digits: 10
29. Remove Duplicates from an Array
-
Problem: Write a program to remove duplicate elements from an array.
-
Example:
javaimport java.util.HashSet;
public class RemoveDuplicates {
public static void main(String[] args) {
int[] arr = {1, 2, 2, 3, 4, 4, 5};
HashSet<Integer> set = new HashSet<>();for (int num : arr) {
set.add(num);
}System.out.print(“Array after removing duplicates: “);
for (int num : set) {
System.out.print(num + ” “);
}
}
} -
Expected Output:
pgsqlArray after removing duplicates: 1 2 3 4 5
30. Sort an Array Using Merge Sort
-
Problem: Implement the merge sort algorithm to sort an array.
-
Example:
java
System.out.print(“Sorted array: “);public class MergeSort {
public static void main(String[] args) {
int[] arr = {12, 11, 13, 5, 6, 7};
mergeSort(arr, 0, arr.length - 1);
for (int num : arr) {
System.out.print(num + ” “);
}
}
public static void mergeSort(int[] arr, int left, int right) {
if (left < right) {
int mid = (left + right) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
public static void merge(int[] arr, int left, int mid, int right) {
int n1 = mid – left + 1;
int n2 = right – mid;
int[] L = new int[n1];
int[] R = new int[n2];
System.arraycopy(arr, left, L, 0, n1);
System.arraycopy(arr, mid + 1, R, 0, n2);
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k++] = L[i++];
} else {
arr[k++] = R[j++];
}
}
while (i < n1) {
arr[k++] = L[i++];
}
while (j < n2) {
arr[k++] = R[j++];
}
}
} -
Expected Output:
phpSorted array: 5 6 7 11 12 13
Conclusion:
These core Java interview programs cover various essential topics such as recursion, sorting, string manipulation, data structures (arrays, linked lists), and more. Practicing these problems will improve your coding and problem-solving skills, making you well-prepared for Java-related job interviews.core java interview programs
-