Tuesday, 9 May 2017

Top 10 Tricky Java interview questions and Answers

Top 10 Tricky Java interview questions and Answer


What is a tricky question? Well, tricky Java interview questions are those questions which have some surprise element on it. If you try to answer a tricky question with common sense, you will most likely fail because they require some specific knowledge. Most of the tricky Java questions comes from confusing concepts like function overloading and overriding, Multi-threading which is really tricky to master, character encoding, checked vs unchecked exceptions and subtle Java programming details like Integer overflow. Most important thing to answer a tricky Java question is attitude and analytical thinking, which helps even if you don't know the answer. Anyway in this Java article we will see 10 Java questions which are real tricky and requires more than average knowledge of Java programming language to answer them correctly. As per my experience, there is always one or two tricky or tough Java interview question on any core Java or J2EE interviews, so it's good to prepare tricky questions from Java in advance.

Sunday, 7 May 2017

2 Ways to find duplicate elements in an Array - Java

2 Ways to find duplicate elements in an Array - Java
Problem: You have given an array of objects, which could be an array of integers and or array of Strings or any object which implements the Comparable interface. How would you find duplicate elements from an array? Can you solve this problem in O(n) complexity? This is actually one of the frequently asked coding problems from Java interviews. There are multiple ways to solve this problem and you will learn two popular ways here, first the brute force way, which involves comparing each element with every other element and other which uses a hash table like data structure to reduce the time complexity of problem from quadratic to linear, of course by trading off some space complexity. This also shows that how by using a suitable data structure you can come up with a better algorithm to solve a problem. If you are preparing for programming job interviews, then I also suggest you take a look at Cracking the Coding Interview book, which contains 150 programming questions and solutions, good enough to do well on any programming job interviews e.g. Java, C++, Python or Ruby.

Friday, 5 May 2017

Java Program to reverse an array in place? Fastest Example

Java Program to reverse an array in place? Fastest Example


It's easy to reverse an array if you have the luxury to use another array, but how would you reverse an array if a temporary buffer is not allowed? This is one of the testing array interview questions, which often proved tricky for Java programmers. Well, you can also reverse an array in place without using an additional buffer. If you know how to access array elements and how to loop over an array in Java using traditional for loop, you can easily solve this problem without using additional space. All you need to do is loop over the array fhttps://www.blogger.com/blogger.g?blogID=8114691391974361909#editor/target=post;postID=8599089079040113425rom start to the middle element and swap first element to the last, second element to the second last etc. Once you reach the middle element, your array is already sorted and that too without using any additional space. You can even use this algorithm to reverse a String in Java as well. After all, a String is backed by character array.

Wednesday, 3 May 2017

Top 5 Java Main method Interview Questions with Answers

Top 5 Java Main method Interview Questions with Answers
The main() method in Java is starting point of any standalone core Java application. JVM starts executing Java program from main method and the thread which executes main is called main thread in Java. The main method is also an important topic in Java interviews for 2 to 3 years experienced developer. In this Java article, we will a couple of questions related to the main method in Java. Apart from Why main is static in Java, I see following questions keep coming related to the main method:
1.     Can we overload the main method in Java? Which main method JVM will call?
2.     Can we override the main method in Java?
3.     Can we make main final in Java?
4.     Can we make main synchronized in Java?
5.     How to call a nonstatic method from main in Java?

Monday, 1 May 2017

What is Polymorphism in Java? Overriding or Overloading

What is Polymorphism in Java? Overriding or Overloading


Polymorphism vs Overloading vs Overriding
Someone asked me What are the difference between Polymorphism and Overriding in Java and the similar difference between Polymorphism and Overloading. Well, they are not two different things, Polymorphism is an object oriented or OOPS concept like Abstraction, Encapsulation or Inheritance which facilitate the use of the interface and allows Java program to take advantage of dynamic binding in Java. Polymorphism is also a way through which a Type can behave differently than expected based upon which kind of Object it is pointing. Overloading and overriding are two forms of Polymorphism available in Java.