Wednesday, September 3, 2008

Web Services Interview Questions

What is a WebService
It allows the POJO classes to expose itself over the HTTP protocol, so that clients in different platforms can connect with each other.

There are two types of WebServices
1. SOAP ( Simple Object Access Protocol)
2. REST

Advantages of WebService?
Webservices allows programs written on two different platforms to work with each other.
  • Main component of Service Oriented Architecture
  • No location boundaries
  • Very easy, tools like Axis, XFire available to the job.
  • No overheads like that in RMI, CORBA
What is SOAP?
SOAP (Simple Object Access Protocol), is a XML based protocol to exchange information between client and server. The XML envelope has all the information about the function call and it parameters.
SOAP has two components
1. WSDL file (WebServices Definition Lang) : Its like the definining the java interface in XML, it allows the client to understand what methods are available , parameters and return types.
With tools like Apache Axis, CXF , commands like wsdl2java can be used to convert a java class into WSDL file.
2. SOAP Envelope
It is how data is exchanged , SOAP envelope is a XML file with a SOAP header and Body, body has all the data that is passed.

How are Exception Handled in WebServices?

Propagating errors back to a client is done by throwing exceptions. A Web service method can do this in the following ways:

* Throw a SoapException

* Throw a SoapHeader Exception

* Throw a Exception specific to the problem


REST WebServices

In REST webservices, the server uses the HTTP methods to invoke calls on the client. And the client responds by performing the action and returning a pure XML file , which has the data.

* HTTP Get - Used to retrieve data from server.

e.g http://abc.com/employees

* HTTP Post - Used to put data on server




Wednesday, August 27, 2008

EJB Interview Questions

1. What is EJB

EJB is a enterprise component, part of J2EE framework, which allows to create independant components to implement business logic or represent Data.

2. What are the Advantages of EJB
  • Component Driver architecture, so plug and play to any project
  • With local , remote interfaces present, the EJB itself does not have to know who the client is whether an RMI or local Servlet or something else. Developer have to focus on writing business logic, rest everthing like Remote access, Session management , Security is taken care by the framework.
3. Different Types of EJB
  • Session Beans - It has two types : Statefull and Statless session beans
    Stateless Session Beans : They behave like POJO classes, just write methods, they do not maintain session or state across the requests from the user.
Following is a Simple Example of EJB3.0 Stateless Session bean
Step1 : Write the Local and Remote Interfaces
package com.abc; import javax.ejb.Remote; @Remote public interface CalcBeanRemote { public int getRate(); }

Step2 : Write the Stateless Session Bean
package com.abc; import javax.ejb.Stateless;
@Stateless public class CalcBean implements CalcBeanRemote, CalcBeanLocal { /** * Default constructor. */ public CalcBean() { // TODO Auto-generated constructor stub } public int getRate(){ return 75; } }
  • Entity Beans : Provides the domain as well as DAO layer to the enterprise application. In EJB2.0 with CMP and BMP they were too complicated, Hence Hibernate was used for the database Interaction.
  • With EJB3, JPA has been introduced which is even better than Hibernate as it uses annotations. There is a good article on Java world about JPA. http://www.javaworld.com/javaworld/jw-01-2008/jw-01-jpa1.html
  • Message Driven Beans (MDB) : They execute the business logic on the OnMessage(Message obj) method , they listen to JMS Queues to get invoked. In the Deployment Descriptor(ejb-jar.xml , we mention the Queue to listen to)
4. How to invoke an EJB from a client e.g Servlet or Struts Action class or any other POJO.
Following are the steps to invoke a Ejb method
  • 1.Get hold of EJB HOME through JNDI Lookup.
EJBHome home = ctx. loopup("java:comp//ejbname);
Note: EJBHome has all the create methods to create the EJBObject

  • 2. Create instance of EjbObject from Home
EJBObject ejbObject = home.create();
Note: ejbObject is an interface and just provides the signatures of the all the methods in EJB itself.

  • 3. Invoke the method on the EJB.
Object result = ejbObject.convertDegreeToCentri("20);


5. What are Local and Remote interfaces in EJB and their importance..

Since EJBObjects are the interfaces which expose themselves to the client, for a remote client, we need a Remote Interface. And for local clients like Servlets we need Local Interfaces.
We two different because, we do not want have a overload of a Remote interface if the invocation is loca.

6. What version of EJB you have used?

I have used EJB2.x, lates is EJB 3.0 which makes heavy use of Annotations, thus eliminating the need of Home and EJBObject interfaces.

7. What is the Directory Structure of EJB Jar.
EjbProject --> EjbHome.java
EjbObject.java
Ejb.java
ejb-jar.xml (Deployment Descriptor which metions various settings like EJB Type, connection params, Listening queue etc)

Wednesday, August 13, 2008

Core Java Interview Questions - Differene Questions

1. Difference between Vector and ArrayList
2. Difference between Array and ArrayList




Answers
1. Vector and ArrayList?
  • All methods in vector are synchronized while in Arraylist they are not synchronized(not threadsafe)
  • When Vector reaches the end of its size, it doubles its size to accomdate new objects. whereas Arraylist grows by 50% of initial size.
2. Array and ArrayList?
  • Array is can store only a set similar objects and its size is fixed
  • ArrayList is internally an error, but can grow dynamically (so size is not fixed), its not synchronized.

Wednesday, July 30, 2008

Core Java Interview Questions

1. Abstract Class And Interface
2. Runtime(UnChecked) and CompileTime(Checked) exceptions
3. Deadlock and Race Condition
4. String and StringBuffer
5. List and ArrayList
6. Hashtable and Hashmap
7. Static and Non Static


Answer 1
  • Abstract class is a class which atleast has one method abstract or unimplemented while Interface only has method signatures and public constants only.
  • A class can implement many Interfaces but can extract only one class.
Note : Neither Interfaces or Abstract classes can be instantiated.

Answer 2
  • For compile time exeptions, Compiler forces the Caller method to either have a try/catch or throw the exception itself , thus making sure the Exception if thrown is well handled. Examples of Compile time exceptions are the UserDefined Exceptions, or any class which Extends the Exception class.
  • For Runtime exception, Compiler does not force the calling method, so potentially if a method throws an exception, the behaviour has be decided at Runtime.
    Examples include, NullPointer, ArrayIndexOutOfBounds exception etc.
Answer 3
  • Deadlock happens when two threads are holding each others resources and waiting for the other thread to release the resource first. Since each thread is waiting on the other to release the resource, both the threads will be always be in waiting condition resulting in a Deadlock.
  • Race condition, happens when two threads are trying for the resource, and any once can occupy it depending on who goes first. Thus giving rise to a random output.
Answer 4
  • String is a immutable , there is just one instance of the string. If the string is changed a new String gets created. Thus lot of objects are created
  • StringBuffer is used when the String object needs to be changed , so that we work on the same instance. StringBuffer.toString() can be used to convert it back to String. StringBuffer.append("abc") can be used to append the String to the StringBuffer.
Answer 5
  • List is an interface which is implemented by Vector, ArrayList
  • ArrayList is a class which implements List interface and stores objects. All methods in ArrayList are not threadsafe.

Answer 6
  • Hashtable and Hashmap have exactly same, only difference is, in Hashtable all the methods are Synchronized or ThreadSafe while Hashmap is not thread safe
  • So Hashmap is faster but not ThreadSafe
Answer 7
  • Static methods work on Class level, hence do not need an instance to call it
  • Non Static methods are instance level methods , so need an instance to call it.
  • Static variables are CLASS level , while instance variables work on an instance and are instance specific