10 of the trickiest Java Interview Questions for Developers in 2018
1. How can you determine if JVM is 32-bit or 64-bit from Java Program?
Ans: You can find JVM bit size e.g. 32 bit or 64 bit by using either running java command from the command prompt or by using System.getProperty() from Java program.
2. Can you override private or static method in Java ?
Ans: You can not override private or static method in Java. If you create a similar method with same return type and same method arguments, that’s called method hiding.”
3. Can you create an immutable object that contains a mutable object?
Ans: If you want to encapsulate a mutable object into an immutable one, then you need to:
- Create a copy of the mutable object (i.e. via copy constructor, cloning, serialization/deserialization, etc.); never store the reference to the original mutable object.
- Never return the mutable object. If you must to, then return a copy of the object.
- Avoid methods which can change the mutable object.
public Employee(int id, Address address){
this.id = id;
this.address=new Address();
this.address.setStreet( address.getStreet() );
}
public Address getAddress() {
Address nuAdd = new Address(); // must copy here too
nuAdd.setStreet( address.getStreet() );
return nuAdd;
}
4. How can you mark an array volatile in Java?
Ans: By only changing to the reference which is pointing to an array will become visible to all threads , not to the whole array. What does that mean is - if one thread changes the reference variable to points to another array, that will provide a volatile guarantee, but if multiple threads are changing individual array elements, there won’t be any happens before guarantee provided by the volatile modifier for such modification. So, if your use-case is to provide memory visibility guarantee for individual indices of the Array, volatile is of no use for you. You must rely on other mechanism for thread-safety in that particular case, for example, use of synchronized keyword.
5. What is the difference between StringBuffer and StringBuilder in Java?
Ans: StringBuilder was introduced in Java 5… only difference is that Stringbuffer methods are synchronized while StringBuilder is non-synchronized.
6. Will this return 5*0.1 = 0.5? True or false?
Ans: Find yourself!!
7. What is the right data type to represent Money (like Dollar/Pound) in Java?
Ans : BigDecimal is the best data type to use for currency.
There are a whole lot of containers for currency, but they all use BigDecimal as the underlying data type. You won't go wrong with BigDecimal, probably using BigDecimal.ROUND_HALF_EVEN rounding.
8. Can we use multiple main methods?
Ans : “Yes. While starting the application, we mention the class name to be run. The JVM will look for the main method only in the class whose name you have mentioned. Hence there is no conflict amongst the multiple classes… We can overload main method but we can not override it. So, we can have many main methods in a class.”
9. Is ++ operation thread-safe in Java?
Ans: The method is not thread-safe, because the counter++ operation is not atomic, which means it consists more than one atomic operations. ... So the value returned to Thread 1 is the value that has not been increased.
10. In Java, can we store a double value in a long variable without explicit casting?
Ans: Yes, double has 52 bits mantissa, that is more than enough to exactly represent 32 bit integers.
COMMENT YOUR ANSWER FOR QUESTION NUMBER 6 OR YOU CAN ASK DOUBT IF YOU ARE HAVING IN AFOREMENTIONED QUESTIONS!!
No comments:
Post a Comment