Wednesday, June 13, 2012

10 Tricky Java Interview Questions

Here are some Java interview questions which are  un-common
  1. What is the performance effect of a large number of import statements which are not used?
    Answer: They are ignored if the corresponding class is not used.
  2. Give a scenario where hotspot will optimize your code?
    Answer: If we have defined a variable as static and then initialized this variable in a static block then the Hotspot will merge the variable and the initialization in a single statement and hence reduce the code.
  3. What will happen if an exception is thrown from the finally block?
    Answer: The program will exit if the exception is not catched in the finally block.
  4.  How does decorator design pattern works in I/O classes?
    Answer:  The various classes like BufferedReader , BufferedWriter workk on the underlying stream classes. Thus Buffered* class will provide a Buffer for Reader/Writer classes.
  5. If I give you an assignment to design Shopping cart web application, how will you define the architecture of this application. You are free to choose any framework, tool or server?
    Answer:  Usually I will choose a MVC framework which will make me use other design patterns like Front Controller, Business Delegate, Service Locater, DAO, DTO, Loose Coupling etc. Struts 2 is very easy to configure and comes with other plugins like Tiles, Velocity and Validator etc. The architecture of Struts becomes the architecture of my application with various actions and corresponding JSP pages in place.
  6. What is a deadlock in Java? How will you detect and get rid of deadlocks?
    Answer:  Deadlock exists when two threads try to get hold of a object which is already held by another object.
  7. Why is it better to use hibernate than JDBC for database interaction in various Java applications?
    Answer:  Hibernate provides an OO view of the database by mapping the various classes to the database tables. This helps in thinking in terms of the OO language then in RDBMS terms and hence increases productivity.
  8. How can one call one constructor from another constructor in a class?
    Answer:  Use the this() method to refer to constructors.
  9. What is the purpose of intern() method in the String class?
    Answer:  It helps in moving the normal string objects to move to the String literal pool
  10. How will you make your web application to use the https protocol?
    Answer:  This has more to do with the particular server being used  than the application itself. Here is how it can be done on tomcat:

No comments:

Post a Comment