All java c# asp.net .net servlet jdbc c sqlserver android ado.net javascript php wcf clearcase angularjs 2

What is Java Virtual Machine and how it is considered in context of Java’s platform independent feature?

When Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web and interpreted by Java Virtual Machine (JVM) on whichever platform it is being run.

Platform independency is one of the key aspect that Java provides and the question you have asked is a very common and basic one. I'm trying to explain it below. Hope it will help.

Explanation :

The java compiler compiles a java program and converts it into bytecode because of which a java program can run on wide variety of platforms.

Java Virtual Machine(JVM) is a Java run time system. It is platform dependent i.e its implementation differs from platform to platform. It provides the runtime environment in which java bytecode can be executed.

In order to run a java program on different platforms, only we need to implement platform specific JVMs and these all will execute the same byte code.This is the feature due to which java is considered as platform independent.

Explanation :

The java compiler compiles a java program and converts it into bytecode because of which a java program can run on wide variety of platforms. 

Java Virtual Machine(JVM) is a Java run time systemIt is platform dependent i.e its implementation differs from platform to platform. It provides the runtime environment in which java bytecode can be executed.

In order to run a java program on different platforms, only we need to implement platform specific JVMs and these all will execute the same byte code.This is the feature due to which java is considered as platform independent.

Explain servlet life cycle.

A servlet life cycle can be defined as the entire process from its creation till the destruction. The following are the paths followed by a servlet.

  • The servlet is initialized by calling the init () method.
  • The servlet calls service() method to process a client's request.
  • The servlet is terminated by calling the destroy() method.
  • Finally, servlet is garbage collected by the garbage collector of the JVM.
The web container maintains the life cycle of a servlet instance. Let's see the life cycle of the servlet:
  1. Servlet class is loaded.
  2. Servlet instance is created.
  3. init method is invoked.
  4. service method is invoked.
  5. destroy method is invoked
there are three states of a servlet: new, ready and end. The servlet is in new state if servlet instance is created. After invoking the init() method, Servlet comes in the ready state. In the ready state, servlet performs all the tasks. When the web container invokes the destroy() method, it shifts to the end state.

1) Servlet class is loaded

The classloader is responsible to load the servlet class. The servlet class is loaded when the first request for the servlet is received by the web container.

2) Servlet instance is created

The web container creates the instance of a servlet after loading the servlet class. The servlet instance is created only once in the servlet life cycle.

3) init method is invoked
The web container calls the init method only once after creating the servlet instance. The init method is used to initialize the servlet. It is the life cycle method of the javax.servlet.Servlet interface. Syntax of the init method is given below:
  1. public void init(ServletConfig config) throws ServletException  
 

4) service method is invoked

The web container calls the service method each time when request for the servlet is received. If servlet is not initialized, it follows the first three steps as described above then calls the service method. If servlet is initialized, it calls the service method. Notice that servlet is initialized only once. The syntax of the service method of the Servlet interface is given below:
  1. public void service(ServletRequest request, ServletResponse response)   
  2.   throws ServletException, IOException  
 

5) destroy method is invoked

The web container calls the destroy method before removing the servlet instance from the service. It gives the servlet an opportunity to clean up any resource for example memory, thread etc. The syntax of the destroy method of the Servlet interface is given below:
  1. public void destroy()  
 
  1. Loading Servlet Class : A Servlet class is loaded when first request for the servlet is received by the Web Container.
  2. Servlet instance creation :After the Servlet class is loaded, Web Container creates the instance of it. Servlet instance is created only once in the life cycle.
  3. Call to the init() method : init() method is called by the Web Container on servlet instance to initialize the servlet.

    Signature of init() method :

    public void init(ServletConfig config) throws ServletException
    
  4. Call to the service() method : The containers call the service() method each time the request for servlet is received. The service() method will then call the doGet() or doPost() methos based ont eh type of the HTTP request, as explained in previous lessons.

    Signature of service() method :

    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
    
  5. Call to destroy() method: The Web Container call the destroy() method before removing servlet instance, giving it a chance for cleanup activity.

What is Downcasting?

suppose you have a base class, and a class that derives from that base class either directly or indirectly. Then, anytime an object of that base class type is type cast into a derived class type, it is called a downcast.

When reference variable of child cl;ass refers to the object of parent class, it is known as downcasting, viceversa of upcasting. By the real time example i ll explain.

 If you want to give somebody a gift. You can chose from multiple things like pen, jewelry ,keychain etc. Now every item itself is a type of gift.When you give a gift you can chose to give it directly as item itself or as a gift by putting it inside a gift box.Now when we say gift = pen it is correct. But when we want to get a pen inside the gift box, then we need to UNWRAP the gift box like pen = (pen) (from the gift box).Let us see this in terms of the code. We have parent class gift and many children classes like Pen, KeyChain etc as mentioned above.

class Gift { }

class Pen extends Gift { }

So here upcasting means Gift objGift = new Pen() //it is automatic as pen is a gift

But down casting means Pen objPen = (Pen) (objGift) //It is explicit as gift needs to be unwrapped to get the pen inside.

UpCast -> From lower level class type to upper level class type

Downcast -> From upper level class type to lover level class type

Downcasting is assigning parent class reference object to the sub class which is not allowed in Java. However, if you do downcasting, there will not be any compiler error. But, there will be runtime exception in java.lang.ClassCastException.  Downcasting is legal in some scenarios where the actual object referred by the parent class is of sub class.

Example:
public class DownCastingExample {

	public static void main(String[] args) {
		SuperClass superClass1 = new SuperClass();
		SuperClass superClass2 = new SubClass();

		//valid downcasting
   SubClass subClass1 = (SubClass)superClass2;
        subClass1.method();
        //Invalid down casting
        SubClass subClass2 = (SubClass)superClass1;
    }
}

The reason it’s called a downcast is because of the way that inheritance diagrams are normally written – base classes are at the top and derived classes are down below the base classes. So, in downcasting, you are going down the inheritance diagram by taking an object of a base class (at the top), and then trying to convert into the type of one of the derived classes (going down).So,It is the casting from a general to a more specific type, i.e. casting down the hierarchy.

DownCasting is also a kinf of Boxing . Where One Class is created as Reference of the Other Class. For Example class Hotel { } class Room: Hotel { } Here Hotel contains many rooms if (room is Hotel) { Hotel m = (Hotel)room; }

What is drawable folder in android?

A compiled visual resource that can used as a backgrounds,banners, icons,splash screen etc.
By default Android maintain three folders for the images with different resolution reason behind it is the use or the resolution of the Android Device on which the application gonna execute. hdpi image folder maintain images for the Android Broad Screen set or Android Phones with the Higher resolution. ldpi for Lower images quality which supports by the earlier sets of the android mdpi for medium images support xhdi images folder for devices with maximum resolution. Android OS select the image it self by checking the compatible device and its resolution. Hope it helps. Accept if you get your explanation.

What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy?

The Reader/Writer class hierarchy is character-oriented, and the InputStream/OutputStream class hierarchy is byte-oriented.

reader/writer support only cahracter and input/output 
stream support only bytes input stram and the java reader 
class has only one differnce and that is input stream only 
support bytes and the reader class only supports the 
character.

What is URL rewriting?

You can append some extra data on the end of each URL that identifies the session, and the server can associate that session identifier with data it has stored about that session. For example, with http://tutorialspoint.com/file.htm;sessionid=12345, the session identifier is attached as sessionid=12345 which can be accessed at the web server to identify the client.

URL manipulation, also called URL rewriting, is the process of altering (often automatically by means of a program written for that purpose) the parameters in aURL (Uniform Resource Locator). URL manipulation can be employed as a convenience by a Web server administrator, or for nefarious purposes by a hacker.

What is a JDBC Driver?

JDBC driver is an interface enabling a Java application to interact with a database. To connect with individual databases, JDBC requires drivers for each database. The JDBC driver gives out the connection to the database and implements the protocol for transferring the query and result between client and database.

JDBC Driver is a  an application program interface (API).  that enables java application to interact with the database.There are 4 types of JDBC drivers:

There are 4 types of JDBC drivers:

  • JDBC-ODBC bridge driver
  • Native-API driver (partially java driver)
  • Network Protocol driver (fully java driver)
  • Thin driver (fully java driver)

1) JDBC-ODBC bridge driver

The JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The JDBC-ODBC bridge driver converts JDBC method calls into the ODBC function calls. This is now discouraged because of thin driver.

Advantages:

  • easy to use.
  • can be easily connected to any database.

Disadvantages:

  • Performance degraded because JDBC method call is converted into the ODBC function calls.
  • The ODBC driver needs to be installed on the client machine.

2) Native-API driver

The Native API driver uses the client-side libraries of the database. The driver converts JDBC method calls into native calls of the database API. It is not written entirely in java.

Advantage:

  • performance upgraded than JDBC-ODBC bridge driver.

Disadvantage:

  • The Native driver needs to be installed on the each client machine.
  • The Vendor client library needs to be installed on client machine.

3) Network Protocol driver

The Network Protocol driver uses middleware (application server) that converts JDBC calls directly or indirectly into the vendor-specific database protocol. It is fully written in java.

Advantage:

  • No client side library is required because of application server that can perform many tasks like auditing, load balancing, logging etc.

Disadvantages:

  • Network support is required on client machine.
  • Requires database-specific coding to be done in the middle tier.
  • Maintenance of Network Protocol driver becomes costly because it requires database-specific coding to be done in the middle tier.

4) Thin driver

The thin driver converts JDBC calls directly into the vendor-specific database protocol. That is why it is known as thin driver. It is fully written in Java language.

Advantage:

  • Better performance than all other drivers.
  • No software is required at client side or server side.

Disadvantage:

  • Drivers depends on the Database.

What are Wrapper classes?

These are classes that allow primitive types to be accessed as objects. Example: Integer, Character, Double, Boolean etc.

Wrapper class in java provides the mechanism to convert primitive into object and object into primitive.

ince J2SE 5.0, autoboxing and unboxing feature converts primitive into object and object into primitive automatically. The automatic conversion of primitive into object is known as autoboxing and vice-versa unboxing.

The eight classes of java.lang package are known as wrapper classes in java. The list of eight wrapper classes are :

Primitive TypeWrapper class
booleanBoolean
charCharacter
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
 

it is used to convert primitive into object and object into primitive data type.

Why Java is considered dynamic?

It is designed to adapt to an evolving environment. Java programs can carry extensive amount of run-time information that can be used to verify and resolve accesses to objects on run-time.

Java is considered as Dynamic because of Bytecode[a class file]. A source code writen in one platform, the same code can be executed in any platform[ which JDK is installed.]. And it also loads the class files at runtime. anything that happes at runtime is considered as Dynamic

Because byte code and source code written in one platform ,but some code will run on JDK thats why java is considered as a dynamic language.

What things should be kept in mind while creating your own exceptions in Java?

While creating your own exception -

  1. All exceptions must be a child of Throwable.
  2. If you want to write a checked exception that is automatically enforced by the Handle or Declare Rule, you need to extend the Exception class.
  3. You want to write a runtime exception, you need to extend the RuntimeException class.

Required files
Client.java [ My business logic will goes here ]
MyOwnExceptionClass.java  [ This is our own Exception class ]

  1. All exceptions must be a child of Throwable.
  2. If you want to write a checked exception that is automatically enforced by the Handle or Declare Rule, you need to extend the Exception class.