Commonly Asked Java Programs for Coding Interview

In this article, I am going to share Commonly Asked Java Programs for Coding Interview. You can expect many questions from this topic in the interview. Coding is an integral part of any programming job interviews Java development interviews are no exception. So prepare well for the coding interview.

Q1. Write a program in Java to print the Fibonacci series?
Ans:

import java.util.Scanner;

/**
 * Java program to  print Fibonacci number using Iteration.
 * Fibonacci number is sum of previous two Fibonacci numbers fb= fb-1+ fb-2
 * @author Ashish
 */
public class FiboCalculator {

    public static void main(String args[]) {
    
       //Enter the input to print Fibonacci series upto how many numbers
        System.out.println("Enter the  number to which Fibonacci series : ");
        int number = new Scanner(System.in).nextInt();
   
        //Displaying the Fibonacci series
        for(int i=1; i<=number; i++){
            System.out.print(fibo(i) +" ");
        }
   } 
 
    /*
     * Method to calculate Fibonacci number using loop or Iteration.
     * @return Fibonacci number
     */
    public static int fibo(int value){
        if( value == 1 || value == 2){
            return 1;
        }
        int fibFirst=1, fibSecond=1, fibonacci=1;
        for(int i= 3; i<= value; i++){
           
            //sum of previous two Fibonacci number
            fibonacci = fibFirst + fibSecond;             
            fibFirst = fibSecond;
            fibSecond = fibonacci;
          
        }
        return fibonacci;
      
    }   
  
}


Output: Enter the number to which Fibonacci series : 10 1 1 2 3 5 8 13 21 34 55

Q2. Write a Java program to check prime number?
Ans:

import java.util.Scanner;
/**
 * Program to check if a number is Prime or Not. 
 *
 * @author  Ashish 
 */
public class TestPrime {

    public static void main(String args[]) {
    
        System.out.println("Enter the number to check");
        int number = new Scanner(System.in).nextInt();
        while (number != 0) {
          
            System.out.printf("Does this number:"+ number+”is prime”+isPrime(number));
        }
    }


    /*
     * check if a number is prime or not.
     */
    public static String isPrime(int val) {
        if ( val < 0) {
            return "not valid";
        }
        if ( val == 0 || val == 1) {
            return "not prime";
        }
        if ( val == 2 || val == 3) {
            return "prime number";
        }
        if (( val * val - 1) % 24 == 0) {
            return "prime";
        } else {
            return "not prime";
        }
    }
}


Output:
Enter the number to check
Does this number:”+ 2+” is prime”+prime number
Does this number:”+ 3+” is prime”+prime number
Does this number:”+ 4+” is prime”+not prime
Does this number:”+ 7+” is prime”+prime

Q3. Write a program of a string is a palindrome or not?
Ans:

/**
 * Program to check if a String is palindrome or not
 *
 * @author Ashish
 */
public class Palindrome {
   
      public static void main(String args[]) {
        System.out.println("Is zzz palindrom?: " + isPalindrom("zzz");
        System.out.println("Is ashi palindrom?: " + isPalindrom("ashi"));
      }

    /**
     * Method to check if given String is Palindrome
     */
    public static boolean isPalindrom(String text){
       String reverse = reverse(text);
       if(text.equals(reverse)){
           return true;
       }
     
       return false;
    }
   
    /**
     * using recursion
     */
    public static String reverse(String inputValue){
        if( inputValue == null || inputValue.isEmpty()){
            return inputValue;
        }
       
        return inputValue.charAt(inputValue .length()- 1) + reverse(inputValue .substring(0,        inputValue.length() - 1));
    }
   
}

Output
Is zzz palindrome?: true
Is ashi palindrome?: false

Q4 Find the Factorial of the Number?
Ans:

/**
 * Simple Java program to find the factorial of a number using recursion and by a loop.
 */
public class FactorialInJava{

  public static void main(String args[]) {
     
      //using recursion 
      System.out.println("factorial of 7 using recursion is: " + factorialRecur(7));
     
      //using Iteration 
       System.out.println("factorial of 7 using iteration is: " + factLoop(7));
    }
 
 
    /*
     * Java program get factorial of a number using recursion method
     * @return factorial of number
     */
    public static int factorialRecur(int num){      
        //base case
        if( num == 0){
            return 1;
        }
        return num*factorialRecur(num -1); 
    }
 
    /*
     * Java program  to get factorial using while loop or iteration
     * @return factorial of a number
     */
 
    public static int factLoop(int num){
        int value = 1;
        while(num != 0){
            value = value*num;
            num--;
        }
     
        return result;
    }
}

Q5 Write a java program to reverse the String?
Ans:

/**
 * Java program to reverse String in Java.
 * @author Ashish
 */
public class ReverseString {

    public static void main(String args[]) {
    String word = "HelloWorld";
    public static String reverse(String word){
        if( word == null || word.isEmpty()){
            return word;
        }      
        String reverse = "";
        for(int i = word.length() -1; i>=0; i--){
            reverse = reverse + word.charAt(i);
        }
     
        return reverse;
    }
   
}

Output:
original String: HelloWorld, reversed String dlroWolleH

Q6. Write a program to remove the repeated elements from ArrayList in Java.
Ans:

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;


/**
 * Java Program to remove repeated elements from ArrayList in Java.
 * @author Ashish
 */

public class ArrayListDuplicate{
    public static void main(String args[]){

        // This will create the  ArrayList with duplicate elements
        List<Integer> list = new ArrayList<Integer>();
       
        list.add(2);
        list.add(3);
        list.add(5);
        list.add(7);  //duplicate
        list.add(7);
        list.add(11);
       
        // This will print all the elements with duplicate data.
        System.out.println("list of  numbers : " + list);
       
        // Now let's remove duplicate element without affecting the order
        // LinkedHashSet will guarantee the order (this will not affect the order) and since it's set
        // it will not allow us to insert duplicates.
        // repeated elements will automatically be filtered.
       
        Set<Integer> dataWithoutDuplicates = new LinkedHashSet<Integer>(list);
       
        // now let's clear the ArrayList so that we can copy all elements from LinkedHashSet
        list.clear();
       
        // copying elements but without any duplicates
        list.addAll(dataWithoutDuplicates);
       
     System.out.println("list of data without duplicates : " + list);
       
    }
 
}

Output

list of numbers : [2, 3, 5, 7, 7, 11]
list of data without duplicates: [2, 3, 5, 7, 11]

Q7Write a program to find the square root of a number.
Ans:

import java.util.Scanner;

/**
 *
 * Java program to find square root of number.
 * In thisprogram we will use math class
* sqrt() method to get square root of a number
 *
 * @author ashish
 */
public class SquareRootDemo{

    public static void main(String args[]) {
     
        //
        Scanner value = new Scanner(System.in);
     
        System.out.println("Enter number to find square root ");
     
        //getting input number from user to calculate square root
        double squareValue= value.nextDouble();
     
     
        //getting square root of a number in Java
        double squareRootValue = Math.sqrt(squareValue);
     
        //printing number and its square root in Java
        System.out.printf("Square root of number” + squareValue+”is”+squareRootValue);
   
    }
 
 
}

Output:

Enter number to find square root
64
Square root of number: 64.000000 is : 8.000000

Q8. Write a java program to reverse a number?
Ans:

import java.util.Scanner;

/**
 * Java program to reverse a number
*  using division operator(/) and Remainder Operator(%)
* @author Ashish
 */
public class ReverseNumberDemo{

    public static void main(String args[]) {
       //Enter the number to reverse a number.
        System.out.println("Please enter number to be reversed:");
        int numberValue = new Scanner(System.in).nextInt();
     
        int reverseValue = reverseNumber(numberValue);
        System.out.println("Reverse of number: " + number + " is " + reverseValue(numberValue));  
   
    }
 
    /*
     * reverse a number  method
     * @return reverse of number
     */
    public static int reverseNumber(int numberValue){
        int reverseValue = 0;
        int remainderValue = 0;
        do{
            remainderValue = numberValue%10;
            reverseValue = reverseValue*10 + remainderValue;
            numberValue = numberValue/10;
            }while(numberValue > 0);
              return reverseValue;
    }
}

Output:

Please enter number to be reversed
98765
Reverse of number: 98765 is 56789

That’s all about Java coding interview of core Java Interview. I have covered Commonly Asked Java Programs for Coding Interview as per my knowledge for both freshers and experienced, so prepare well for the Java coding interview, I suggest you take a look on more Java Programming Interview Questions with answers on different topics of the core java posted in this xadmin website. Keep Learning!  All the Best!