Sunday, 27 September 2015

Top 10 Interview Questions

      1. What are the Data Types supported by Java? What is Autoboxing and Unboxing?

      One of the most common and fundamental Java interview questions.

     Ans :

     The eight Primitive Data types supported by Java are:
  • Byte : 8-bit signed two’s complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive)
  • Short : 16-bit signed two’s complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).
  • Int : 32-bit signed two’s complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive)
  • Long : 64-bit signed two’s complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive)
  • Float
  • Double


    2. What is Function Over-Riding and Over-Loading in Java?
     
    This is a very important concept in OOPS and is a must know for every Java Programmer.
    Ans:
    Over-Riding: An override is a type of function which occurs in a class which inherits from another class. An override function “replaces” a function inherited from the base class, but does so in such a way that it is called even when an instance of its class is pretending to be a different type through polymorphism. That probably was a little over the top. The code snippet below should explain things better.

    Over-Loading: Overloading is the action of defining multiple methods with the same name, but with different parameters. It is unrelated to either overriding or polymorphism. Functions in Java could be overloaded by two mechanisms ideally:
  • Varying the number of arguments.
  • Varying the Data Type.

    3. What is the difference between a JDK and a JVM?

    Ans : 
    JDK is Java Development Kit which is for development purpose and it includes execution environment also. But JVM is purely a run time environment and hence you will not be able to compile your source files using a JVM.

    4. Difference between Abstract Class and Interface

    Ans:
    1. abstract keyword is used to create an abstract class and it can be used with methods also whereas interface keyword is used to create interface and it can’t be used with methods.
    2. Subclasses use extends keyword to extend an abstract class and they need to provide implementation of all the declared methods in the abstract class unless the subclass is also an abstract class whereas subclasses use implements keyword to implement interfaces and should provide implementation for all the methods declared in the interface.
    3. Abstract classes can have methods with implementation whereas interface provides absolute abstraction and can’t have any method implementations.
    4. Abstract classes can have constructors but interfaces can’t have constructors.
    5. Abstract class have all the features of a normal java class except that we can’t instantiate it. We can use abstract keyword to make a class abstract but interfaces are a completely different type and can have only public static final constants and method declarations.
    6. Abstract classes methods can have access modifiers as public, private, protected, static but interface methods are implicitly public and abstract, we can’t use any other access modifiers with interface methods.
    7. A subclass can extend only one abstract class but it can implement multiple interfaces.
    8. Abstract classes can extend other class and implement interfaces but interface can only extend other interfaces.
    9. We can run an abstract class if it has main() method but we can’t run an interface because they can’t have main method implementation.
    10. Interfaces are used to define contract for the subclasses whereas abstract class also define contract but it can provide other methods implementations for subclasses to use.


    5. What is String in Java? String is a data type?
    Ans :

    String is a Class in java and defined in java.lang package. It’s not a primitive data type like int and long. String class represents character Strings. String is used in almost all the Java applications and there are some interesting facts we should know about String. String in immutable and final in Java and JVM uses String Pool to store all the String objects.
    Some other interesting things about String is the way we can instantiate a String object using double quotes and overloading of “+” operator for concatenation.


    6. Difference between String, StringBuffer and StringBuilder?
    Ans :
     
    String is immutable and final in java, so whenever we do String manipulation, it creates a new String. String manipulations are resource consuming, so java provides two utility classes for String manipulations – StringBuffer and StringBuilder.
    StringBuffer and StringBuilder are mutable classes. StringBuffer operations are thread-safe and synchronized where StringBuilder operations are not thread-safe. So when multiple threads are working on same String, we should use StringBuffer but in single threaded environment we should use StringBuilder.
    StringBuilder performance is fast than StringBuffer because of no overhead of synchronization.

     7. What is the difference between GET and POST method?

    Ans:
    • GET is a safe method (idempotent) where POST is non-idempotent method.
    • We can send limited data with GET method and it’s sent in the header request URL whereas we can send large amount of data with POST because it’s part of the body.
    • GET method is not secure because data is exposed in the URL and we can easily bookmark it and send similar request again, POST is secure because data is sent in request body and we can’t bookmark it.
    • GET is the default HTTP method whereas we need to specify method as POST to send request with POST method.
    • Hyperlinks in a page uses GET method.  

      8. Do we need to override service() method?
      Ans:
       
      When servlet container receives client request, it invokes the service() method which in turn invokes the doGet(), doPost() methods based on the HTTP method of request. I don’t see any use case where we would like to override service() method. The whole purpose of service() method is to forward to request to corresponding HTTP method implementations. If we have to do some pre-processing of request, we can always use servlet filters and listeners.
      9. Why HttpServlet class is declared abstract?

      Ans: 

      HttpServlet class provide HTTP protocol implementation of servlet but it’s left abstract because there is no implementation logic in service methods such as doGet() and doPost() and we should override at least one of the service methods. That’s why there is no point in having an instance of HttpServlet and is declared abstract class.

      10 .What are life cycle methods of a servlet?

       Ans :
      Servlet Life Cycle consists of three methods:
        1. public void init(ServletConfig config) – This method is used by container to initialize the servlet, this method is invoked only once in the lifecycle of servlet.
        2. public void service(ServletRequest request, ServletResponse response) – This method is called once for every request, container can’t invoke service() method until unless init() method is executed.
        3. public void destroy() – This method is invoked once when servlet is unloaded from memory.


     

Saturday, 4 July 2015

Deploy Your First Web Service in Tomcat


Deploy JAX-WS web services on Tomcat servlet container. Following  steps of a web service deployment.
  1. Create a web service .
  2. Create a sun-jaxws.xml, defines web service implementation class.
  3. Create a standard web.xml, defines WSServletContextListener, WSServlet and structure of a web project.
  4. Build tool to generate WAR file.
  5. Copy JAX-WS dependencies to “${Tomcat}/lib” folder.
  6. Copy WAR to “${Tomcat}/webapp” folder.
  7. Start It.
 Follow this Package Structure


HelloWorldService : End Point Interface





 HelloWorldServiceImpl : Implementation Class




This method will return the string when you will create the client you can use this method


sun-jaxws.xml: 
You need to create the this xml file to define the Name of the Web Service , implementation class to search the method in the class and url pattern to be used while calling the web service



<?xml version="1.0" encoding="UTF-8"?>
<endpoints
  xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
  version="2.0">
  <endpoint
      name="HelloWorldService"
      implementation="com.javatopoint.blogspot.com.HelloWorldServiceImpl"
      url-pattern="/helloService"/>
</endpoints>


Note: Implementation url will be depends your package name and implementation class name.



web.xml.


 Insert Listener class in web.xml file 
    com.sun.xml.ws.transport.http.servlet.WSServletContextListener
and servlate class 
       com.sun.xml.ws.transport.http.servlet.WSServlet





JAX-WS Dependencies

By default, Tomcat does not comes with any JAX-WS dependencies, So, you have to include it manually.
1. Go here http://jax-ws.java.net/.
2. Download JAX-WS RI distribution.
3. Unzip it and copy following JAX-WS dependencies to Tomcat library folder “{$TOMCAT}/lib“.
  • jaxb-impl.jar
  • jaxws-api.jar
  • jaxws-rt.jar
  • gmbal-api-only.jar
  • management-api.jar
  • stax-ex.jar
  • streambuffer.jar
  • policy.jar


Now Export your project to War file [ Right Click + Export + war ]

 Put war file in tomcat webapps folder












Now Restart your Tomcat server you will find the folder with your web service name with all class and .java files.


 You are all set to see that your webservice is deployed at tomcat

Go to browser and Enter the Url : localhost:Port_No/WAR_File_Name/Url_Pattern




When you click on the Url you will able to find the WSDL file.