- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- How can one call one constructor from another constructor in a class?
Answer: Use the this() method to refer to constructors.
- 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
- 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:
10 Tricky Java Interview Questions
Join the DZone community and get the full member experience.Join For Free
Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.
Opinions expressed by DZone contributors are their own.