In this tutorial, we will see Java interview programs for logic building. These java programs will help students to crack Java interview.
Here is the list of Top 10 Java interview programs for logic building.
Question 1: Check if number is odd or even?
Answer:
It is a very basic question. You need to check remainder value when you divide number by 2. If it is 1 then it is odd else it is even.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
package com.arpit.java2blog; public class OddEvenCheckMain { public static void main(String[] args) { int number=27; if ( number % 2 == 0 ) { // If remainder is 0, it is even number System.out.println("Number is even"); } else { // If remainder is 1, it is odd number System.out.println("Number is odd."); } } } |
When you run above program, you will get below output:
Number is odd.
Question 2: How to check number is prime or not?
Answer:
If any number which is not divisible by any other number which is less than or equal to square root of that number, then it is prime number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
package org.arpit.java2blog; public class PrimeNumberMain { public static void main(String[] args) { System.out.println("17 is prime number?: "+isPrime(17)); System.out.println("2 is prime number?: "+isPrime(2)); System.out.println("91 is prime number?: "+isPrime(91)); System.out.println("29 is prime number?: "+isPrime(29)); System.out.println("81 is prime number?: "+isPrime(81)); } public static boolean isPrime(int number) { for (int i = 2; i <=Math.sqrt(number); i++) { if(number%i==0) return false; } return true; } } |
When you run above program, you will get below output:
17 is prime number?: true
2 is prime number?: true
91 is prime number?: false
29 is prime number?: true
81 is prime number?: false
If you can go through check if number is prime for more details.
Question 3: Reverse a String:
Answer:
|
package com.arpit.java2blog; class ReverseStringForMain { public static void main(String[] args) { String str="Happy"; String reverse=""; for(int i=str.length()-1;i>=0;i--) { reverse=reverse+str.charAt(i); } System.out.println("Reverse of Happy is:"+reverse); } } |
When you run above program, you will get below output:
Reverse of Happy is:yppaH
Question 4: Check If String is palindrome.
Answer:
Reverse a String and check it with input String.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
package org.arpit.java2blog; import java.util.Scanner; public class StringFullLoopPalindrome { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter string : "); String str = scanner.nextLine(); String reverseStr = ""; for(int i = str.length() - 1; i >= 0; i--){ reverseStr = reverseStr + str.charAt(i); } if(str.equals(reverseStr)){ System.out.println("String is palindrome"); } else { System.out.println("String is not palindrome"); } } } |
When you run above program and you will get below output:
Enter string : 12121
String is palindrome
Enter a string : Apple
String is not palindrome
You can go through Check if String is palindrome for more details.
Question 5: Search a number in sorted array in o(logn) time?
Answer:
You need to use binary search to search an element in array in o(logn) time.
Code for binary search
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
package org.arpit.java2blog.thread; public class BinarySerarchMain { public static int binarySearch(int[] sortedArray, int elementToBeSearched) { int first = 0; int last = sortedArray.length - 1; while (first < last) { int mid = (first + last) / 2; // Compute mid point. if (elementToBeSearched < sortedArray[mid]) { last = mid; // repeat search in first half. } else if (elementToBeSearched > sortedArray[mid]) { first = mid + 1; // Repeat sortedArray in last half. } else { return mid; // Found it. return position } } return -1; // Failed to find element } public static void main(String[] args) { int[] sortedArray={2,6,67,96,107,119,128,453}; int indexOfElementToBeSearched=binarySearch(sortedArray,67); System.out.println("Index of 74 in array is: " +indexOfElementToBeSearched); int indexOfElementToBeSearchedNotFound=binarySearch(sortedArray,7); System.out.println("Index of 7 in array is: " +indexOfElementToBeSearchedNotFound); } } |
When you run above program, you will get below output:
Index of 67 in array is: 2
Index of 7 in array is: -1
Question 6: Find missing number in array.
Answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
package org.arpit.java2blog; public class MissingNumberMain { public static void main(String[] args) { int[] arr1={7,5,6,1,4,2}; System.out.println("Missing number from array arr1: "+missingNumber(arr1)); int[] arr2={5,3,1,2}; System.out.println("Missing number from array arr2: "+missingNumber(arr2)); } public static int missingNumber(int[] arr) { int n=arr.length+1; int sum=n*(n+1)/2; int restSum=0; for (int i = 0; i < arr.length; i++) { restSum+=arr[i]; } int missingNumber=sum-restSum; return missingNumber; } } |
When you run above program, you will get below output:
Missing number from array arr1: 3
Missing number from array arr2: 4
You can go through find missing number in array for more details.
Question 7: Java program to find duplicate characters in the String?
Answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
package org.arpit.java2blog; import java.util.HashMap; import java.util.Set; public class StringFindDuplicatesMain { public static void main(String[] args) { String str = "java2blog.com "; HashMap charCountMap = new HashMap(); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (charCountMap.containsKey(c)) { charCountMap.put(c, charCountMap.get(c) + 1); } else { charCountMap.put(c, 1); } } for (Character c : charCountMap.keySet()) { if (charCountMap.get(c) > 1) System.out.println("duplicate character : " + c + " ====== " + " count : " + charCountMap.get(c)); } } } |
When you run above program, you will get below output:
|
duplicate character : a ====== count : 2 duplicate character : o ====== count : 2 |
You can go through find duplicate characters in String for more details.
Question 8: Java program to find number of words in String?
Answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
package com.arpit.java2blog; class CountTheWords { public static void main(String[] args) { String str="My Name is John"; String[] words = str.trim().split(" "); System.out.println("Number of words in the string = "+words.length); System.out.println("Words are:"); for (int i = 0; i < words.length; i++) { System.out.println(words[i]); } } } |
WHen you run above program, you will get below output:
Number of words in the string = 4
Words are:
My
Name
is
John
Question 9: Write a program to find factorial of number?
Answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
package com.arpit.java2blog; import java.util.Scanner; public class Factorial{ public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.println("Enter the number to calculate Factorial: "); int n = in.nextInt(); int f =1; for(int i=n; i>0; i--){ f = f*i; } System.out.println("Factorial of "+n+" is "+f); } } |
When you run above program, you will get below output:
Enter the number to calculate Factorial:
5
Factorial of 5 is 120
Question 10: Write a program to print fibonacci series?
Answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
package org.arpit.java2blog.algo; public class FibonnacciIterativeMain { public static void main(String[] args) { System.out.println("Printing Fibonacci series:"); // first two number of series int prev=0,next=1; //printing first two elements System.out.print(prev+" "+next); //number of elements to be printed int numberOfElements=10; int sum=0; for(int i=2;i<numberOfElements;i++) { sum=prev+next; System.out.print(" "+sum); prev=next; next=sum; } } } |
When you run above program, you will get below output:
Printing Fibonacci series:
0 1 1 2 3 5 8 13 21 34
You can go through Fibonacci series program for more details
that’s all about Java interview programs for logic building.
You may also like:
Java Interview programs
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.
i am fresher and i lot of thing learn from your website ,thank you sir