If you are appearing for any interview you should always keep in mind that the interviewer has prepared some questions which is going to challenge your knowledge , your ideas . Those questions are the keys for your success .
In this blog we are going to cover some of the tricky interview questions related to java programming language . Though I have prepared and shared a lot of difficult core Java interview question and answers, But I have chosen them as the Top 5 tricky questions because you can not guess answers to these tricky Java questions easily, you need some subtle details of Java programming language to answer these questions.
1. Why is multiple inheritances not supported in Java?
2. Can you access a non-static variable in the static context?
This is one of the tricky question which is asked in the interview . So the answer of this question is No, you can not access a non-static variable from the static context in Java. If you try, it will give a compile-time error. This is actually a common problem beginner in Java face when they try to access instance variables inside the main method. Because main is static in Java, and instance variables are non-static, you can not access instance variable inside main.3. Why is (Integer) 1 == (Integer) 1 but (Integer) 130 != (Integer) 130 ?
4. Why is 0.1 * 3 not equals to 0.3 ?
With this representation, 0.1 cannot be even represented exactly by a 96bit float. Neither can 0.3 for that matter, but if the latter were, as 0.1 cannot be represented exactly, it means that multiplying it by 3 would not yield 0.3 exactly.
The individual digits of a binary number are different powers of 2 and any number is thus a sum of different powers of 2.
0.1
does not correspond to a power of 2 and can not be summed up from different powers of 2. Thus it can be represented only with a certain error.5. How many String objects are created by the following code?
String s=new String("Hindi Coding Community");
(a) 1 (b) 2 (c) 3 (d) 4
So if you are familiar with String class in java then you might know that java uses two things to store the String data . First is String pool and other is Heap.
So when you write String s="hindi coding community"; then it is a string literal which will be stored in String pool .
And if you write String s = new String("hindi "); then in this case first it will go to string pool and search whether "hindi" is there or not .
1. If "hindi" is there in string pool then it will create only one object in heap .
2. If "hindi" is not there in string pool then it will create an object in string pool as well as one object in heap . So it will create 2 objects .