Frequently Asked Programming and Coding Interview questions answers in Java

Here is my list of top Java coding interview questions and answers, which is good to prepare before appearing on any Java interviews. As I said Java coding questions are mostly based on programming, logical analysis and problem solving skill and are on top of any list of  tough Java interview questions, so better to get it right in first place. Any way you may be able to solve and find answers of these Java coding questions by yourself, but if you stuck do a google, and you can get many alternative ways to solve these problems. Some times knowing more than one way to solve any programming question or coding problem in Java also helps to impress interviewer. This list mainly contains basic programs asked on Interviews.
Frequently Asked Programming and Coding Interview questions answers in Java

1. Write a Java program to replace certain characters from String

This is a tricky Java coding interview question is asked in one of the written test my friend had appeared recently. This Java coding question can be solved in multiple way e.g. by using charAt() or subString() method,  but any approach throws couple of follow-up question e.g. you may be asked to write two version to solve this coding exercise, one by using recursion and other by using Iteration. They may also ask you to write JUnit test for this function which means handling null, empty string etc. By the way this programming question is quite common on technical interviews not just Java but also C, C++ or Scala, but knowing API definitely helps to produce better solution quickly.

2. Write a Java program to print Fibonacci series upto 100?

This is one of the most popular coding interview question asked in Java programming language. Even though, writing program for Fibonacci series is one of the basic Java program, not every Java developer get it right in interview. Again interview can ask to solve this programming interview question, by using recursion or Iteration. This Java programming question also tests your problem solving skills and if you come up with an original solution that may even help. See here for complete code example of Fibonacci series in Java.

3. FizzBuzz problem:

Write a Java program that prints the numbers from 1 to 50. But for multiples of three print "Fizz"instead of the number and for the multiples of five prints "Buzz". For numbers which are multiples of both three and five print "FizzBuzz"
This is also one of the classical programming questions, which is asked on any Java programming or technical interviews. This question is very basic but can be very trick for programmers, who can't code, that's why it is used to differentiate programmers who can do coding and who can't. Here is a sample Java program to solve FizzBuzz problem:
public class FizzBuzzTest{
    public static void main(String args[]){
        for(int i = 1; i <= 50; i++) {
            if(i % (3*5) == 0) System.out.println("FizzBuzz");
            else if(i % 5 == 0) System.out.println("Buzz");
            else if(i % 3 == 0) System.out.println("Fizz");
            else System.out.println(i);

4. Write a Comparator in Java to compare two employees based upon there name, departments and age?

This is pure Java based Coding exercise. In order to solve this Java coding or programming interview question you need to know What is comparator in Java and How to use compare method in Java for sorting Object. Sorting is one of the most logical and practical question on technical interview and ability to sort Java object is must to code in Java.  This article help you to solve this Java coding question by explaining how to sort object in Java using Comparable and Comparator. Just remember that Comparable has compareTo() method and use to sort object based upon there natural order e.g. numeric order for number, and alphabetic order for String, while Comparator can define any arbitrary sorting. A good followup question can also be difference between Comparator and Comparable in Java, so be ready for that.

5. Design vending machine in Java which vends Item based upon four denomination of coins and return coin if there is no Item.

This kind of Java coding interview question appear in written test and I believe if you get it right, you are almost through the Interview. These kind of problem solving questions in Java are not easy, you need to design , developer and write JUnit test within 2 to 3 hours and only good Java developers, with practical coding experience can solve this kind of Java programming question. What helps you is to keep practicing your coding skill even before interview. See this programming exercise in Java to get yourself going. I personally like to ask programming questions, which test your object oriented design skills e.g. designing ATM machine, designing parking lot or implementing logic for Traffic Signal controller.

6. Write a Java program

Another popular logical coding interview questions in Java, which is based on programming logic. In order to answer this programming question, you need to know what is Armstrong number, but that is not a problem because question may specify that and even provide sample input and output. Key thing to demonstrate is logic to check if a number is Armstrongor not. In most cases, you can not use utility methods defined by logic and you need to produce logic by yourself by using basic operators and methods. By the way this is also one of the basic programming questions and I have already provided asolution for this. I suggest to see this Java program to find Armstrong Number in Java to answer this coding question.

7. Write a Java program to prevent deadlock in Java ?

Some of the programming or coding interview question is always based on fundamental feature of Java programming language e.g. multi-threading, synchronization etc. Since writing deadlock proof code is important for a Java developer, programming questions which requires knowledge of concurrency constructs becomes popular coding question asked in Java Interviews. Deadlock happens if four condition is true e.g. mutual exclusion, no waiting, circular wait and no preemption. If you can break any of this condition than you can create Java programs,which are deadlock proof. One easy way to avoid deadlock is by imposing an ordering on acquisition and release of locks. You can further check How to fix deadlock in Java to answer this Java programming questions with coding in Java.

8. Write Java program to reverse String in Java without using API functions ?

Another classic Java programming or coding exercise mostly asked on 2 to 5 years experienced Java interviews. Despite being simple answering this coding question is not easy, specially if you are not coding frequently. Its best to prepare this programming question in advance to avoid any embarrassment during interviews. I suggest to see this post which shows How to reverse String using recursion in Java

9. Write a Java program to find if a number is prime number or not

One more basic Java program, which made it's way to Interviews. One of the simplest coding question and also a very popular Java programming exercise. Beauty of these kinds of logical questions is that, they can really test basic programming skills or a coder, programmer or developer. Not just problem solving, you can also check there coding style and thought process. By the way. you can  check this article.

10. How to Swap two numbers without using third variable in Java?

This Java program might require just four lines to code, but it's worth preparing. Most of the programmers make same kind of mistakes, while writing solution for this program e.g. Integer overflow, they tend to forget that integer can overflow if it's limit exceeded, which is not very big. Sure shot way to answer this programming questions is to use XOR trick to swap numbers, as mentioned in that blog post.

11. Write a Java program to calculate Factorial of a number in Java?

This Java coding interview questions is also based on list of basic Java programs for beginners. As usual, you better remember how to calculate factorial and how to code solution using loop and recursive method calls. For complete code solution of this programming question, see Java program to calculate factorial.