Interview Question

Top 30 Coding Interview Questions You Should Know in Java

Top 30 Coding Interview Questions You Should Know ! - Image 63
Image 63

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.

Top 30 Coding Interview Questions You Should Know in Java

YT:- DecodeIT

Basic Java Coding Questions

  1. Write a Java program to calculate the sum of two numbers.
  2. Find the area of a circle using its radius.
  3. Convert a temperature from Fahrenheit to Celsius.
  4. Determine whether a number is prime.
  5. Reverse a given string.
  6. Calculate the factorial of a number using recursion.
  7. Find the sum of natural numbers up to a specified value.
  8. Find the largest value among three numbers.
  9. Check whether a string is a palindrome.
  10. Count the vowels present in a string.

Array and Problem-Solving Questions

  1. Calculate the power of a number with recursion.
  2. Find the common intersection of two arrays.
  3. Calculate the LCM of two numbers.
  4. Find the second-largest value in an array.
  5. Rotate an array toward the right by k positions.
  6. Check whether every character in a string is unique.
  7. Calculate the total of all values in a two-dimensional array.
  8. Sort an integer array in ascending order.
  9. Count the words contained in a string.
  10. Find the missing value from an integer array.

Advanced Java Coding Questions

  1. Find the square root of a number using the Newton-Raphson approach.
  2. Combine two already sorted arrays into one sorted array.
  3. Find the longest substring that contains no repeated characters.
  4. Check whether a number is a perfect number.
  5. Find common values between two arrays.
  6. Calculate factorial without recursion.
  7. Find the majority element in an array.
  8. Perform multiplication of two matrices.
  9. Count how many times a character appears in a string.
  10. 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)&&prime;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

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us