Top 30 Coding Interview Questions You Should Know in Java
Java coding interviews are designed to test how effectively a candidate can turn a problem statement into a working solution. Along with Java syntax, interviewers commonly focus on logical thinking, loops, strings, arrays, recursion, sorting, and basic problem-solving techniques.
This collection brings together 30 Java coding interview questions, starting with fundamental programming tasks and gradually moving toward more challenging array, string, and algorithm-based problems. Practicing these questions can help students and developers become more comfortable with coding rounds.
Table of Contents

YT:- DecodeIT
Basic Java Coding Questions
- Write a Java program to calculate the sum of two numbers.
- Find the area of a circle using its radius.
- Convert a temperature from Fahrenheit to Celsius.
- Determine whether a number is prime.
- Reverse a given string.
- Calculate the factorial of a number using recursion.
- Find the sum of natural numbers up to a specified value.
- Find the largest value among three numbers.
- Check whether a string is a palindrome.
- Count the vowels present in a string.
Array and Problem-Solving Questions
- Calculate the power of a number with recursion.
- Find the common intersection of two arrays.
- Calculate the LCM of two numbers.
- Find the second-largest value in an array.
- Rotate an array toward the right by
kpositions. - Check whether every character in a string is unique.
- Calculate the total of all values in a two-dimensional array.
- Sort an integer array in ascending order.
- Count the words contained in a string.
- Find the missing value from an integer array.
Advanced Java Coding Questions
- Find the square root of a number using the Newton-Raphson approach.
- Combine two already sorted arrays into one sorted array.
- Find the longest substring that contains no repeated characters.
- Check whether a number is a perfect number.
- Find common values between two arrays.
- Calculate factorial without recursion.
- Find the majority element in an array.
- Perform multiplication of two matrices.
- Count how many times a character appears in a string.
- Calculate the sum of even numbers up to a specified limit.
Java Coding Solutions
1. Sum of Two Numbers
double a=10,b=20;
System.out.println("Sum: "+(a+b));
2. Area of a Circle
double radius=5;
double area=Math.PI*Math.pow(radius,2);
System.out.println(area);
3. Fahrenheit to Celsius
double fahrenheit=98.6;
double celsius=(fahrenheit-32)*5/9;
System.out.println(celsius);
4. Prime Number Check
int n=17;
boolean prime=n>1;
for(int i=2;i<=Math.sqrt(n)&′i++)
if(n%i==0) prime=false;
System.out.println(prime);
5. Reverse a String
String text="Hello";
String reversed=new StringBuilder(text).reverse().toString();
System.out.println(reversed);
6. Factorial Using Recursion
static long factorial(int n){
return n==0 ? 1 : n*factorial(n-1);
}
7. Sum of Natural Numbers
int n=10,sum=0;
for(int i=1;i<=n;i++) sum+=i;
System.out.println(sum);
8. Largest of Three Numbers
double a=12,b=25,c=18;
System.out.println(Math.max(a,Math.max(b,c)));
9. Palindrome Check
String text="racecar";
String reverse=new StringBuilder(text).reverse().toString();
System.out.println(text.equals(reverse));
10. Count Vowels
String text="Programming";
int count=0;
for(char c:text.toLowerCase().toCharArray())
if("aeiou".indexOf(c)>=0) count++;
System.out.println(count);
11. Power Using Recursion
static int power(int base,int exp){
return exp==0 ? 1 : base*power(base,exp-1);
}
12. Intersection of Two Arrays
int[] a={1,2,3,4,5},b={3,4,5,6};
for(int x:a)
for(int y:b)
if(x==y) System.out.print(x+" ");
13. LCM of Two Numbers
int a=12,b=18;
int max=Math.max(a,b),min=Math.min(a,b),lcm=max;
while(lcm%min!=0) lcm+=max;
System.out.println(lcm);
14. Second Largest Element
int[] a={10,5,8,20,15};
Arrays.sort(a);
System.out.println(a[a.length-2]);
15. Right Array Rotation
int[] a={1,2,3,4,5};
int k=2;
int[] result=new int[a.length];
for(int i=0;i<a.length;i++)
result[(i+k)%a.length]=a[i];
System.out.println(Arrays.toString(result));
16. Unique Characters
String text="abcdefg";
System.out.println(text.chars().distinct().count()==text.length());
17. Sum of a 2D Array
int[][] a={{1,2,3},{4,5,6}};
int sum=0;
for(int[] row:a)
for(int value:row) sum+=value;
System.out.println(sum);
18. Sort an Array
int[] a={5,3,8,1,2};
Arrays.sort(a);
System.out.println(Arrays.toString(a));
19. Count Words
String text="This is a sample sentence";
System.out.println(text.trim().split("\\s+").length);
20. Find the Missing Number
int[] a={1,2,4,5};
int n=5,total=n*(n+1)/2;
for(int value:a) total-=value;
System.out.println(total);
21. Newton-Raphson Square Root
double n=16,x=n;
while(Math.abs(x-n/x)>1e-10)
x=(x+n/x)/2;
System.out.println(x);
22. Merge Sorted Arrays
int[] a={1,3,5},b={2,4,6};
int[] result=new int[a.length+b.length];
int i=0,j=0,k=0;
while(i<a.length&&j<b.length)
result[k++]=a[i]<b[j]?a[i++]:b[j++];
while(i<a.length) result[k++]=a[i++];
while(j<b.length) result[k++]=b[j++];
23. Longest Substring Without Repeating Characters
String text="abcabcbb";
Set<Character> set=new HashSet<>();
int left=0,max=0;
for(int right=0;right<text.length();right++){
while(set.contains(text.charAt(right)))
set.remove(text.charAt(left++));
set.add(text.charAt(right));
max=Math.max(max,right-left+1);
}
System.out.println(max);
Complete Advance AI Topics: Click Here
SQL Tutorial: Click Here
Questions 24–30 for Further Practice
The remaining problems continue into more algorithm-focused areas. Try solving them independently before checking a reference implementation.
- Check whether a number is a perfect number.
- Find common elements between two arrays.
- Calculate factorial without recursion.
- Find the majority element in an array.
- Implement matrix multiplication.
- Count occurrences of a character in a string.
- Find the sum of even numbers up to a given limit.
Final Thoughts
These 30 coding problems provide useful practice across the areas commonly encountered in Java programming interviews. Start with the basic exercises, then move toward arrays, recursion, strings, and algorithmic challenges. Instead of simply memorizing solutions, try writing each program yourself and testing it with different inputs.
Keywords: Top 30 Coding Interview Questions You Should Know, Java coding interview questions, Java interview questions, Java coding questions, Core Java interview questions, Java programming interview questions,Top 30 Coding Interview Questions You Should Know in Java