corejava_13

61. If you assign null to the String variable, and then you print the variable what will be the output ? Justify your answer.
No, the answer is not NullPointerException,It will print null.We may confuse that case like this would lead to the NullPointerExeception.Such exception will arise only if the object reference (which is not pointing to any object ),is used ,to try to access the object's behavior or properties.

62. Why would you use a synchronized block vs. synchronized method?
Synchronized blocks place locks for shorter periods than synchronized methods.

63. What does String intern() method do?
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
This method always return a String that has the same contents as this string, but is guaranteed to be from a pool of unique strings.

64. What will happen if we are calling super() in constructor but our class does not extending any class?
 If our class not extending any class Yes still we can use super(); call in our class.  Because in java every class will extend Object class by default this will be added by JVM. But make sure we are using only super(); default call we can not place parameterized super call because Object class does not have any parameterized constructor.

65. Why Map interface doesn’t extend Collection interface?
Although Map interface and it’s implementations are part of Collections Framework, Map are not collections and collections are not Map. Hence it doesn’t make sense for Map to extend Collection or vice versa.
If Map extends Collection interface, then where are the elements? Map contains key-value pairs and it provides methods to retrieve list of Keys or values as Collection but it doesn’t fit into the “group of elements” paradigm.

No comments:

Post a Comment