Select chapter



Java programs (2.papers)

Sponsored Links


  

common programs for interviews interview questions and answers

1
Reverse elements of an array:
Write a program to generate random numbers and store it into integer array. After that reverse all the elements with in the same array.
For eg: if array elements are {1,2,3,4,5}. After reversing array should look like {5,4,3,2,1}.
  1. public class ArrayReverse {
        public static void main(String[] args) {
            int[] arr = new int[4];
            for (int k = 0; k < arr.length; k++) {
                arr[k] = (int) (Math.random() * 100);
                System.out.println(arr[k]);
             }
             System.out.print("----------");
             System.out.println();
             for (int i = 0; i < arr.length / 2; i++) {
                 int temp = arr[i];
                 arr[i] = arr[arr.length - 1 - i];
                 arr[arr.length - 1 - i] = temp;
             }
             for (int j = 0; j < arr.length; j++) {
                 System.out.println(arr[j]);
             }
         }
    }
Show Answer
Share Facebook gmail Twitter
2
Write a program to find the common elements between two given arrays.
  1. public class CommonElementsInArray {
     
        public static void main(String a[]){
            int[] arr1 = {4,7,3,9,2};
            int[] arr2 = {3,2,12,9,40,32,4};
            for(int i=0;i<arr1.length;i++){
                for(int j=0;j<arr2.length;j++){
                    if(arr1[i]==arr2[j]){
                        System.out.println(arr1[i]);
                    }
                }
            }
        }
    }
    
Show Answer
Share Facebook gmail Twitter
3
Return the String between the braces :
eg: String str1=abc(xyz)ijk;
Write the logic to return the string between the braces.
  1. public class Main {
    	public static void main(String[] args) {
    		String str="abiffff(jkllllllllK)l";
    		int i=str.indexOf("(");
    		int j=str.indexOf(")");
    		str=str.substring(++i,j);
    		System.out.println(str);
    	}
    }
Show Answer
Share Facebook gmail Twitter
4
Reverse a string:
Write a program to reverse a string without using built in string functions.
  1. public class ReverseString {
    	public static void main()
    	{
    		String original,reverse="";
    		Scanner in= new Scanner(System.in);
    		System.out.println("Enter a string to reverse");
    		original=in.nextLine();
    		int length=original.length();
    		for(int i=(length-1);i>=0;i--)
    		{
    			reverse=reverse+original.charAt(i);
    		}
    		System.out.println("Reversed String is="+reverse);
    	}
    }
Show Answer
Share Facebook gmail Twitter
5
String to lowercase:
Write a program to convert the character array to Lower Case and print it ?
  1. public class AsciiValue {
    	public static void main(String[] args) {
    		char ch[]={'P','A','l','L','e'};
    		int length=ch.length;
    		for(int i=0;i<length;i++)
    		{
    			int j=ch[i];
    			if(j<=91 && j>=65)
    			{
    				j=j+32;
    				ch[i]=(char) j;
    			}
    		}
    		System.out.println(ch);
    	}
    }
Show Answer
Share Facebook gmail Twitter
6
Remove white spaces in a string:
Write a program to remove all the white spaces in a give string?
Suppose if str=”abc  d m vvv” then output should be “abcdmvvv”
  1. public class RemoveSpaces {
    	public static void main(String[] args) {
    		String str="a. bcd v vvv";
    		str=str.replaceAll(" ","");
    		System.out.println(str);
    	}
    }
Show Answer
Share Facebook gmail Twitter
7
Write a program to find the common characters between two strings.
  1. public class StringCompare {
    	public static void main(String[] args) {
    		String str1="abcdgggg";
    		String str2="aaabgg";
    		Set<String> set=new HashSet<String>();
    		for(int i=0;i<str1.length();i++)
    		{
    			for(int j=0;j<str2.length();j++)
    			{
    				if(str1.charAt(i)==str2.charAt(j))
    				{
    					set.add(str1.charAt(i)+"");
    				}
    			}
    		}
    		System.out.println(set);
    	}
    }
Show Answer
Share Facebook gmail Twitter
8
Even or odd program:
Write a program to check if a number is even or odd ?
  1. public class CheckEvenOdd{
    	public static void main(String[] args) {
    		System.out.println("Enter the number which you want to check");
    		Scanner scan=new Scanner(System.in);
    		int num=Integer.parseInt(scan.next());
    		if(num%2==0)
    			System.out.println("Number is even: ”+num);
    		else
    			System.out.println("Number is odd: ”+num);
    	}
    }
Show Answer
Share Facebook gmail Twitter
9
Prime number program:
Write a program to find out if a number is prime number or not ?
  1. class CheckPrime
    {
    	public static void main(String args[])
    	{  
    		int n, i;
    		Boolean b=false;
    		Scanner scan= new Scanner(System.in);
    		System.out.println("Please Enter a No.");
    		n=scan.nextInt();
    		for(i=2;i<=n/2;i++)
    		{
    			if(n%i==0)
    			{
    				b=true;
    				break;
    			}
    		}
    		if(n==0||n==1)
    		{
    			System.out.println(n +" is not a prime no"); 
    		}
    		else
    		{
    			if(b==false)
    			{
    				System.out.println(n +" is a prime no");
    			}
    			else
    			{
    				System.out.println(n +" is a not prime no"); 
    			}
    		}
    	}
    }
Show Answer
Share Facebook gmail Twitter
10
Palindrome program:
Write a program to check if a number is palindrome?
  1. public class Test {
    	public static void main(String[] args) {
    		System.out.println("Please Enter a number : ");
    		int original = new Scanner(System.in).nextInt();
    		int duplicate=original;
    		int reverse = 0;
    		while (original != 0) {
    			int remainder = original % 10;
    			reverse = reverse * 10 + remainder;
    			original = original / 10;
    		}
    
    		if (duplicate == reverse) {
    			System.out.println(duplicate+“ is palindrome”);
    		}
    		else{
    			System.out.println(duplicate+“ is not palindrome”);  
    		}
    	}
    }
Show Answer
Share Facebook gmail Twitter
11
Fibonacci series:
Write a program to print Fibonacci series up to given number?
  1. public class Fibonacci {
    	public static void main(String[] args) {
    		int no=0;
    		Scanner scan= new Scanner(System.in);
    		System.out.println("Please Enter a No.");
    		no=scan.nextInt();
    		int fib[]=new int[no];
    		fib[0]=0;
    		fib[1]=1;
    		for(int i=2;i<no;i++)
    		{
    			fib[i]=fib[i-1]+fib[i-2];
    		}
    		for(int index:fib)
    		{
    			System.out.print(index+" ");
    		}
    	}
    }
Show Answer
Share Facebook gmail Twitter
12
Factorial program:
Write a program to calculate factorial of an integer number ?
  1. public class Factorial {
    	public static void main(String[] args) {
    		int no,fact=1;
    		Scanner scan= new Scanner(System.in);
    		System.out.println("Please Enter a No.");
    		no=scan.nextInt();
    		for(int i=1;i<=no;i++)
    		{
    			fact=fact*i;
    		}
    		System.out.println(fact);
    	}
    }
Show Answer
Share Facebook gmail Twitter
13
Selection sort program : 
Write the logic to implement selection sort ?
  1. public class MySelectionSort {
    	public static int[] doSelectionSort(int[] arr){
    
    		for (int i = 0; i < arr.length - 1; i++)
    		{
    			int index = i;
    			for (int j = i + 1; j < arr.length; j++)
    				if (arr[j] < arr[index]) 
    					index = j;
    
    			int smallerNumber = arr[index];  
    			arr[index] = arr[i];
    			arr[i] = smallerNumber;
    		}
    		return arr;
    	}
    	public static void main(String a[]){
    		int[] arr1 = {10,34,2,56,7,67,88,42};
    		int[] arr2 = doSelectionSort(arr1);
    		for(int i:arr2){
    			System.out.print(i);
    			System.out.print(", ");
    		}
    	}
    }
Show Answer
Share Facebook gmail Twitter
14
Insertion sort program:
Write the logic to implement insertion sort ?
  1. public class MyInsertionSort {
    	public static void main(String a[]){
    		int[] arr1 = {10,34,2,56,7,67,88,42};
    		int[] arr2 = doInsertionSort(arr1);
    		for(int i:arr2){
    			System.out.print(i);
    			System.out.print(", ");
    		}
    	}
    
    	public static int[] doInsertionSort(int[] input){
    
    		int temp;
    		for (int i = 1; i < input.length; i++) {
    			for(int j = i ; j > 0 ; j--){
    				if(input[j] < input[j-1]){
    					temp = input[j];
    					input[j] = input[j-1];
    					input[j-1] = temp;
    				}
    			}
    		}
    		return input;
    	}
    }
Show Answer
Share Facebook gmail Twitter
15
Merge sort program:
Write the logic to implement merge sort ?
  1. public class MyMergeSort {
    	private int[] array;
    	private int[] tempMergArr;
    	private int length;
    	public static void main(String a[]){
    		int[] inputArr = {45,23,11,89,77,98,4,28,65,43};
    		MyMergeSort mms = new MyMergeSort();
    		mms.sort(inputArr);
    		for(int i:inputArr){
    			System.out.print(i);
    			System.out.print(" ");
    		}
    	}
    
    	public void sort(int inputArr[]) {
    		this.array = inputArr;
    		this.length = inputArr.length;
    		this.tempMergArr = new int[length];
    		doMergeSort(0, length - 1);
    	}
    
    	private void doMergeSort(int lowerIndex, int higherIndex) {
    
    		if (lowerIndex < higherIndex) {
    			int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
    			// Below step sorts the left side of the array
    			doMergeSort(lowerIndex, middle);
    			// Below step sorts the right side of the array
    			doMergeSort(middle + 1, higherIndex);
    			// Now merge both sides
    			mergeParts(lowerIndex, middle, higherIndex);
    		}
    	}
    
    	private void mergeParts(int lowerIndex, int middle, int higherIndex) {
    
    		for (int i = lowerIndex; i <= higherIndex; i++) {
    			tempMergArr[i] = array[i];
    		}
    		int i = lowerIndex;
    		int j = middle + 1;
    		int k = lowerIndex;
    		while (i <= middle && j <= higherIndex) {
    			if (tempMergArr[i] <= tempMergArr[j]) {
    				array[k] = tempMergArr[i];
    				i++;
    			} else {
    				array[k] = tempMergArr[j];
    				j++;
    			}
    			k++;
    		}
    		while (i <= middle) {
    			array[k] = tempMergArr[i];
    			k++;
    			i++;
    		}
    	}
    }
Show Answer
Share Facebook gmail Twitter
16
Bubble sort program:
Write the logic to implement bubble sort ?
  1. public class BubbleSort {
    	public static  void main(String[] args) {
    		int[] arr={12,23,43,34,3,6,7,1,9,6};
    
    		int n =arr.length;
    		int temp = 0;
    
    		for(int i=0; i < n; i++){
    			for(int j=1; j < (n-i); j++){
    
    				if(arr[j-1] > arr[j]){
    					//swap the elements!
    					temp = arr[j-1];
    					arr[j-1] = arr[j];
    					arr[j] = temp;
    				}
    			}
    		}
    		for(int k=0;k<arr.length;k++)
    		{
    			System.out.println(arr[k]);
    		}
    	}
    }
Show Answer
Share Facebook gmail Twitter
17
Assume DrawLine(x1,y1,x2,y2) function will draw a line from (x1,y1) to (x2,y2) . Using this write only the logic for regular scale.
(Fx,Fy) is the origin.
g is the gap between two lines.
BL is the length of bigger line.   
SL is the length of smaller line.
  1. public class LineExample extends java.applet.Applet
    {
    	@Override
    	public void paint(Graphics arg0) {
    		int fx=40;
    		int fy=50;
    		int bl=10;
    		int sl=5;
    		int g=5;
    
    		for(int i=0;i<15;i++)
    		{
    			arg0.drawLine(fx,fy,fx,fy+bl);
    			for(int j=0;j<4;j++)
    			{
    				fx=fx+g;
    				arg0.drawLine(fx,fy,fx,fy+sl);
    			}
    			fx=fx+g;
    		}
    	}
    	public static void main(String[] args)
    	{
    		LineExample le=new LineExample();
    		Graphics g=null;
    		le.paint(g);
    	}
    }
Show Answer
Share Facebook gmail Twitter
18
Write a  program to display a number in words
Eg: 270176
OUTPUT : TWO SEVEN ZERO ONE SEVEN SIX
  1. public class NumbersToWords {
    	public static void main(String[] args) {
    		int arr[]={2,7,0,1,7,6};
    		for(int i=0;i<arr.length;i++)
    		{
    			switch (arr[i]) {
    			case 0:
    				System.out.print("ZERO ");
    				break;
    			case 1:
    				System.out.print("ONE ");
    				break;
    			case 2:
    				System.out.print("TWO ");
    				break;
    			case 3:
    				System.out.print("THREE ");
    				break;
    			case 4:
    				System.out.print("FOUR ");
    				break;
    			case 5:
    				System.out.print("FIVE ");
    				break;
    			case 6:
    				System.out.print("SIX ");
    				break;
    			case 7:
    				System.out.print("SEVEN ");
    				break;
    			case 8:
    				System.out.print("EIGHT ");
    				break;
    			case 9:
    				System.out.print("NINE "); 
    				break;
    			default:
    				break;
    			}
    		}
    	}
    }
Show Answer
Share Facebook gmail Twitter
19
Decimal to binary conversion program:
Write a  program to accept a decimal number and display it in the binary form.
  1. public class DecimalToBinary {
    	public void printBinaryFormat(int number){
    		int binary[] = new int[25];
    		int index = 0;
    		while(number > 0){
    			binary[index++] = number%2;
    			number = number/2;
    		}
    		for(int i = index-1;i >= 0;i--){
    			System.out.print(binary[i]);
    		}
    	}
    
    	public static void main(String a[]){
    		DecimalToBinary dtb = new DecimalToBinary();
    		dtb.printBinaryFormat(4);
    	}
    }
Show Answer
Share Facebook gmail Twitter
20
Binary to decimal conversion program:
Write a program to accept a binary number and display it in the binary form.
  1. public class BinaryToDecimal {
    	public int getDecimalFromBinary(int binary){
    		int decimal = 0;
    		int power = 0;
    		while(true){
    			if(binary == 0){
    				break;
    			} else {
    				int tmp = binary%10;
    				decimal += tmp*Math.pow(2, power);
    				binary = binary/10;
    				power++;
    			}
    		}
    		return decimal;
    	}
    	
    	public static void main(String a[]){
    		BinaryToDecimal bd = new BinaryToDecimal();
    		System.out.println("11 ===> "+bd.getDecimalFromBinary(11));
    		System.out.println("110 ===> "+bd.getDecimalFromBinary(110));
    		System.out.println("100110 ===> "+bd.getDecimalFromBinary(100110));
    	}
    }
Show Answer
Share Facebook gmail Twitter
Read more papers :