## Frequently asked Java coding interview questions and answers

In this post we will discuss some of the frequently asked Java coding interview questions and answers for freshers. These questions may also be asked for experience level up to 2 years. While attending a Java interview, the interviewer will not only test the understanding of basic concepts but also will test your ability to write programs. So it is important that you be thorough with the logic for some of the common problems. If you didn’t prepare for it, then it may be tough to get the logic on the spot. Need more presence of mind.

### Write a program to find if a given number is an Armstrong number.

Armstrong number is a number which is equal to sum of digits raised to the power of total number of digits in the number. Example: **371 = 3^3 + 7^3 + 1^3**;

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 | public class ArmStrongNumber { public static void main(String[] args) { int number = 371; int digits = String.valueOf(number).length(); int sum = 0; int temp = number; while (temp > 0) { sum = sum + power(temp % 10, digits); temp = temp / 10; } if (sum == number) { System.out.println(number + " is an Armstrong number"); } else { System.out.println(number + " is not an Armstrong number"); } } private static int power(int num, int digits) { int power = 1; for (int i = 1; i <= digits; i++) { power = power * num; } return power; } } |

### Write a program to find if a given number is a Palindrome number.

A *palindrome number* is a number whose reverse is also the same. e.g. 121, 212 etc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class PalindromeNumber { public static void main(String[] args) { int remainder, sum = 0, temp; int n = 454; // It is the number to be checked for palindrome temp = n; while (n > 0) { remainder = n % 10; // getting remainder sum = (sum * 10) + remainder; n = n / 10; } if (temp == sum) { System.out.println("palindrome number "); } else { System.out.println("not palindrome"); } } } |

Another version of program using string reverse.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class PalindromeNumber { /** * @param args */ public static void main(String[] args) { int num = 212; String s = new StringBuilder(String.valueOf(num)).reverse().toString(); int rev = Integer.valueOf(s); if (num == rev) { System.out.println(num + " is a Palindrome Number"); } else { System.out.println(num + " is not a Palindrome Number"); } } } |

### Write a program to find if a given string is a Palindrome.

A string is said to be palindrome if its reverse is also same.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class PalindromeString { /** * @param args */ public static void main(String[] args) { String s = "madam"; String reverse = new StringBuilder(s).reverse().toString(); if (s.equals(reverse)) { System.out.println(s + " is a palindrome"); } else { System.out.println(s + " is not a palindrome"); } } } |

Another way to solve the problem.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public class PalindormeString { public static void main(String[] args) { String s = "madam"; System.out.println(s + " is a palindrome: " + istPalindrome(s.toCharArray())); } private static boolean istPalindrome(char[] word) { int i1 = 0; int i2 = word.length - 1; while (i2 > i1) { if (word[i1] != word[i2]) { return false; } i1++; i2--; } return true; } } |

### Write a program to find length of a string without using inbuilt functions.

In this program we are expected to find the length of the given string without using inbuilt functions like length() etc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class StringLength { /** * @param args */ public static void main(String[] args) { String str = "Hello"; int length = 0; for (char c : str.toCharArray()) { length++; } System.out.println("Length of String " + str + " is " + length); } } |

### Write a program to print Fibonacci series of N numbers.

In mathematics, the **Fibonacci numbers** or **Fibonacci series** or **Fibonacci sequence** are the numbers in the following sequence:

By definition, the first two numbers in the Fibonacci series are 0 and 1, and each subsequent number is the sum of the previous two.

In mathematical terms, the sequence *F _{n}* of Fibonacci numbers is defined by the recurrence relation

- with values

Now let us see the program using recursion.

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 | import java.util.Scanner; public class FibonacciSeries { /** * @param args */ public static void main(String[] args) { System.out .println("Enter number of elements to print in Fibonacci series"); Scanner s = new Scanner(System.in); int number = s.nextInt(); s.close(); for (int i = 0; i < number; i++) { System.out.print(fibanocci(i) + " "); } } private static int fibanocci(int n) { if (n == 0) { return 0; } else if (n == 1 || n == 2) { return 1; } else { return fibanocci(n - 1) + fibanocci(n - 2); } } } Output: Enter number of elements to print in Fibonacci series 15 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 |

I hope that you find these list of programs useful. We will see some more programs in upcoming posts. It is important that you get accustomed with two or more logic for each program because sometimes the interviewer may ask you to solve the program without using inbuilt functions.

Also read my posts on Frequently asked Java interview questions and answers.

#### Latest posts by Uday (see all)

- How to run shell commands using SSH connection in Java - July 26, 2017
- Java program to print rectangle of stars - July 20, 2017
- Introduction to Java Enum data type with example programs - March 31, 2017