Java Coding Interview Questions

Preparing for a Java coding interview requires a solid understanding of the language and the ability to solve coding problems efficiently. In this article, we will cover a range of Java interview questions, starting from easy ones to more challenging coding problems.

Java coding interview questions

Java Coding Interview Qusetions

Write a Java Program to check if a number is Even or Odd

import java.util.Scanner;

public class EvenOdd {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number");
        int number = sc.nextInt();
        if(number%2==0){
            System.out.println("Number is Even");
        }
        else{
            System.out.println("Number is Odd");
        }
    }
}

Java Program to Check if Given Number Is Even or Odd using Ternary Operator

import java.util.Scanner;

public class EvenOdd {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number");
        int number = sc.nextInt();
         String ans = number%2==0 ? "Number is Even": "Number is Odd";
        System.out.println(ans);
    }
    }

Write a Java program to check if a number is prime.

import java.util.Scanner;

public class PrimeNumber {
    public static void main(String[] args) {
        Scanner sc =new Scanner(System.in);
        System.out.println("Enter a Number");
        int num = sc.nextInt();
        boolean flag = true;
        for(int i=2;i<=num/2;i++){
            if(num%i==0){
            flag=false;
            break;
            }
        }

        if(flag)
            System.out.println("Prime Number");
        else System.out.println("NOn Prime Number");
    }
}

Write a program to print all prime number in range 1 to n

import java.util.Scanner;

public class PrimeNos {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the Range");

        int num = sc.nextInt();

        for (int i = 1; i <= num; i++) {
            boolean flag = true;
            for (int j = 2; j <=i/2; j++) {
                if (i % j == 0) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                System.out.println(i);
            }
  }
}
}


Write a program to reverse a string.

import java.util.Scanner;

public class StringReverse {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        
        String rev="";

        for(int i = str.length()-1;i>=0;i--){
            rev=rev+str.charAt(i);
        }

        System.out.println(rev);

    }
}


Write a program to check if a string is a palindrome.

import java.util.Scanner;

public class Palindrome {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        String rev="";
        for(int i = str.length()-1;i>=0;i--){
            rev=rev+str.charAt(i);
        }

        if(rev.equals(str))
        System.out.println("Palindrome");
        else
            System.out.println("Not Palindrome");

    }
}


Write a program to find the factorial of a number.

import java.util.Scanner;

public class Factorial {
    public static long factorial(int n){
        if(n==1 || n==0)
            return 1;
        else
            return n*factorial(n-1);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("enter the number to check factorial");
        int n = sc.nextInt();
        if(n<0)
            System.out.println("Invalid Input");
        else{
            System.out.println("Factorial of "+n+" = "+factorial(n));
        }
    }
}

Leave a Comment