Friday 23 June 2017

Java Interview Questions You Must Prepare For In 2017




1.What is the purpose of garbage collection in Java? When is it used?
 ANS : The purpose of garbage collection is to identify and discard the objects that are no longer needed by the application to facilitate the resources to be reclaimed and reused.

2.What are the differences between path and classpath variables?
ANS : PATH is an environment variable used by the operating system to locate the executables. This is the reason we need to add the directory location in the PATH variable when we install Java or want any executable to be found by OS.Classpath is specific to Java and used by Java executables to locate class files. We can provide the classpath location while running a Java application and it can be a directory, ZIP file or JAR file.

3.What are the differences between Heap and Stack Memory?
ANS : The major difference between Heap and Stack memory are:

  • Heap memory is used by all the parts of the application whereas stack memory is used only by one thread of execution.
  • When an object is created, it is always stored in the Heap space and stack memory contains the reference to it.
  • Stack memory only contains local primitive variables and reference variables to objects in heap space.
  • Memory management in a stack is done in LIFO manner; it is more complex in Heap memory as it is used globally.

4.What are the differences between String, StringBuffer and StringBuilder?
ANS : 
The string is immutable and final in Java, so a new String is created whenever we do String manipulation. As String manipulations are resource consuming, Java provides two utility classes: StringBuffer and StringBuilder.
  • StringBuffer and StringBuilder are mutable classes. StringBuffer operations are thread-safe and synchronized where StringBuilder operations are not thread-safe.
  • StringBuffer is to be used when multiple threads are working on same String and StringBuilder in the single threaded environment.
  • StringBuilder performance is faster when compared to StringBuffer because of no overhead of synchronizing
5.What is a servlet?
ANS :

  • Java Servlet is server side technologies to extend the capability of web servers by providing support for dynamic response and data persistence.
  • The java.servlet and java.servlet.HTTP packages provide interfaces and classes for writing our own servlets.
  • All servlets must implement the java.servlet.Servlet interface, which defines servlet lifecycle methods. When implementing a generic service, we can extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet() and doPost(), for handling HTTP-specific services.
  • Most of the times, web applications are accessed using HTTP protocol and that's why we mostly extend HttpServlet class. Servlet API hierarchy is shown in below image.

6.How do cookies work in Servlets?
ANS : Cookies are text data sent by a server to the client and it gets saved to the client local machine.
Servlet API provides cookies support through java.servlet.http.Cookie class that implements Serializable and Cloneable interfaces.
HttpServletRequest getCookies() method is provided to get the array of Cookies from a request since there is no point of adding Cookie to request, there are no methods to set or add the cookie to request.
Similarly, HttpServletResponse addCookie(Cookie c) method is provided to attach cookie in the response header, there are no getter methods for the cookie.


7.What is JDBC Driver?
JDBC Driver is a software component that enables Java application to interact with the database. There are 4 types of JDBC drivers:
  1. JDBC-ODBC bridge driver
  2. Native-API Driver (partially Java driver)
  3. Network Protocol driver (fully Java driver)
  4. Thin driver (fully Java driver)

8.What is JDBC Connection interface?
ANS : The Connection interface maintains a session with the database. It can be used for transaction management. It provides factory methods that return the instance of Statement, PreparedStatement, CallableStatement, and DatabaseMetaData.

9.How to integrate Spring and Hibernate Frameworks?
ANS : We can use Spring ORM module to integrate Spring and Hibernate frameworks if you are using Hibernate 3+ where SessionFactory provides current session, then you should avoid using HibernateTemplate or HibernateDaoSupport classes and better to use DAO pattern with dependency injection for the integration.
Also, Spring ORM provides support for using Spring declarative transaction management, so you should utilize that rather than going for a hibernating boiler-plate code for transaction management.

10.What are the advantages of Hibernate over JDBC?
ANS : 
  1. Hibernate removes a lot of boilerplate code that comes with JDBC API, the code looks cleaner and readable.
  2. Hibernate supports inheritance, associations, and collections. These features are not present with JDBC API.
  3. Hibernate implicitly provides transaction management, in fact, most of the queries can’t be executed outside the transaction. In JDBC API, we need to write code for transaction management using commit and rollback. 
  4. JDBC API throws SQLException that is a checked exception, so we need to write a lot of try-catch block code. Most of the times it’s redundant in every JDBC call and used for transaction management. Hibernate wraps JDBC exceptions and throw JDBCException or HibernateException unchecked exception, so we don’t need to write code to handle it. Hibernate built-in transaction management removes the usage of try-catch blocks.
  5. Hibernate Query Language (HQL) is more object oriented and close to Java programming language. For JDBC, we need to write native SQL queries.
  6. Hibernate supports caching that is better for performance, JDBC queries are not cached hence performance is low.
  7. Hibernate provides the option through which we can create database tables too, for JDBC tables must exist in the database.
  8. Hibernate configuration helps us in using the JDBC-like connection as well as JNDI DataSource for the connection pool. This is the very important feature in enterprise application and completely missing in JDBC API.


If want become Java Training and Development to learn about Java Training in Chennai , Java Training Institute in Chennai , Java Training , Best Java Training in Chennai , Java Training Course in Chennai, Java Training Center in Chennai , Java Training in Chennai with placement , Java Training Institute in Chennai With Placement, Java Training Classes in Chennai , No.1 Java Training Institute in Chennai , Excellent Java Training in Chennai