Tuesday, October 30, 2012

Java Oracle Interview Questions

Q: What is the difference between set and list?
Ans : Set stores elements in an unordered way but does not contain duplicate elements, whereas list stores elements in an ordered way but may contain duplicate elements.

Q : What is garbage collection? What is the process that is responsible for doing that in java?
Ans : Reclaiming the unused memory by the invalid objects. Garbage collector is responsible for this process

Q: What kind of thread is the Garbage collector thread?
Ans. It is a daemon thread.

Q : What is a daemon thread?
Ans : These are the threads which can run without user intervention. The JVM can exit when there are daemon thread by killing them abruptly.

Q : What is the basic difference between string and stringbuffer object?
Ans. String is an immutable object. StringBuffer is a mutable object.

Q: What is Servlet chaining?
Ans: Servlet chaining is a technique in which two or more servlets can cooperate in servicing a single request. In servlet chaining, one servlet’s output is piped to the next servlet’s input. This process continues until the last servlet is reached. Its output is then sent back to the client.

Q: What are JSP scripting elements?
Ans :JSP scripting elements lets to insert Java code into the servlet that will be generated from the current JSP page. There are three forms:
1) Expressions of the form <%= expression %> that are evaluated and inserted into the output
2) Scriptlets of the form<% code %>that are inserted into the servlet’s service method
3) Declarations of the form <%! Code %>that are inserted into the body of the servlet class, outside of any existing methods.

Q: What is the servlet lifecycle?
Instantiation and initialization
The servlet engine  creates an instance of the servlet. The servlet engine creates the servlet configuration object and uses it to pass the servlet initialization parameters to the init() method. The initialization parameters persist until the servlet is destroyed and are applied to all invocations of that servlet.

Servicing requests
The servlet engine creates a request object and a response object. The servlet engine invokes the servlet service() method, passing the request and response objects.

The service() method gets information about the request from the request object, processes the request, and uses methods of the response object to create the client response. The service method can invoke other methods to process the request, such as doGet(), doPost(), or methods you write.

The servlet engine stops a servlet by invoking the servlet's destroy() method. Typically, a servlet's destroy() method is invoked when the servlet engine is stopping a Web application which contains the servlet. The destroy() method runs only one time during the lifetime of the servlet and signals the end of the servlet.
After a servlet's destroy() method is invoked, the servlet engine unloads the servlet, and the Java virtual machine eventually performs garbage collection on the memory resources associated with the servlet.

Q: Can't I use a constructor instead of init(ServletConfig)?
Ans:Yes, of course you can use the constructor instead of init(). There's nothing to stop you. But you shouldn't. The original reason for init() was that ancient versions of Java couldn't dynamically invoke constructors with arguments, so there was no way to give the constructur a ServletConfig. That no longer applies, but servlet containers still will only call your no-arg constructor. So you won't have access to a ServletConfig or ServletContext.

Q: Should I get my database connection in the init() method?
Ans: The init(ServletConfig) method is only called once when a servlet is brought into service by the servlet container, not for each new servlet thread.
Generally it is better to use the init(ServletConfig) method to register the database driver and get a reference to the DriverManager, a DataSource or database connection pool. Then use the connection provider to get a connection for each request within a try/catch block. This way you can handle the case where the connection fails, and ensure that all connections are closed in any case. The close() method of a pooled Connection instance just returns it to the pool.

Q: What is the difference between merge and update in hibernate?
 Update() : Update the persistent instance with the identifier of the given detached instance. If there is a persistent instance with the same identifier, an exception is thrown. This operation cascades to associated instances if the association is mapped with cascade="save-update".

merge() : Copy the state of the given object onto the persistent object with the same identifier. If there is no persistent instance currently associated with the session, it will be loaded. Return the persistent instance. If the given instance is unsaved, save a copy of and return it as a newly persistent instance. The given instance does not become associated with the session. This operation cascades to associated instances if the association is mapped with cascade="merge".

Q: what is the use of cascade in hibernate?
Ans: In cascade, after one operation (save, update and delete) is done, it decide whether it need to call other operations (save, update and delete) on another entities which has relationship with each other.

Q: Write an sql query for deleting duplicate rows ?
FROM MyTable
  FROM MyTable
  GROUP BY DuplicateColumn1, DuplicateColumn2, DuplicateColumn3

Q:What are Primary Keys and Foreign Keys?
Ans: Primary keys are the unique identifiers for each row. They must contain unique values and cannot be null. Due to their importance in relational databases, Primary keys are the most fundamental aspect of all keys and constraints. A table can have only one primary key.
Foreign keys are a method of ensuring data integrity and manifestation of the relationship between tables.

Q : What is DriverManager ?
Ans: DriverManager is a class in java.sql package. It is the basic service for managing a set of JDBC drivers.

No comments:

Post a Comment