Tuesday, March 6, 2012

Java J2EE Interview Questions Part 2

15. How do we test private methods in JUnit?
- Private methods cannot be directly tested. If required we can use Java Reflection to test private methods.

16. How do we test protected methods?

- Place the test class under the same package as the class under test.

17. How do you invoke Stored Procedure from Hibernate?
- use hibernate 3 annotation as:
@NamedNativeQueries({
    @NamedNativeQuery(
    name = "callStockStoreProcedure",
    query = "CALL GetStocks(:stockCode)",
    resultClass = Stock.class
    )
})

Query query = session.getNamedQuery("callStockStoreProcedure")
    .setParameter("stockCode", "7277");
   
18. How do you manage transaction in Spring using Hibernate?
- Declare HibernateTransactionManager and set dataSource and sessionFactory property
- Create TransactionInterceptor and set transactionManager and transactionAttributes
- Now create ProxyFactoryBean and set the target dao/bo bean and interceptorNames

19. How do you configure connection pooling using JDBC?
-

20. How to create store procedure in oracle?
CREATE OR REPLACE PROCEDURE my_store_procedure
IS
BEGIN
    select * from my_table where uid = n;
END;

21. What is the difference between LinkList and ArrayList?

22. What is the difference between forward and send redirect?

23. What is MVC?




In progress...........

Sunday, March 4, 2012

Java J2EE Interview Questions Part 1

1. What are the new features in JDK or Java 6?
- XML and Web Services support
- JDBC 4.0 support
- More Annotation type
- More flexible annotation processing
- Java compiler API accessible from programs
- Application client GUI enhancements for both AWT and Swing.
- Scripting support.
- Security like XML-Digital Signature APIs.

2. What is Java reflection?
- Java's reflection API's makes it possible to inspect classes, interfaces, fields and methods at runtime, without knowing the name of the classes, methods etc at compile time. It is also possible to instantiate new objects, invoke methods and get/set field values using reflection. Board use of Java Reflection is: security, code complexity, run time performance etc. Other tangible use of reflection is in JavaBeans, where software components can be manipulated visually via a builder tool.

3. What is synchronization?
-    With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared variable while another thread is in the process of using or updating same shared variable. this usually leads to significant errors.

4. What is volatile in Java?
- volatile is keyword and value of the volatile variable will never be cached thread-locally (i.e. all reads and writes will go straight to main memory).
- also access to the variable acts as though it is enclosed in a synchronized block.

5. How do you improve performance of the database query? or How do you performance tune your database?
- denormalize the table where appropriate.
- proper use of index columns (eg index based on numeric fields than character columns)
- reduce number of columns that make up a composite key
- proper partitioning of tablespace and create special tablespace for CLOB, BLOG
- data access performance can be tuned by store procedure reducing network overhead and also caching data within your application

6. What is Hibernate dirty checking?
- Hibernate automatically detects object state changes in order to synchronize the updated state with the database, this is called dirty checking. Hence, it automatically update only those fields which has been modified. As opposed to updating all persistent objects at the end of each work, automatic dirty checking can offer significant performance savings.

7. What is SOAP fault?
- SOAP fault is an error in a SOAP communication resulting from incorrect message format, header-processing problems, or incompatibility between appliaiton. when a SOAP fault occurs, a special message is generated that contains data indicating where the error originated and what caused it. This data is called a fault element. A message that includes a fault element is known as a fault message.

8. What is the difference between SOAP and RESTful web services?
- Use SOAP when
    1. required stateful operations
    2. required more formal contracts
    3. required asynchronous processing and invocation
- Use RESTful when
    1. required limited bandwidth and resources
    2. required totally stateless operations
    3. required caching situations

9. What is the difference between JDBC excecute(), executeQuery() and executeUpdate()?
-    execute statement is prepared statement which is kind of SQL statement. signature boolean execute().
-    executeQuery  statement is prepared statement to execute SQL Query and returns ResultSet. signature ResultSet executeQuery().
-    executeUpdate is prepared statement for insert, update, delete and returns nothing. signature int executeUpdate().

10. What is the prepared and callable statement in JDBC?
- Prepared Statement is precompiled SQL Statement and accept parameters, more faster than regular statement and secure for SQL Injection.
- Callable Statement is for calling Stored Procedure.

11. What happen to the JMS message when MOM is down?
- message delivery mode set to durable (persistent) when MOM save message to database incase of server crash or message lost and it has adverse effect on performance, but ensure that message delivery is guaranteed.

12. How will you map class inheritance to relational data model?
- Map class hierarchy to single database table (union mapping)
- Map each class to its own table (vertical mapping)
- Map each concrete class to its own table (horizontal mapping)

13. What is the difference between throw and throws in Java?
- throws declare the exception for the method like:
    public void myMethod() throws MyException{} //tells us that what method might pass back to the caller.
- while throw actually throw the exception like:
    public void myMethod() throws MyException{
        if(condition) {
            throw new MyException("throw exception"); // tells us to throw MyException to the caller.
        }
    }
  
14. How will you control two concurrent transactions accessing a database?
- use isolation levels. Dirty read, Norepeatable read and Phantom reads. Provide different isolation levels like:
    TRANSACTION_READ_UNCOMMITED, TRANSACTION_READ_COMMITED, TRANSACTION_REPEATABLE_READ, and TRANSACTION_SERIALIZABLE.