Java Interview Questions For Experienced

Java Interview Questions For Experienced

Java Training Institute in Hyderabad1.Why main() in java is declared as public static void main? What if the main method is declared as private?
ANSWER:
Public :- main method is called by JVM to run the method which is outside the scope of project therefore the access specifier has to be public to permit call from anywhere outside the application static – When the JVM makes are call to the main method there is not object existing for the class being called therefore it has to have static method to allow invocation from class. void – Java is platform independent language therefore if it will return some value then the value may mean different to different platforms so unlike C it can not assume a behavior of returning value to the operating system. If main method is declared as private then – Program will compile properly but at run-time it will give “Main method not public.” error.

The only difference between experienced and inexperienced software developers is that the experienced ones realize when they’re making a mistake.
Can you think of a questions which is not part of this post? Please don’t forget to share it with me in comments section & I will try to include it in the list.

2.Why doesn’t Collection extend Cloneable and Serializable?
ANSWER:  Sun FAQ Page: Many Collection implementations (including all of the ones provided by the JDK) will have a public clone method, but it would be mistake to require it of all Collections. For example, what does it mean to clone a Collection that’s backed by a terabyte SQL database? Should the method call cause the company to requisition a new disk farm? Similar arguments hold for serializable. If the client doesn’t know the actual type of a Collection, it’s much more flexible and less error prone to have the client decide what type of Collection is desired, create an empty Collection of this type, and use the addAll method to copy the elements of the original collection into the new one. Note on Some Important Terms

Synchronized means only one thread can modify a hash table at one point of time. Basically, it means that any thread before performing an update on a hashtable will have to acquire a lock on the object while others will wait for lock to be released.
Fail-fast is relevant from the context of iterators. If an iterator has been created on a collection object and some other thread tries to modify the collection object “structurally”, a concurrent modification exception will be thrown. It is possible for other threads though to invoke “set” method since it doesn’t modify the collection “structurally”. However, if prior to calling “set”, the collection has been modified structurally, “IllegalArgumentException” will be thrown.

3.Explain different way of using thread?
ANSWER: The thread could be implemented by using runnable interface or by inheriting from the Thread class. The former is more advantageous, ’cause when you are going for multiple inheritance..the only interface can help.

4.What is Polymorphism?
ANSWER: Polymorphism is briefly described as “one interface, many implementations.” Polymorphism is a characteristic of being able to assign a different meaning or usage to something in different contexts – specifically, to allow an entity such as a variable, a function, or an object to have more than one form.

5.Which two method you need to implement for key Object in Hash Map ?
ANSWER: In order to use any object as Key in Hash Map, it must implements equals and hashcode method in Java. Read How Hash Map works in Java for detailed explanation on how equals and hash code method is used to put and get object from Hash Map. You can also see my post 5 tips to correctly override equals in Java to learn more about equals.

6.Explain the usage of Java packages?
ANSWER: This is a way to organize files when a project consists of multiple modules. It also helps resolve naming conflicts when different packages have classes with the same names. Packages access level also allows you to protect data from being used by the non-authorized classes.

7.Difference between Hash Map and Hash Table?
ANSWER: The Hash Map class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls. (Hash Map allows null values as key and value whereas Hashtable doesnt allow). Hash Map does not guarantee that the order of the map will remain constant over time. Hash Map is unsynchronized and Hash table is synchronized.

8.How does Java implement polymorphism?
ANSWER: Polymorphism manifests itself in Java in the form of multiple methods having the same name.
In some cases, multiple methods have the same name, but different formal argument lists (overloaded methods).
In other cases, multiple methods have the same name, same return type, and same formal argument list (overridden methods).

9.Where does these  two method comes in picture during get operation?
ANSWER: This core Java interview question is follow-up of previous Java question and candidate should know that once you mention hashCode, people are most likely ask How its used in Hash Map. See How HashMap works in Java for detailed explanation.

10.Explain in your own words the “bottom line” benefits of the use of an interface?
ANSWER: The interface makes it possible for a method in one class to invoke methods on objects of other classes, without the requirement to know the true class of those objects, provided that those objects are all instantiated from classes that implement one or more specified interfaces. In other words, objects of classes that implement specified interfaces can be passed into methods of other objects as the generic type Object, and the methods of the other objects can invoke methods on the incoming objects by first casting them as the interface type.

For Java Class room Training please visit http://www.revanthtechnologies.com