Saturday, August 18, 2012

Top 10 Java Programming Coding Interview Questions Answers for programmers

Java coding interview questions answers
Java coding interview questions are mostly some programming exercises which is asked to be solved in java programming language in limited time during any core Java or J2EE interview. No matter whether you have 2 years of experience or 4 years of experience, There is always some coding interview question in Java programming job you are applying. Actually questions related to Java Coding are increasingly getting popular on Java interviews for two reasons, first its difficult to crack Java coding interview questions than answering fact based questions like Why String is immutable in Java or Why main is static in Java; Second reason of popularity of Coding question in Java interviews is read need of good developers who are required to do lot of coding in projects rather than doing some maintenance works. Since Java is front line language for any server side application, and as complexity of business process and need of performance is increasing,  its obvious that amount of coding skill required in Java programmers are getting increased with every passing days, which effectively mean Java coding questions are top of any list of core Java interview question. These Coding interview questions are collected from various Java programming interviews, from friends and colleagues and can be a good starting point to refresh your coding skills before appearing on any Java interviews. These basics Java Programs and logical questions can also be a good resources for learning programming and to improve your problem solving skills in Java.


10 Programming and Coding Interview questions answers in Java

Java Coding Interview Question and Answers
Here are my list of 10 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 problem. 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.



Write a Java program to replace certain characters from String like
public String replace(String str, char ch)
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.


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 test 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

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 print "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 questions 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);
        } 
    } 

}

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.


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.

Write a Java program to check if a number is Armstrong or not ?
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 Armstrong or 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 a solution for this. I suggest to see this Java program to find Armstrong Number in Java   to answer this coding question

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

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

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 for answer of this Java coding interview question.


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.

Create a Java program to find middle node of linked list in Java in one pass?
Any list of programming questions is incomplete without any questions from linked list, arrays and string, these three forms bulk of coding questions asked on Java interviews. Trick to solve this problem is to remember that last node of linked list points to null and you can trade memory with speed. Sometime your approach to come to two pointer solution really matters, by taking rational steps as mentioned above, you can sound more intelligent, problem solver and genuine. Quick solution of this programming question can be found here.

How to find if a linked list contains cycle or not in Java?
Another programming question based on linked list. By the way this coding question is bit tricky than previous one, but this can also be solved using two pointer approach. If linked list has cycle, than fast pointer will either catch slow pointer or point to null. See Java program to check if linked list contains loop in Java  for complete solution of this coding interview question.

Implement Producer Consumer design Pattern in Java using wait, notify and notifyAll method in Java?
Similar to deadlock related programming interview question, this is also used to test programmers ability to write bug free concurrent programs in Java. This coding questions can be difficult if you haven't used wait and notify before, you can confuse yourself as hell on different aspect e.g. which condition to check, on which lock you should synchronized etc. I suggest following here to answer this multithreading based programming interview question.

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

These are some of the Java coding interview questions and answers, which appears frequently on Java Programming interviews. I have included links, with some of my blog posts, which discusses answers of these Java coding question, but you can also find answers by doing google yourself. Please share  what kind of Programming, logical, Problem solving or coding related questions, asked to you in Java interviews?

Other Interview questions from Java67 blog
10 tricky Java interview questions - Answered
10 Linux and UNIX interview questions - Answered
Best Java Collection interview questions answers for practice

 

14 comments:

  1. Good collection of Coding and programming question in Java. Surprised not to see Generics, Data structure's, LinkedList etc

    ReplyDelete
  2. Here is my list of Java programming interview question :

    Write a Java program to create Thread and Stop Thread (remember there is no stop method for stopping thread in Java)
    Write a Java program to sort Map based on keys without using TreeMap
    Write a Java program to create deadlock ? how do you fix it ?
    Write a Java program to find maximum number in Array

    I agree that these Java programming question are basic but in my opinion you need to be better than average programmer to be able to write code which does that correctly.

    ReplyDelete
    Replies
    1. How to use the java program to basic

      Delete
    2. Basic concept ask to me plz

      Delete
  3. very nice collection of information and improve them.

    ReplyDelete
  4. Re question "Write a Java program to replace certain characters from String" seems like the example method is missing a replaceWith parameter no?

    public String replace(String str, char old, char new)

    ReplyDelete
  5. thanks for the questions...they really helped....plus your blog is awesome for an aspiring programmer like me..keep it up...:)

    ReplyDelete
  6. This is the coding question asked to me in couple of interviews :

    1) How to remove duplicates from an array of Integers, without using API methods in Java?

    2) Code to implement binary search in Java?

    let me know, if anyone has answers for these questions?

    ReplyDelete
  7. Some of these questions have appeared in a written test on Java programming, I had given few months back. I would personally like to see more questions from class loading involving static and non static variables, questions which are confusing enough on overloading and overriding, questions which has some tricky concept on it etc. Though these are still good for beginners, you would like to add few more for experienced Java programmers.

    ReplyDelete
  8. This was a coding question which i was asked

    Write a program, to find a missing number in array of 100 numbers

    ReplyDelete
    Replies
    1. apply the formula sum of n numbers n * (n+1) /2
      suppose number 8 is missing from the array 1..10.
      Sum of 10 numbers : 10* 11/2 = 55
      Sum of numbers from array = 47
      missing number = 55-47 =8

      Delete
  9. Write a program in java to change number into words
    for example 2341 into Two Thousand Three Hundred Fourthy One

    ReplyDelete
  10. Find the count of number in list? ex: 1,1,1,1,2,2,3,3,4,4,4,4,4
    o/p: 1-4,2-2,3-2,4-5

    ReplyDelete

Java67 Headline Animator