Java Developer Interview Questions
IndicThreads ran an article some years ago about the kind of job interview questions that were faced by Java developers. The software industry has kept growing and software developers continue to be in great demand. So here is a compilation of important questions & answers from the earlier article as well as new ones on Java SE and Java EE. In between then and now, Java has changed and has come out with newer versions and more features (complexities?). So have changed the questions. The Java developer is today tested on web2.0, ajax frameworks, soa, and so on. We don’t have all the questions yet and will keep updating this piece. If you would like to share interview question & answers, please add your comment along with your name, the role you play and company name.
:: Interview Questions on Java SE ::
1. What is the difference between an interface and abstract class?
An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract. An interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract methods. An excellent comparison between interfaces and abstract classes can be found at http://mindprod.com/jgloss/interfacevsabstract.html
2. Explain polymorphism
Polymorphism means to change form. In java, a superclass reference or an interface reference can change form by accepting objects of implementing classes or of subclasses. For e.g: If Vehicle is an interface that a class Car implements, polymorphism can be achieved in the equation Vehicle v = new Car(); . Here an object of subclass Car is put into the reference of the interface Vehicle. Now invoking a method of the interface on v will invoke the overridden version of the method from class Car.
3. Why do we need interfaces in Java? Instead of an interface, we can write the methods in the class itself. Why do we need seperate interface?
Firstly, when you wish to enforce a set of classes to implement a set of methods, you can do so be creating an interface with a set of methods. When those classes implement that interface, they are forced to provide an implementation for all the methods of that interface. Secondly, an interface is useful in scenarios where all the classes have a different implementation for the methods of the interface. Thirdly, a class can implement multiple interfaces.
4. Are java.lang.String instances immutable?
Yes. A String object value cannot be modified. A String object reference can simply point to a new String object value.String name = “ABC”; a = “XYZ”; The value “ABC” does not change – The pointer to value “ABC” is removed and name starts pointing to a new object “XYZ”. “ABC” remains in memory unless garbage collected.
5. Which one is thread safe,(1)Arraylist(2)Vector(3)Map(4)Table?
Vector is thread safe
6. Why are methods like notify(),notifyAll(),wait present in super class Object?
Wait causes current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed. So the thread which owns the monitor will wait for object to be available.
The thread goes in wait state for this object and then to relinquish any and all synchronization claims on this object. Thread T becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:
• Some other thread invokes the notify method for this object and thread T happens to be arbitrarily chosen as the thread to be awakened.
• Some other thread invokes the notifyAll method for this object.
• Some other thread interrupts thread T.
• The specified amount of real time has elapsed, more or less. If timeout is zero, however, then real time is not taken into consideration and the thread simply waits until notified.
7. How to kill all the threads in a program in one command?
To read about the Thread class, visit java.sun.com/javase/6/docs/api/java/lang/Thread.html
8. State the difference between throw and throws.
1) throws: If a method is capable of throwing a checked exception, it must declare this by using the throws clause in it’s signature. For eg: public void testMeth() throws IOException{
}
Now any method invoking testMeth() will have to either use a try catch block to deal with IOException or use the throws clause in it’s signature.
2) throw: A method which wishes to throw an exception object must use the throw clause in it’s body. For eg.
public void testMeth() throws IOException { throw new IOException();
}
The throw creates an exception object within the method body. The throws clause is used in the method signature to signal to other methods that it may throw so and so exceptions.
For more about exceptions, visit java.sun.com/javase/6/docs/api/java/lang/Exception.html
9. What is the difference between a Vector and an ArrayList?
Take a look at java.sun.com/javase/6/docs/api/java/util/Vector.html and
http://java.sun.com/javase/6/docs/api/java/util/ArrayList.html
10. What is the difference between the JVM and the JRE?
Read the answer at http://java.sun.com/javase/6/docs/technotes/guides/index.html
11. Difference between overloading a method and overriding a method?
Overloading is the ability to have a class with multiple methods of the same name where the arguments and return type (method signature) allow the compiler to determine which of the same named methods were meant to be called.
Overriding methods have the same name and identical signatures. When a class is specialized by creating a subclass, to change the behavior of that subclass you replace the behavior of the parent class. This is done by writing a new method with the same name and method signature.
Source: http://java.sun.com/new2java/supplements/2003/Jun03.html
12. What is Exception Handling and how it is implemented?
Check out the javadocs at java.sun.com/javase/6/docs/api/java/lang/Exception.html
13. What does the “protected” keyword do?
A protected field or method is accessible to the class itself, its subclasses, and classes in the same package.
Source: java.sun.com/developer/onlineTraining/Programming/BasicJava2/oo.html
:: Interview Questions on Java EE ::
1. What is the difference between POST and GET?
Get – form data is to be encoded (by a browser) into a URL. Usage recommendations – If the processing of a form is idempotent (i.e. it has no lasting observable effect on the state of the world), then the form method should be GET. Many database searches have no visible side-effects and make ideal applications of query forms.
Post – form data is to appear within a message body. Usage – If the service associated with the processing of a form has side effects (for example, modification of a database or subscription to a service), the method should be POST.
2. What problems might you encounter if you have a class level variable in a servlet?
All requests use the same instance of a servlet and values can get over-written. Unless the servlet implements single Thread model
3. When writing code to access the database, where is the best place to write your query? In the JSP, the servlet or somewhere else?
Best to write it in xml/config files. So that if the DB changes, the code does not have to be recomplied
4. What is the difference between Web Server and Application Server?
It supports Load Balance,cache control and it supports HTTP, TCP/Ip and all protocols. But Web server can supports only HTTPExplain working of a stateless session bean
5. Explain working of a stateless session bean.
6. Explain the JEE architecture of any project you have worked on
7. Explain MVC with reference to Apache Struts.
8. How can you track a customer or store customer data using HttpServlet sessions?
9. What does loadonstartup in a web.xml file achieve?
10. What methods are present in HttpServletRequest/HttpServletResponse/HttpServletSession?
11. Steps involved in obtaining DBase connection in JDBC?
12. In how many ways session can be traced?
11. Explain RMI
13. Explain Java security
14. What is Request, Session and Application scope?
15. What is a TX and XA Interface?
16. Difference between JTA and JTS
17. What is the difference between ServletContext and SessionContext?
… Rarely in an interview can one give a hyperlink as a response. So this article fails on that score. Otherwise a fair, if dated summary of possible questions.
good
i want iterate the map interface