Java Tricky Interview Questions

0



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?


Mostly asked question in interview . So the answer of this question is that when we use multiple inheritance a diamond type of shape is created which leads to the ambiguity errors . This is why java does not support multiple inheritance . Although we can implement it by using interfaces but in case of classes we can not .

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 ?

I know you may have never seen such problem before , because it is also one of the most trickiest question . So when you read the documentation of Integer you will find that if the value is between -128 and 127, so all of them use the cached pool, and the == operator compares the memory location which is in the same cached pool . In cache of 130 it will not use cached pool so the location will be different.  

(Integer ) 1== (Integer) 1 returns true but (Integer)130 == (Integer)130 returns false.

If you want to compare two objects you need to use the .equals() method . It just compare the values of both the objects and returns either true or false.


4. Why is 0.1 * 3 not equals to 0.3 ?

It is due to IEEE 754 which is the way computers store floating point numbers.
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.
Also the reason is that only numbers that are powers of 2 can be represented precisely with a simple binary representation.
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 . 


So it depends on the String pool , if the value is present then it will create only one object otherwise it will create two objects.

I hope you like these questions . If you want to add some more questions in the list post them into the comment box.



Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !