JavaSpartans
Java J2ee Struts Hibernate Spring Interview Questions Java J2ee Struts Hibernate Spring Interview Questions
1 2 3 4 5    Next

1. When a NullPointerException is thrown ?

If you play with null values NullPointerException will be thrown. for instance, if some value is null in the database and if you got that null value to a string variable, after that if you trim it or substring it, then null pointer exception will be thrown. String str= null; str.trim() will throw NullPointerException.


2. What is singleton class ? where is it used ?

Singleton is a design pattern meant to provide one and only one instance of an object. Other objects can get a reference to this instance through a static method (class constructor is kept private). Why do we need one? Sometimes it is necessary, and often sufficient, to create a single instance of a given class. This has advantages in memory management, and for Java, in garbage collection. Moreover, restricting the number of instances may be necessary or desirable for technological or business reasons--for example, we may only want a single instance of a pool of database connections or PrintSpooler

public class Singleton {
    private static Singleton INSTANCE = null;

    // Private constructor suppresses
    // default public constructor
    private Singleton() {}

    //synchronized creator to defend against multi-threading issues
    //another if check here to avoid multiple instantiation
    private synchronized static void createInstance() {
        if (INSTANCE == null) {
            INSTANCE = new Singleton();
        }
    }

    public static Singleton getInstance() {
        if (INSTANCE == null) createInstance();
        return INSTANCE;
    }
}



3. How can I prevent the word "null" from appearing in my HTML input text fields when I populate them with a resultset that has null values?

You could make a simple wrapper function, like
<%!
String blanknull(String s) {
return (s == null) ? "" : s;
}
%>
then use it inside your JSP form, like
<%=blanknull(shoesize)%>


4. Can a null element added to a set ?

Yes but only once. A null element can be added only if the set contains one element because when a second element is added then as per set definition a check is made to check duplicate value and comparison with null element will throw NullPointerException.


5. How do you configure DataSource in Struts ? How do you implement Connection Poooling in Struts ?

Connection Pooling in Struts :

In struts-config.xml file data sources tag is there? you can configure connection pool from that xml file and you can create the connection as below.

<struts-config>
<data-sources>
<data-source type="org.apache.commons.dbcp.BasicDataSource">
<set-property property="driverClassName" value="com.mysql.jdbc.Driver" />
<set-property property="url" value="jdbc:mysql://localhost/DBNAME" />
<set-property property="username" value="" /> 
<set-property property="password" value="abcdefgh" />
<set-property property="maxActive" value="20" />
<set-property property="maxWait" value="5000" />
<set-property property="defaultAutoCommit" value="false" />
<set-property property="defaultReadOnly"   value="false" />  
data-source>
data-sources>
...//Here Other Struts Configuration Tags...
struts-config >

In the above file the default values comes with this file was maxActive 20.
You can set the values here as per your network traffic on your server?
Here totally 20 connection objects will be active in the pool and 5000 (5 seconds ) will be the time to wait for a connection object. You can set number of maxIdle connections also.

CODE : in your Action Class execute method or any other class you can create the connection object like this.
//Just declare the data source and connection objects;
javax.sql.DataSource dataSource = null;
java.sql.Connection myConnection = null;

//Just by using the following 2 lines of Code you can create a connection //object from the data source by using the concept of connection pooling.

dataSource = getDataSource(request);
myConnection = dataSource.getConnection();

You can also refer to EJB Instance Pooling...

How do you configure data source in Tomcat ?
How do you configure data source in WebLogic ?
How do you configure data source in JBoss ?
How do you configure data source in WebSphere ?


6. What can go wrong if you replace && with & in the following code: String a=null; if (a!=null && a.length()>10) {?}

A single ampersand here would lead to a NullPointerException.


7. Is null a keyword?

The null value is not a keyword.


8. Explain garbage collection?

Garbage collection is one of the most important feature of Java. Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects (value is null) from the memory. User program cann't directly free the object from memory, instead it is the job of the garbage collector to automatically free the objects that are no longer referenced by a program. Every class inherits finalize() method from java.lang.Object, the finalize() method is called by garbage collector when it determines no more references to the object exists. In Java, it is good idea to explicitly assign null into a variable when no more in use. I Java on calling System.gc() and Runtime.gc(), JVM tries to recycle the unused objects, but there is no guarantee when all the objects will garbage collected.


9. How can one prove that the array is not null but empty?

Print array.length. It will print 0. That means it is empty. But if it would have been null then it would have thrown a NullPointerException on attempting to print array.length.


10. What is Java Server Faces (JSF) - Spring integration mechanism?

Spring provides a custom JavaServer Faces VariableResolver implementation that extends the standard JavaServer Faces managed beans mechanism. When asked to resolve a variable name, the following algorithm is performed:

Does a bean with the specified name already exist in some scope (request, session, application)? If so, return it Is there a standard JavaServer Faces managed bean definition for this variable name? If so, invoke it in the usual way, and return the bean that was created. Is there configuration information for this variable name in the Spring WebApplicationContext for this application? If so, use it to create and configure an instance, and return that instance to the caller.

If there is no managed bean or Spring definition for this variable name, return null instead. BeanFactory also takes part in the life cycle of a bean, making calls to custom initialization and destruction methods. As a result of this algorithm, you can transparently use either JavaServer Faces or Spring facilities to create beans on demand.


Java J2ee Struts Hibernate Spring Interview Questions
1 2 3 4 5    Next
Menu - javaspartans.com - Java J2ee Struts Spring Hibernate Interview Questions
Hostens.com - A home for your website
Ads By Google