Java Interview Programs for Logic Building

Java interview programs

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.

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.
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:

When you run above program, you will get below output:

Reverse of Happy is:yppaH
 You can go through How to reverse a String for more approaches.

Question 4: Check If String is palindrome.

Answer:
Reverse a String and check it with input String.

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

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:

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:

When you run above program, you will get below output:

You can go through find duplicate characters in String for more details.

Question 8: Java program to find number of words in String?

Answer:

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:

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:

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

Was this post helpful?

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *