Q1.What is J2EE?
J2EE is an environment for developing and deploying enterprise applications. The J2EE platform consists of a set of services, application programming interfaces (APIs), and protocols that provide the functionality for developing multi tiered, and web-based applications.
Q2.What is Generic Servlet ?
Defines a generic, protocol-independent servlet. GenericServlet implements the Servletand ServletConfig interfaces. GenericServlet makes writing servlets easier. It provides simple versions of the lifecycle methods in it and destroy. To use Protocol specific servlet like http protocol, we can use HttpServlet. Difference in HttpServlet and GenericServlet is that, HttpServlet provides few more methods like doGet and doPost. Same operations can be done in service method of generic servlet. Read Servlet Life cycle
Q3. What is the J2EE module?
A J2EE module consists of one or more J2EE components for the same container type and one component deployment descriptor of that type.
Q4. Can abstract class have Constructor ?
Yes. abstract class can have a constructor.
Q5.What are the components of J2EE application?
A J2EE component is a self-contained functional software unit that is assembled into a J2EE application with its related classes and files and communicates with other components. The J2EE specification defines the following J2EE components:
Application clients and applets are client components.
Java Servlets and Java Server Pages(JSP) technology components are web components.
Enterprise JavaBeans (EJB) components (enterprise beans) are business components.
Resource adapter components provided by EIS and tool vendors.
Q6. We cannot instantiate the abstract class, then what is the need of constructor and how it is called?
You may have not come across this situation. but answer i very easy. If want to initialize few parameters for all the subclasses then we can use the constructor of abstract class. Constructor calling mechanism is same as normal inheritance in java. Default constructor gets called automatically and to call other constructors we have to explicitly call it by using keyword “super”.
Q7. What are the four types of J2EE modules?
- Application client module
- Web module
- Enterprise JavaBeans module
- Resource adapter module
Q8. Does java have virtual function ?
As such there is no keyword “virtual” present in java. but except static, final and private method all the methods are virtual bu default. Refer this article to read more on it.
Q9. What does application client module contain?
The application client module contains:class files,an application client deployment descriptor.
Application client modules are packaged as JAR files with a .jar extension.
Q10. What are different ways to create Action classes in Struts2?
Struts2 provide different ways to create action http://classes.By implementing Action interfaceUsing Struts2 @Action annotationBy extending ActionSupport classAny normal java class with execute() method returning String can be configured as Action class.
Q11. How you will create custom tag in JSP ?
Create tld file in “WEB-INF/tlds” folder and configure required properties.
Create a class which implements interface Tag / BodyTag or extends class TagSupport / BodyTagSupport. In that class write getter and setter for defined property. Few methods required by interface are doStartTag(), doEndTag() and release(). Read how to create custom tagin detail.
Q12. What does resource adapt module contain?
The resource adapt module contains:
1.all Java interfaces
5.A resource adapter deployment descriptor.
Resource adapter modules are packages as JAR files with a .rar (Resource adapter Archive) extension.
Q13. What is Externalization ?
Externalization is the interface provided by the JAVA which extends interface Serializable. It provides two more methods as compared to Serializable interface –
void readExternal(ObjectInput in)
void writeExternal(ObjectOutput out)
The above two methods gives the flexibility and control over how the object is serialized and deserialized. for further read refer this article.
Q14. How many development roles are involved in J2EE application?
There are at least 5 roles involved:
Enterprise Bean Developer
Writes and compiles the source code
Specifies the deployment descriptor
Bundles the .class files and deployment descriptor into an EJB JAR file
Web Component Developer
Writes and compiles Servlets source code
Writes JSP and HTML files
Specifies the deployment descriptor for the Web component
Bundles the .class, .jsp, .html, and deployment descriptor files in the WAR file
J2EE Application Client Developer
Writes and compiles the source code
Specifies the deployment descriptor for the client
Bundles the .class files and deployment descriptor into the JAR file
Application Assembler The application assembler is the company or person who receives application component JAR files from component providers and assembles them into a J2EE application EAR file. The assembler or deployer can edit the deployment descriptor directly or use tools that correctly add XML tags according to interactive selections. A software developer performs the following tasks to deliver an EAR file containing the J2EE application:
Assembles EJB JAR and WAR files created in the previous phases into a J2EE application (EAR) file
Specifies the deployment descriptor for the J2EE application
Verifies that the contents of the EAR file are well formed and comply with the J2EE specification
Application Deployer and Administrator
Configures and deploys the J2EE application
Resolves external dependencies
Specifies security settings & attributes
Assigns transaction attributes and sets transaction controls
Specifies connections to databases
Deploys or installs the J2EE application EAR file into the J2EE server
Administers the computing and networking infrastructure where J2EE applications run
Oversees the runtime environment
But a developer role depends on the job assignment. For a small company, one developer may take these 5 roles altogether.
Q15. What is the use of serialVersionUID in JAVA ?
Whenever object is created, one unique ID is associated with object and its implementation may defer from compiler to compiler. Whenever any object is serialized and deserealized, this variable is used to determine whether the object is in same state or not ? assignment of default serialVersionUID is mostly depends upon the methods and fields present in class and if any field is added or removed from the class definition then its serialVersionUID also changes and if the value does not match then it results in “InvalidClassException”. Because of this reason it is recommended to have a default unique serialVersionUID with each serialized undergoing class. To read in detail with example, refer this article.
Q16. What is difference between J2EE 1.3 and J2EE 1.4?
J2EE 1.4 is an enhancement version of J2EE 1.3. It is the most complete Web services platform ever.
J2EE 1.4 includes:
Java API for XML-Based RPC (JAX-RPC 1.1)
SOAP with Attachments API for Java (SAAJ),
Web Services for J2EE(JSR 921)
J2EE Management Model(1.0)
J2EE Deployment API(1.1)
Java Management Extensions (JMX),
Java Authorization Contract for Containers(JavaACC)
Java API for XML Registries (JAXR)
J2EE Connector 1.5
The J2EE 1.4 features complete Web services support through the new JAX-RPC 1.1 API, which supports service endpoints based on Servlets and enterprise beans. JAX-RPC 1.1 provides interoperability with Web services based on the WSDL and SOAP protocols.
The J2EE 1.4 platform also supports the Web Services for J2EE specification (JSR 921), which defines deployment requirements for Web services and utilizes the JAX-RPC programming model.
In addition to numerous Web services APIs, J2EE 1.4 platform also features support for the WS-I Basic Profile 1.0. This means that in addition to platform independence and complete Web services support, J2EE 1.4 offers platform Web services interoperability.
The J2EE 1.4 platform also introduces the J2EE Management 1.0 API, which defines the information model for J2EE management, including the standard Management EJB (MEJB). The J2EE Management 1.0 API uses the Java Management Extensions API (JMX).
The J2EE 1.4 platform also introduces the J2EE Deployment 1.1 API, which provides a standard API for deployment of J2EE applications.
The J2EE 1.4 platform includes security enhancements via the introduction of the Java Authorization Contract for Containers (JavaACC). The JavaACC API improves security by standardizing how authentication mechanisms are integrated into J2EE containers.
The J2EE platform now makes it easier to develop web front ends with enhancements to Java Servlet and JavaServer Pages (JSP) technologies. Servlets now support request listeners and enhanced filters. JSP technology has simplified the page and extension development models with the introduction of a simple expression language, tag files, and a simpler tag extension API, among other features. This makes it easier than ever for developers to build JSP-enabled pages, especially those who are familiar with scripting languages.
Q17. If i don’t want to serialize some fields in class, then how to achieve this ?
This question can also be asked as “What is the transientvariable ?”. “transient variables and static variables” can be used to avoid the field being serialized in Java. (As static variable is class level variable, and it does not associated at object level – it is not serialized).
Q18. What will happen if one of the members in the class doesn’t implement Serializable interface?
If you try to serialize an object of a class which implements Serializable, but the object includes a reference to an non- Serializable class then a “˜NotSerializableException‘ will be thrown at runtime.
Q19. If a class is Serializable but its super class in not, what will be the state of the instance variables inherited from super class after deserialization?
Java serialization process only continues in object hierarchy till the class is Serializable and values of the instance variables inherited from super class will be initialized by calling constructorof Non-Serializable Super class during deserialization process .
Q20. What is the conceptual difference between interface and abstract class ? When to use interface and abstract class ?
Abstract class and its usage :
A class which is partially implemented and have few abstract methods are know an “abstract class“. few developers think that why anybody should create a partial implementation ? The best example i have in my mind is “DataMapper Pattern”. For the CRUD operation – open connection and close connection and sequence of save method will be known to the programmer however which query will be executed to save or retrieve differs for different object. in that case createInsertSQL() method can be made abstract and internally save() will call openConnection() and closeConnection() which is implemented. below code snap explains the save() method.
abstract public String createInsertSQL();//abstract method
public void save() //method implemented
String sql = createInsertSQL(); //abstract method
Interface and its usage :As you can see in above code snap, only unknown implementation is the SQL Query which is made abstract and others are implemented. and therefore underlying child class must implement the method createInsertSQL() and call save().interface are also known as “contract”. when we know the skeleton/ structure of the class but it complete implementation differs then we must use interface. in following scenario we must use interface :
In design pattern programming to interface is preferable rather than programming to implementation (inheritance).
Q21. How to iterate over keys of HashMap in JDK 4 and 5?
This is the common question asked in interview.
In JAVA 5 : we can use advance for loop as shown in above code, use map.keySet(). This will return the Set (As Keys must be unique)
In JAVA 4 : use map.keySet() and get the Iterator object using map.iterate() . then using while loop , get the value for each key.