Java Programming Interview Questions

Hi Folks, In today blog i am going to share basic programming interview question on java which interviewer asking in campus selection process This is one round of 10 questions i am sharing with you. Soon i will post 10 more java programming questions which recently asked in campus interview.

Program 1 : Factorial Program in Java ?

Factorial of n is donated by n!. For example.

4! = 4*3*2*1 = 24

5! = 5*4*3*2*1 = 120

Solution -

public class class Factorial{
    public static void main(String args[]){
        int i,fact=1;
        int number=5;//It is the number to calculate factorial        for(i=1;i<=number;i++){
            fact=fact*i;
        }
        System.out.println("Factorial of "+number+" is: "+fact);
    }
}

Program 2:-  Fibnocci series program in Java ?

In fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 etc.

Solution- 

class FibonacciSeriesExample1{
    public static void main(String args[])
    {
        int n1=0,n2=1,n3,i,count=10;
        System.out.print(n1+" "+n2);//printing 0 and 1    
        for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed            {
            n3=n1+n2;
            System.out.print(" "+n3);
            n1=n2;
            n2=n3;
        }

    }
}  

Problem 3:- Armstrong Number in Java ?

Armstrong Number in Java: Armstrong number is a number that is equal to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407 etc.

153 = (1*1*1)+(5*5*5)+(3*3*3)

where:

(1*1*1)=1

(5*5*5)=125

(3*3*3)=27

So:

1+125+27=153

Solution-

class ArmstrongNumber{
    public static void main(String[] args)  {
        int c=0,a,temp;
        int n=371;//It is the number for which we are checking armstrong        temp=n;
        while(n>0)
        {
            a=n%10;
            n=n/10;
            c=c+(a*a*a);
        }
        if(temp==c)
            System.out.println("armstrong number");
        else            
            System.out.println("Not armstrong number");
    }
}

Problem 4: Palindrome Program in Java ?

A palindrome number is a number that is same after reverse. For example 545, 151, 34543, 343, 171, 48984 are the palindrome numbers.

Solution-

class PalindromeNumber{
    public static void main(String args[]){
        int r,sum=0,temp;
        int n=454;//It is the number variable to be checked for palindrome  
        temp=n;
        while(n>0){
            r=n%10;  //getting remainder              sum=(sum*10)+r;
            n=n/10;
        }
        if(temp==sum)
            System.out.println("true ");
        else            System.out.println("false");
    }
}  

Problem 5:- String Reverse ?

Solution-

public class reverseString {
    public static void main(String[] args) {
        String input="Javatechworld";
        StringBuilder input1 = new StringBuilder();
        input1.append(input);
        input1=input1.reverse();
        for (int i=0;i<input1.length();i++)
            
            System.out.print(input1.charAt(i));
    }
}

Problem 6:- Prime Number ?

Prime number is a number that is greater than 1 and divided by 1 or itself. In other words, prime numbers can't be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17.... are the prime numbers.

Solution-

class PrimeNumber{
    public static void main(String args[]){
        int i,m=0,flag=0;
        int n=17;     //it is the number to be checked          
        m=n/2;
        for(i=2;i<=m;i++){
            if(n%i==0){
                System.out.println("Number is not prime");
                flag=1;
                break;
            }
        }
        if(flag==0)
            System.out.println("Number is prime");
    }
} 

Problem 7:- Java Program to Reverse Array In Place ?

Solution-

import java.util.Arrays;
public class ArrayReversalProgram {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5, 6, 7};
        reverse(numbers);
 }
//reverse the given array in place     
public static void reverse(int[] input) {
        System.out.println("original array : " + Arrays.toString(input));
        // handling null, empty and one element array       
      if(input == null || input.length <= 1){
            return;
        }
        for (int i = 0; i < input.length / 2; i++)
        {
            int temp = input[i];
            // swap numbers            input[i] = input[input.length - 1 - i];
            input[input.length - 1 - i] = temp;
        }
        System.out.println("reversed array : " + Arrays.toString(input));
    }
}

Problem 8:- Determine If Year Is Leap Year Java Example ?

A leap year has 366 days. Algorithm to find a leap year
not a leap year else if year % 4 = 0,
if year % 400 = 0, leap year else if year % 100 = 0,
leap year
else,
not a leap year.

Solution -

public class LeapYearExample {
    public static void main(String[] args) {
        //year we want to check        
        int year = 2004;
        //if year is divisible by 4, it is a leap year
        if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
            System.out.println("Year " + year + " is a leap year");
        else            System.out.println("Year " + year + " is not a leap year");
    }
}

Problem 9:- Check whether string is palindrome or not ?

Solution-

import java.io.*;
import java.util.Scanner;
public class Palindrom {
    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        String s1 = scn.nextLine();
        String temp = s1;
        StringBuffer sb = new StringBuffer(s1);
        sb.reverse();
        s1 = sb.toString();
        if (temp.equalsIgnoreCase(s1)) {
            System.out.println(temp + " is palindrome ");
        }
        else {
            System.out.println(temp+ " is not palindrome");
        }
    }
}

Problem 10:- Java program to remove duplicate words in a string ?

Solution -

/* Java Program to remove duplicate characters from String. */
public class RemoveDuplicateCharacters{ 
public static void main(String args[]) {
    System.out.println("Call removeDuplicates(String word) method ....");
    String[] testdata = {"aabscs", "abcd", "aaaa", null, "", "aaabbb", "abababa"};
    for (String input : testdata) {
        System.out.printf("Input : %s Output: %s %n", input, removeDuplicates(input));
    }
    System.out.println("Calling removeDuplicatesFromString(String str).");
    for (String input : testdata) {
        System.out.printf("Input : %s Output: %s %n",
                input, removeDuplicatesFromString(input));

    }
}
    public static String removeDuplicates(String word) {
        if (word == null || word.length() < 2) {
            return word;
        }
        int pos = 1;
        // possible position of duplicate character        
        char[] characters = word.toCharArray();
        for (int i = 1; i < word.length(); i++) {
            int j; for (j = 0; j < pos; ++j) {
                if (characters[i] == characters[j]) {
                    break;
                }
            }
            if (j == pos) {
                characters[pos] = characters[i]; ++pos;
            } else {
                characters[pos] = 0; ++pos;
            }
        } return toUniqueString(characters);
    } 

    /* This solution assumes that given input String only contains      
ASCII characters. You should ask this question to your Interviewer,     
if he says ASCII then this solution is Ok. This Algorithm
 uses additional memory of constant size. */

    public static String removeDuplicatesFromString(String input) {
        if (input == null || input.length()< 2) {
            return input;
        }
        boolean[] ASCII = new boolean[256];
        char[] characters = input.toCharArray();
        ASCII[input.charAt(0)] = true; int dupIndex = 1;
        for (int i = 1; i<input.length(); i++) {
            if (!ASCII[input.charAt(i)]) {
                characters[dupIndex] = characters[i];
                ++dupIndex; ASCII[characters[i]] = true;
            } else {
                characters[dupIndex] = 0; ++dupIndex;
            }
        } return toUniqueString(characters);
    }

 /* Utility method to convert Character array to String, 
omitting NUL character, ASCII value 0. */
    public static String toUniqueString(char[] letters) {
        StringBuilder sb = new StringBuilder(letters.length);
        for (char c : letters) { if (c != 0) { sb.append(c);
        }
        }
        return sb.toString();
    }
}

 

 

 

Leave a Comment

%d bloggers like this: