Showing posts with label Java interview questions and answers. Show all posts
Showing posts with label Java interview questions and answers. Show all posts

Wednesday, October 14, 2015

EJB Interview Questions and Answers

8. Explain briefly about the EJB components ?
  • EJB Container : It provides runtime environment for the EJB components and services, such as Life cycle management, connection pooling and security.
  • EJB Component : It is a java class that follows the Enterprise Java Bean specification to implement the business logic of an application. Enterprise Java Bean components are deployed in container and are accessed using the remote and home interfaces of an enterprise bean.
  • EJB Object : Enterprise Java Bean container generates Enterprise Java Bean object, when a client needs to access an instance of the enterprise java bean. The Enterprise Java Bean object acts as an interface between the client and bean instances.
  • Remote Interface : It declares methods, which an enterprise bean provides to clients in order to enable them to perform business operations.
  • Home Object : Home objects are generated by Enterprise Java Bean container. Clients access Enterprise Java Bean objects that are stored at different java virtual machine, using the home object.
Task performed by EJB Object
  • Creating EJB objects
  • Searching EJB Objects
  • Removing EJB Objects
EJB Interfaces
  • Home Interface : It declares the various life cycle methods that involve operations, such as creating, finding, removing enterprise beans.
  • Local Interface : These are used when the enterprise bean and the client machine are stored in the same JVM.
9. what are the system services provided by EJB container?
Container provides system services such as:
  • Persistence
  • Security
  • Transaction
  • Connection pooling
  • Component lifecycle management
  • Threading
10. Mention the steps to call a remote method in EJB?
  • The client calls a local stub that is an object of the remote interface.
  • The stub marshals the parameters of the message into a form that can be sent over the network.
  • The call is sent over the network connection to the skeleton that is present at the server.
  • The skeleton demarshals the parameter in a form suitable for calling the EJB object.
  • The skeleton calls the EJB object that provides the middleware services, such as transaction and security.
  • The EJB object calls the instance of enterprise bean.
  • The Enterprise bean instance performs the tasks that are defined in the called method.

Tuesday, October 13, 2015

EJB Interview Questions and Answers

1. What is EJB?
Enterprise Java Beans are the server side components that run inside the EJB container and encapsulates the business logic of an enterprise application.
2. What is a J2EE container?
Containers are the interface between a component and the low level platform specific functionality that supports the component. The application server maintains control and provides services through an interface or framework known as a container.
3. What are the types of J2EE container types according to J2EE specification?
There are five defined J2EE container types (three server side and two client side). Three server side containers are as follows:
  • The server itself, which provides the J2EE runtime environment.
  • An EJB container to manage EJB components.
  • A web container to manage servlets and java server pages.
Two client side containers are as follows:
  • An application container for standalone GUIs, console, and batch type programs.
  • An applet container, meaning a browser, usually with the java plug-in.
4. what is a J2EE component and what are its types?
It is a self-contained functional software unit that is assembled into a J2EE application with its related classes and files and that communicates with other components. There are three types of components: 
  • Client components, which correlate to the client containers. 
  • Web components - Servlets and JSPs. 
  • EJB components - are business components that run on the server, which is logic that solves or meets the needs of a particular business domain such as banking, retail, or finance.

Monday, October 12, 2015

Servlet Interview Questions and Answers

6. Explain MVC design pattern?
Model-View-Controller (MVC) is a design pattern which divides a software application into three parts namely model, view and controller.
  • A model deals with behavior of the application. It contains the data and business logic of the application. It notifies views and controllers when there is change in its state.
  • A view renders the information to the user so that it looks attractive and appealing. It takes information from the model using which it generates output.
A controller takes input from user and sends command to model or view. It controls the flow of application.
7. Explain the web application directory structure?
web application’s directory structure has document root which contains JSP files, HTML files, JavaScript files and static files such as image files. There is a directory WEB-INF under the document root which contains configuration files related to application. This configuration files can’t be served directly to the client. The WEB-INF directory contains:
  • /WEB-INF/classes/* : It contains compiled class files
  • /WEB-INF/lib/*.jar : It contains different library jar files
  • /WEB-INF/web.xml : It is a deployment descriptor that specify the web application configuration.
8. what are the two packages that deal with servlets?
Servlet API exist in two packages:
  • Javax.servlet: The classes and interfaces in javax.servlet are not protocol dependent. e.g. It can support different protocols like HTTP, FTP.
  • Javax.servlet.http: The classes and interface in this package are specific for requests using HTTP protocol. Some of the classes and interfaces in this package extend those of javax.servlet package.
9. Explain servlet class hierarchy?
  • Interface Servlet: It declares the standaed API’s which all the servlet implementations needs to define. All servlet classes must implement this interface directly or indirectly.
  • Class GenericServlet: It defines a generic, protocol independent servlet. Servlet developed to cater NON-HTTP requests.
  • Class HTTPServlet: Servlet used to cater client HTTP requests.
10. What is Servlet interface?
The Servlet interface is the abstraction of Java Servlet API. It declares the life cycle methods of a servlet. All the servlet classes must implement it either directly or indirectly by extending a class which implements the Servlet interface. E.g. HTTPServlet, GenericServlet.

Sunday, October 11, 2015

Servlet Interview Questions and Answers

1. What is a servlet?
A servlet is a java program that runs within a web server and serves the client request hence the name servlet. Servlets can receive and respond to requests from web clients working on any client-server protocol, but HTTP is predominantly used. Servlet is a server side script technology which can process client request and give back the response.
2. What is a servlet container?
A servlet container is a container for servlets which is responsible for managing the servlets. The main functions are load, initialize and execute servlets to serve client requests. When a request is received, the container decides what servlet to call based on a configuration file. Servlet container comes installed with the web servers. Examples: Tomcat, Glassfish, JBoss.
3. What is CGI?
Common Gateway Interface(CGI) was the most commonly used for server side scripting before the evolution of the java servlet technology. With traditional CGI, a new process is started for each HTTP request eating up more of the CPU cycles thus degrading the performance of the system.
4. What are the advantages of servlet over CGI?
  • When a CGI script is called, a new process is created for each and every client request. But for servlets, each request is handled by a separate thread. Hence, servlet is more efficient because process creation takes more time compared to thread creation.
  • For each new CGI requests, CGI script is copied into memory. But with servlets, there is only one copy across requests and is shared among threads.
  • In CGI multiple process serves concurrent requests. But for servlets, only single instance serves concurrent requests.

Thursday, October 8, 2015

JSP Interview Questions and Answers

7.What are the types of scripting elements?
There are three types of scripting elements:
  • Scriptlets
  • Declaration
  • Expression
8. How to create a scriptlet?
Scriptlets are embedded between <% and %> delimiters.
Syntax: <% java code goes in here %>
9. What is declaration element?
  • Declarations are used to declare, define methods and instance variables.
  • Declaration tags does not produce any output that is sent to client.
  • The methods and classes declared will be translated as class level variables and methods during translation.
10. How to declare methods or variables in JSP?
Methods or variables are declared using <%! and %> delimiters.
Syntax: <%! variable=0; %>
11. What is expression element?
Expression element are used to write dynamic content back to the client browser. It is used in place of out.print() method. Only expressions are supported inside the tag. Declarations of methods and variables is not possible inside this tag.

Wednesday, October 7, 2015

JSP Interview Questions and Answers

1. What is a JSP?
JSP files are HTML files with special tags that contain java source code that provide dynamic content. Google search engine, the search page is dynamic will display the search results based on the user’s search request.
2. Compare between Servlet and JSP?
  • In servlets HTML is written inside java code using print statements. In JSP java code is embedded inside HTML.
  • JSP pages are converted to servlets by the web container so actually it can do the same thing as java servlets.
  • JSP are easier for creating HTML content but servlets are easier for writing the java code.
  • A combination of JSP and servlet can be used to separate the presentation(HTML) and logic (java code) and taking the advantage of both.
3. Explain JSP life cycle phases?
JSP life cycle phases are:
  • Translation and compilation: The JSP will be translated into servlet java file and compiled to servlet class by the web container.
  • Instantiation: The web container creates an instance of the servlet class.
  • Initialization: The web container instantiates the servlet and makes it ready for servicing the request.
  • Service: This is the phase during which the JSP services the user requests.
  • Destroy: JSP is destroyed by the web container when the application is uninstalled.
4. Explain JSP life cycle methods?
JSP life cycle methods are:
  • jspInit() – The web container calls the jspInit() to initialize the servlet instance generated. It is invoked before servicing the client request and invoke only once for a servlet instance.
  • _jspService() – The web container invokes the jspService() for each user request, passing it the request and the response objects.
  • jspDestroy() - The web container invokes this when it decides to take the instance out of service.

Wednesday, November 5, 2014

Advanced Java

1. Which containers use a FlowLayout as their default layout?
The Panel and Applet classes use the FlowLayout as their default layout.

2. What state does a thread enter when it terminates its processing?
When a thread terminates its processing, it enters the dead state.

3. What is the Collections API?
The Collections API is a set of classes and interfaces that support operations on collections of objects.

4. Which characters may be used as the second character of an identifier, but not as the first character of an identifier?
The digits 0 through 9 may not be used as the first character of an identifier but they may be used after the first character of an identifier.

5. What is the List interface?
The List interface provides support for ordered collections of objects.

Tuesday, November 4, 2014

Advanced Java

1. What is a transient variable?
A transient variable is a variable that may not be serialized.

2. Which containers use a border Layout as their default layout?
The Window, Frame and Dialog classes use a border layout as their default layout.

3. Why do threads block on I/O?
Threads block on I/O (that is enters the waiting state) so that other threads may execute while the I/O Operation is performed.

4. How are Observer and Observable used?
Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects.

5. What is synchronization and why is it important?
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 object while another thread is in the process of using or updating that object's value. This often leads to significant errors.

Saturday, January 15, 2011

Java interview questions and answers

Describe what happens when an object is created in Java?
Several things happen in a particular order to ensure the object is constructed properly:
1. Memory is allocated from heap to hold all instance variables and implementation-specific data of the object and its superclasses. Implementation-specific data includes pointers to class and method data.
2. The instance variables of the objects are initialized to their default values.
3. The constructor for the most derived class is invoked. The first thing a constructor does is call the constructor for its uppercase. This process continues until the constructor for java.lang.Object is called, as java.lang.Object is the base class for all objects in java.
4. Before the body of the constructor is executed, all instance variable initializers and initialization blocks are executed. Then the body of the constructor is executed. Thus, the constructor for the base class completes first and constructor for the most derived class completes last.

In Java, you can create a String object as below : String str = "abc"; & String str = new String("abc"); Why cant a button object be created as : Button bt = "abc"? Why is it compulsory to create a button object as: Button bt = new Button("abc"); Why this is not compulsory in String’s case?
Button bt1= "abc"; It is because "abc" is a literal string (something slightly different than a String object, by-the-way) and bt1 is a Button object. That simple. The only object in Java that can be assigned a literal String is java.lang.String. Important to not that you are NOT calling a java.lang.String constuctor when you type String s = "abc"; For example String x = "abc"; String y = "abc"; refer to the same object. While String x1 = new String("abc");
String x2 = new String("abc"); refer to two different objects.

What is the advantage of OOP?
You will get varying answers to this question depending on whom you ask. Major advantages of OOP are:
1. Simplicity: software objects model real world objects, so the complexity is reduced and the program structure is very clear;
2. Modularity: each object forms a separate entity whose internal workings are decoupled from other parts of the system;
3. Modifiability: it is easy to make minor changes in the data representation or the procedures in an OO program. Changes inside a class do not affect any other part of a program, since the only public interface that the external world has to a class is through the use of methods;
4. Extensibility: adding new features or responding to changing operating environments can be solved by introducing a few new objects and modifying some existing ones;
5. Maintainability: objects can be maintained separately, making locating and fixing problems easier;
6. Re-usability: objects can be reused in different programs

What are the main differences between Java and C++?
Everything is an object in Java( Single root hierarchy as everything gets derived from java.lang.Object). Java does not have all the complicated aspects of C++ ( For ex: Pointers, templates, unions, operator overloading, structures etc..) The Java language promoters initially said "No pointers!", but when many programmers questioned how you can work without pointers, the promoters began saying "Restricted pointers." You can make up your mind whether it’s really a pointer or not. In any event, there’s no pointer arithmetic. There are no destructors in Java. (automatic garbage collection), Java does not support conditional compile (#ifdef/#ifndef type). Thread support is built into java but not in C++. Java does not support default arguments. There’s no scope resolution operator :: in Java. Java uses the dot for everything, but can get away with it since you can define elements only within a class. Even the method definitions must always occur within a class, so there is no need for scope resolution there either. There’s no "goto " statement in Java. Java doesn’t provide multiple inheritance (MI), at least not in the same sense that C++ does. Exception handling in Java is different because there are no destructors. Java has method overloading, but no operator overloading. The String class does use the + and += operators to concatenate strings and String expressions use automatic type conversion, but that’s a special built-in case. Java is interpreted for the most part and hence platform independent

What are interfaces?
Interfaces provide more sophisticated ways to organize and control the objects in your system.
The interface keyword takes the abstract concept one step further. You could think of it as a “pure” abstract class. It allows the creator to establish the form for a class: method names, argument lists, and return types, but no method bodies. An interface can also contain fields, but The interface keyword takes the abstract concept one step further. You could think of it as a “pure” abstract class. It allows the creator to establish the form for a class: method names, argument lists, and return types, but no method bodies. An interface can also contain fields, but an interface says: “This is what all classes that implement this particular interface will look like.” Thus, any code that uses a particular interface knows what methods might be called for that interface, and that’s all. So the interface is used to establish a “protocol” between classes. (Some object-oriented programming languages have a keyword called protocol to do the same thing.) Typical example from "Thinking in Java":
import java.util.*;
interface Instrument {
int i = 5; // static & final
// Cannot have method definitions:
void play(); // Automatically public
String what();
void adjust();
}
class Wind implements Instrument {
public void play() {
System.out.println("Wind.play()");
public String what() { return "Wind"; }
public void adjust() {}
}

How can you achieve Multiple Inheritance in Java?
Java’s interface mechanism can be used to implement multiple inheritance, with one important difference from c++ way of doing MI: the inherited interfaces must be abstract. This obviates the need to choose between different implementations, as with interfaces there are no implementations.
interface CanFight {
void fight();
interface CanSwim {
void swim();
interface CanFly {
void fly();
class ActionCharacter {
public void fight() {}
class Hero extends ActionCharacter implements CanFight, CanSwim, CanFly {
public void swim() {}
public void fly() {}
}
You can even achieve a form of multiple inheritance where you can use the *functionality* of classes rather than just the interface:
interface A {
void methodA();
}
class AImpl implements A {
void methodA() { //do stuff }
}
interface B {
void methodB();
}
class BImpl implements B {
void methodB() { //do stuff }
}
class Multiple implements A, B {
private A a = new A();
private B b = new B();
void methodA() { a.methodA(); }
void methodB() { b.methodB(); }
}
This completely solves the traditional problems of multiple inheritance in C++ where name clashes occur between multiple base classes. The coder of the derived class will have to explicitly resolve any clashes. Don’t you hate people who point out minor typos? Everything in the previous example is correct, except you need to instantiate an AImpl and BImpl. So class Multiple would look like this:
class Multiple implements A, B {
private A a = new AImpl();
private B b = new BImpl();
void methodA() { a.methodA(); }
void methodB() { b.methodB(); }
}