Top 10 Most Commonly asked Programming Questions in Java

1)What is the difference between String, StringBuilder, and StringBuffer in Java?
The main difference is that String is immutable but both StringBuilder and StringBuffer are mutable. Also, StringBuilder is not synchronized like StringBuffer and that’s why a good faster and should be used for temporary String manipulation.
2)Why is String final in Java? 
The string is final because of same reason it is immutable. A couple of reasons which I think make sense is the implementation of String pool, Security, and Performance. Java designers know that String will be used heavily in every Java program, so they optimized it from the start.
3) How to Split String in Java?
Java API provides several convenient methods to split the string based upon any delimiter e.g. comma, semicolon or colon. You can even use a regular expression to split a big string into several smaller strings.
4) In an array 1-100 multiple numbers are duplicates, how do you find it?
One  of the tricks in this programming questions is by using HashMap or Hashtable, we can store the number as key and its occurrences as value if the number is already present in Hashtable then increment its values or insert value as 1 and later on print all those numbers whose values are more than one.
5) Given two arrays, 1,2,3,4,5,6 and 2,3,1,0,5,6 find which number is not present in after the second array.
Here is a quick tip to solve this programming question: put the elements of the second array in the Hashtable and for every element of the first array, check whether it’s present in the hash or not, output all those elements from the first array that they are not present in the hash tables.
6) How do you find middle element of a linked list in a single parse?
To answer this programming question I would say you start with a simple solution in which you traverse the LinkedList until you find the tail of linked list where it points to null to find the length of linked list and then reiterating till the middle. After this answer interviewer will ask you find the middle element in single pass and there you can explain the code saying that by doing space-time trade-off you can use two pointers one incrementing one step at a time and other incrementing two step a time, so when the first pointer reaches end of linked second pointer will point to the middle element.
7) How do you find a 3rd element from last in a single pass? 
This programming question is similar to above and can be solved by using 2 pointers, start the second pointer when the first pointer reaches third place.
8) How do you find if there is any loop in the singly linked list? How do you find the start of the loop? 
This programming question can also be solved using 2 pointer variables and if you increase one pointer one step at a time and other as two steps at a time they will meet at some point if there is a loop.
9)How do you sort Java object using Comparator? 
This is another Java specific programming questions and you can check how to sort Object using Comparator and Comparable for an answer.
10)How to check if a number is binary? 
For this question, you need to write a function which will accept an integer and return true if it contains only 0 and 1 e.g. if the input is 123 then your function will return false, for 101 it should return true.
Take your time to comment on this article.
Previous
Next Post »