Top 20 Java Programming Interview Questions:
1) Write a Java Program to reverse a string without using String inbuilt function.
import java.util.Scanner;
// Java Programming Interview Questions
public class ReverseString {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
char[] charArray = input.toCharArray();
int start = 0;
int end = charArray.length - 1;
while (start < end) {
// Swap characters at start and end indices
char temp = charArray[start];
charArray[start] = charArray[end];
charArray[end] = temp;
// Move indices towards each other
start++;
end--;
}
String reversedString = new String(charArray);
System.out.println("Reversed string: " + reversedString);
}
}
Enter a string: Hello
Reversed string: olleH
2)Write a Java Program to reverse a string without using String inbuilt function reverse().
import java.util.Scanner;
// 20 Java Programming Interview Questions
public class ReverseStringWithoutReverse {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
char[] charArray = input.toCharArray();
for (int i = charArray.length - 1; i >= 0; i--) {
System.out.print(charArray[i]);
}
}
}
Enter a string: World
dlroW
3) Write a Java Program to swap two numbers using the third variable.
import java.util.Scanner;
// 20 Java Programming Interview Questions
public class SwapWithVariable {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
int temp;
temp = num1;
num1 = num2;
num2 = temp;
System.out.println("After swapping: First number = " + num1 + ", Second number = " + num2);
}
}
Enter the first number: 5
Enter the second number: 10
After swapping: First number = 10, Second number = 5
4)Write a Java Program to swap two numbers without using the third variable.
import java.util.Scanner;
// 20 Java Programming Interview Questions
public class SwapWithoutVariable {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
System.out.println("After swapping: First number = " + num1 + ", Second number = " + num2);
}
}
Enter the first number: 5
Enter the second number: 10
After swapping: First number = 10, Second number = 5
5) Write a Java Program to find whether a number is prime or not.
import java.util.Scanner;
// 20 Java Programming Interview Questions
public class PrimeNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
boolean isPrime = true;
if (num <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= Math.sqrt(num); i++) {
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.");
}
}
}
Enter a number: 7
7 is a prime number.
6)Write a Java Program to find whether a string or number is palindrome or not.
import java.util.Scanner;
// 20 Java Programming Interview Questions
public class Palindrome {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string or number: ");
String input = scanner.nextLine();
String reversed = "";
for (int i = input.length() - 1; i >= 0; i--) {
reversed += input.charAt(i);
}
if (input.equals(reversed)) {
System.out.println(input + " is a palindrome.");
} else {
System.out.println(input + " is not a palindrome.");
}
}
}
Enter a string or number: radar
radar is a palindrome.
7) Write a Java Program to find the duplicate characters in a string.
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
// 20 Java Programming Interview Questions
public class DuplicateCharacters {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
Map<Character, Integer> charCountMap = new HashMap<>();
for (char c : input.toCharArray()) {
charCountMap.put(c, charCountMap.getOrDefault(c, 0) + 1);
}
System.out.println("Duplicate characters in the string:");
for (Map.Entry<Character, Integer> entry : charCountMap.entrySet()) {
if (entry.getValue() > 1) {
System.out.println(entry.getKey() + " - " + entry.getValue() + " times");
}
}
}
}
Enter a string: programming
Duplicate characters in the string:
r - 2 times
g - 2 times
m - 2 times
8)How do you check if a list of integers contains only odd numbers in Java?
import java.util.ArrayList;
import java.util.List;
// 20 Java Programming Interview Questions
public class CheckOddNumbers {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
numbers.add(3);
numbers.add(7);
numbers.add(15);
numbers.add(8);
boolean containsOnlyOdd = true;
for (int num : numbers) {
if (num % 2 == 0) {
containsOnlyOdd = false;
break;
}
}
if (containsOnlyOdd) {
System.out.println("The list contains only odd numbers.");
} else {
System.out.println("The list contains at least one even number.");
}
}
}
The list contains at least one even number.
9) How do you remove spaces from a string in Java?
// 20 Java Programming Interview Questions
public class Main {
public static void main(String[] args) {
String str = "Hello, World!";
String stringWithoutSpaces = str.replaceAll("\\s", "");
System.out.println("String without spaces: " + stringWithoutSpaces);
}
}
String without spaces: Hello,World!
10) Write a program to print duplicate numbers.
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5, 2, 6, 7, 8, 1, 9, 10, 10};
System.out.println("Duplicate numbers:");
for (int i = 0; i < numbers.length; i++) {
for (int j = i + 1; j < numbers.length; j++) {
if (numbers[i] == numbers[j]) {
System.out.println(numbers[j]);
break;
}
}
}
}
}
Duplicate numbers:
1
2
10
11) Write a program to find min and max numbers in the array.
// 20 Java Programming Interview Questions
public class Main {
public static void main(String[] args) {
int[] numbers = {7, 2, 9, 1, 5, 4, 6};
int min = numbers[0];
int max = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] < min) {
min = numbers[i];
}
if (numbers[i] > max) {
max = numbers[i];
}
}
System.out.println("Minimum number: " + min);
System.out.println("Maximum number: " + max);
}
}
Minimum number: 1
Maximum number: 9
12) Write a program to find the second-highest number in an array.
public class Main {
public static void main(String[] args) {
int[] numbers = {7, 2, 9, 1, 5, 4, 6};
int max = Integer.MIN_VALUE;
int secondMax = Integer.MIN_VALUE;
for (int number : numbers) {
if (number > max) {
secondMax = max;
max = number;
} else if (number > secondMax && number != max) {
secondMax = number;
}
}
System.out.println("Second-highest number: " + secondMax);
}
}
Second-highest number: 7
13) Write a program to Print Even and Odd Numbers.
// 20 Java Programming Interview Questions
public class Main {
public static void main(String[] args) {
int limit = 10;
System.out.println("Even numbers:");
for (int i = 0; i <= limit; i++) {
if (i % 2 == 0) {
System.out.println(i);
}
}
System.out.println("Odd numbers:");
for (int i = 0; i <= limit; i++) {
if (i % 2 != 0) {
System.out.println(i);
}
}
}
}
Even numbers:
0
2
4
6
8
10
Odd numbers:
1
3
5
7
9
14) Write a program to sort the array
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] numbers = {7, 2, 9, 1, 5, 4, 6};
Arrays.sort(numbers);
System.out.println("Sorted array:");
for (int number : numbers) {
System.out.println(number);
}
}
}
Sorted array:
1
2
4
5
6
7
9
15) write a program to remove duplicates in array
import java.util.Arrays;
// 20 Java Programming Interview Questions
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 2, 5, 6, 1, 3, 7, 8, 9, 7};
int length = numbers.length;
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
if (numbers[i] == numbers[j]) {
// Shift elements to the left
for (int k = j; k < length - 1; k++) {
numbers[k] = numbers[k + 1];
}
length--;
j--;
}
}
}
// Create a new array with unique elements
int[] uniqueNumbers = Arrays.copyOf(numbers, length);
// Print the array without duplicates
System.out.println("Array without duplicates:");
for (int number : uniqueNumbers) {
System.out.println(number);
}
}
}
Array without duplicates:
1
2
3
4
5
6
7
8
9
16) How to check whether user input is number or not in Java?
import java.util.Scanner;
// 20 Java Programming Interview Questions
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
if (scanner.hasNextInt()) {
int number = scanner.nextInt();
System.out.println("Input is a number. Number entered: " + number);
} else {
String input = scanner.next();
System.out.println("Input is not a number. Input entered: " + input);
}
scanner.close();
}
}
If you run this program and enter a number, it will output:
Enter a number: 123
Input is a number. Number entered: 123
If you enter a non-numeric input, it will output:
Enter a number: abc
Input is not a number. Input entered: abc
17) Write a program in Java to find sum of digits of a number.
import java.util.Scanner;
// 20 Java Programming Interview Questions
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int sum = 0;
while (number != 0) {
int digit = number % 10;
sum += digit;
number /= 10;
}
System.out.println("Sum of digits: " + sum);
scanner.close();
}
}
Enter a number: 123
Sum of digits: 6
18) Write a Java program to check the given year is leap or not?
import java.util.Scanner;
// 20 Java Programming Interview Questions
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = scanner.nextInt();
boolean isLeapYear = false;
if (year % 4 == 0) {
if (year % 100 == 0) {
// If year is divisible by 100, then it should also be divisible by 400 to be a leap year
if (year % 400 == 0) {
isLeapYear = true;
}
} else {
isLeapYear = true;
}
}
if (isLeapYear) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
scanner.close();
}
}
If you enter 2020
as the input, the program will output:
Enter a year: 2020
2020 is a leap year.
If you enter 2021
as the input, the program will output:
Enter a year: 2021
2021 is not a leap year.
19) Write a Java Program to find the duplicate characters in a string
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
// 20 Java Programming Interview Questions
public class Main {
public static void main(String[] args) {
String str = "programming";
Map<Character, Integer> charCountMap = new HashMap<>();
// Count occurrences of each character in the string
for (char c : str.toCharArray()) {
if (charCountMap.containsKey(c)) {
charCountMap.put(c, charCountMap.get(c) + 1);
} else {
charCountMap.put(c, 1);
}
}
// Print duplicate characters
Set<Map.Entry<Character, Integer>> entrySet = charCountMap.entrySet();
System.out.println("Duplicate characters in the string '" + str + "':");
for (Map.Entry<Character, Integer> entry : entrySet) {
if (entry.getValue() > 1) {
System.out.println(entry.getKey() + ": " + entry.getValue() + " times");
}
}
}
}
Duplicate characters in the string 'programming':
g: 2 times
r: 2 times
m: 2 times
20) How do you merge two lists in Java?
import java.util.ArrayList;
import java.util.List;
// 20 Java Programming Interview Questions
public class Main {
public static void main(String[] args) {
List<Integer> list1 = new ArrayList<>();
list1.add(1);
list1.add(2);
list1.add(3);
List<Integer> list2 = new ArrayList<>();
list2.add(4);
list2.add(5);
list2.add(6);
list1.addAll(list2); // Merge list2 into list1
System.out.println("Merged list: " + list1);
}
}
Merged list: [1, 2, 3, 4, 5, 6]
Explore More Topics:
-> Master Spring Data JPA Custom Query Example: A Simple Guide
-> Spring Boot REST API CRUD Example With MySQL Database
-> Login Registration and Forgot Password Form in HTML CSS
-> Step-by-Step Guide: Setting up Jenkins on Amazon EC2 Instance with Ubuntu
-> Creating a Responsive Login and Signup Form in React.js