00:00

Java Interview Programming Questions and Solutions

This page provides a collection of the most commonly asked Java interview programming questions, designed to help beginners as well as experienced Java developers strengthen their problem-solving skills. Each problem focuses on building a strong foundation in core Java concepts such as strings, arrays, loops, conditions, and basic data manipulation, which are frequently tested in technical interviews.

For every program, we have clearly defined the problem statement along with expected input and output to make understanding easier and learning more effective. These questions are especially useful for Java freshers, mid-level developers, and experienced professionals who are preparing for coding rounds, technical interviews, or revising fundamental Java concepts.


Top 20 Java String Programming Interview Questions

1. Reverse a String

Write a program to reverse a string without using built-in reverse methods.

Input: "Java"
Output: "avaJ"

2. Check if a String is Palindrome

Determine whether a given string is a palindrome.

Input: "madam"
Output: true

3. Count Occurrence of Characters

Count the number of times each character appears in a string.

Input: "apple"
Output: a=1, p=2, l=1, e=1

4. Count Vowels and Consonants

Write a program to count vowels and consonants in a string.

Input: "hello"
Output: Vowels=2, Consonants=3

5. Find Duplicate Characters

Print duplicate characters in a string.

Input: "programming"
Output: r, g, m

6. Remove Duplicate Characters

Remove duplicate characters and return a string with unique characters.

Input: "banana"
Output: "ban"

7. Find First Non-Repeated Character

Find the first non-repeating character in a string.

Input: "swiss"
Output: w

8. Check Anagram Strings

Check whether two strings are anagrams of each other.

Input: "listen", "silent"
Output: true

9. Count Words in a String

Write a program to count the number of words in a sentence.

Input: "Java is powerful"
Output: 3

10. Reverse Words in a Sentence

Reverse each word or reverse the entire sentence.

Input: "Java is fun"
Output: "fun is Java"

11. Find Longest Word in a String

Find the longest word from a given string.

Input: "Java makes development easier"
Output: development

12. Replace Characters Without replace()

Replace all spaces with %20 without using String.replace().

Input: "Java is easy"
Output: "Java%20is%20easy"

13. String Rotation Check

Check if one string is a rotation of another.

Input: "ABCD", "CDAB"
Output: true

14. Remove Special Characters

Remove all special characters from a string.

Input: "Ja@va#1$"
Output: "Java1"

15. Count Digits, Letters, and Special Characters

Count letters, digits, spaces, and special characters.

Input: "Java@123"
Output: Letters=4, Digits=3, Special=1

16. Find Substring Occurrences

Count how many times a substring appears in a string.

Input: "abababa", "aba"
Output: 2

17. Toggle Case of Characters

Convert lowercase characters to uppercase and uppercase to lowercase.

Input: "JaVa"
Output: "jAvA"

18. Check if String Contains Only Digits

Check if the string contains only numeric characters.

Input: "12345"
Output: true

19. Print All Permutations of a String

Print all permutations of a string.

Input: "ABC"
Output: ABC, ACB, BAC, BCA, CAB, CBA

20. String Compression

Compress a string like aaabbc → a3b2c1.

Input: "aaabbc"
Output: "a3b2c1"



Top 20 Java Array Programming Interview Questions

1. Find the Largest Element in an Array

4. Reverse an Array

Write a program to reverse the elements of an array.

Input: [1, 2, 3, 4]
Output: [4, 3, 2, 1]

5. Sort an Array

Write a program to sort an array in ascending order.

Input: [5, 2, 8, 1]
Output: [1, 2, 5, 8]

6. Remove Duplicate Elements

Remove duplicate elements from an array.

Input: [1, 2, 2, 3, 1]
Output: [1, 2, 3]

7. Find Duplicate Elements

Find and print duplicate elements in an array.

Input: [1, 2, 2, 3, 1]
Output: [1, 2]

8. Sum of Array Elements

Calculate the sum of all elements in an array.

Input: [1, 2, 3, 4]
Output: 10

9. Average of Array Elements

Find the average of numbers in an array.

Input: [2, 4, 6, 8]
Output: 5

10. Check if Array is Sorted

Check whether an array is sorted in ascending order.

Input: [1, 2, 3, 4]
Output: true

11. Merge Two Arrays

Merge two arrays into a single array.

Input: [1, 2], [3, 4]
Output: [1, 2, 3, 4]

12. Find Common Elements Between Two Arrays

Find common elements between two arrays.

Input: [1, 2, 3], [2, 3, 4]
Output: [2, 3]

13. Find Missing Number in an Array

Find a missing number from a given range.

Input: [1, 2, 4, 5]
Output: 3

14. Move Zeros to End

Move all zeros to the end of the array.

Input: [0, 1, 0, 3, 12]
Output: [1, 3, 12, 0, 0]

15. Rotate Array

Rotate an array to the right by one position.

Input: [1, 2, 3, 4]
Output: [4, 1, 2, 3]

16. Find Frequency of Elements

Count the frequency of each element in an array.

Input: [1, 1, 2, 3, 2]
Output: 1=2, 2=2, 3=1

17. Find Pair with Given Sum

Find a pair of elements whose sum equals a given number.

Input: [2, 4, 3, 5], Sum=7
Output: (2,5) or (4,3)

18. Find Maximum Product of Two Elements

Find the maximum product of two elements in an array.

Input: [2, 3, 5, 7]
Output: 35

19. Separate Even and Odd Numbers

Separate even and odd numbers in an array.

Input: [1, 2, 3, 4, 5]
Output: Even=[2,4], Odd=[1,3,5]

20. Check Equality of Two Arrays

Check if two arrays are equal.

Input: [1, 2, 3], [1, 2, 3]
Output: true