Showing posts with label programming interview questions. Show all posts
Showing posts with label programming interview questions. Show all posts

Wednesday, 24 April 2013

Java interview questions and answers: - We have return in the try block, will finally block execute?


Let us first try to understand this Java interview question. You can see below is a simple Java code which where in we have put a return statement in the try block. So the question is will finally block execute.


public static void main(String[] args)
      {
            try
            {
                  int i=0;
                  return;
            }
            finally
            {
                  // will the control come here
                  int z=0;
            }
           
      }


The answer is Yes Finally block will execute irrespective there is return or there are no return statements. That’s why finally block is the best place to put clean up code.


Friday, 14 September 2012

Java Training: -What do you mean by Bootstrap, Extension and System Class loader?

There three types of class loaders:-
  • Bootstrap Class loader also called as primordial class loader.
  • Extension Class loader.
  • System Class loader.
Let’s now try to get the fundamentals of these class loaders.

Bootstrap Class loader

Bootstrap class loader loads those classes those which are essential for JVM to function properly. Bootstrap class loader is responsible for loading all core java classes for instance java.lang.*, java.io.* etc. Bootstrap class loader finds these necessary classes from “jdk/jre/lib/rt.jar”. Bootstrap class loader cannot be instantiated from JAVA code and is implemented natively inside JVM.

Extension Class loader

The extension class loader also termed as the standard extensions class loader is a child of the bootstrap class loader. Its primary responsibility is to load classes from the extension directories, normally located the “jre/lib/ext” directory. This provides the ability to simply drop in new extensions, such as various security extensions, without requiring modification to the user's class path.

System Class loader

The system class loader also termed application class loader is the class loader responsible for loading code from the path specified by the CLASSPATH environment variable. It is also used to load an application’s entry point class that is the "static void main ()" method in a class.

See the following video on overview and working of Servlets in Java: -



Click to get Java Training

Regards,

Get more Java Training from author’s blog

Saturday, 8 September 2012

Java Training: - Mention the concept of local interfaces?

One of the biggest issues of creating objects using home interface is performance. Below are the steps which follow when you call the EJB object:-
  • JAVA client calls the local stub.
  • Stub marshal the values in to some other form which the network understands and sends it to the skeleton.
  • Skeleton then de-marshals it back to a form which is suitable for JAVA.
  • Skeleton then calls the EJB object and methods.
  • EJB object then does object creation, connection pooling, transaction etc.
  • Once EJB object calls the bean and the bean completes its functionalities. All the above steps must again be repeated to reach back to the JAVA client.
So you can easily guess from the above step that its lot of work. But this has been improved in EJB 2.0 using Local objects. Local objects implement local interface rather than using remote interface. Just to have a comparison below are the steps how the local object works.
  • JAVA client calls the local object.
  • Local object does connection pooling, transactions and security.
  • It then passes calls the bean and when bean completes its work it returns the data to the Local object who then passes the same to the end client.
See the following video on Introduction to EJB in Java: -



Click to get Java Training

Regards,

Get more Java Training from author’s blog

Wednesday, 5 September 2012

Java Training: - Show different types of resultset?

“ResultSet” is an object that contains the results of SQL query. That means it contains the rows that satisfy the conditions of the query.

There are three basic types of resultsets:-

Forward-only results set

These types of resultset are non-scrollable and moves only forward.

Scroll-insensitive set

Resultset is scrollable so the cursor can move forward, backward, to a particular row etc.
While the resultset is open it does not show any changes done to the underlying database.

Scroll-sensitive set
Resultset is scrollable so the cursor can move forward, backward, to a particular row etc.
Resultset is sensitive to changes when it is open or used. That means if any values are modified in the database it’s propagated to the resultset.

See the following video on Factory Pattern in Java: -



Click to get Java Training

Regards,

Get more Java Training from author’s blog

Monday, 3 September 2012

Java Training: - Explain “ResultSet”, “RowSet”, “CachedRowset”, “JdbcRowset” and “WebRowSet” relation ship?

Below are the major points of difference:-
  • “ResultSet” is a connected architecture while “RowSet” is a disconnected architecture.
  • As “ResultSet” is a connected architecture we cannot serialize the object while “RowSet” can be serialized.
  • “RowSet” object is a Java bean while “ResultSet” is not. In the below diagram you can see “RowSet” interface also derives from “BaseRowSet” interface. “BaseRowSet” interface has all the ingredients to make it a Java bean.
Below diagram shows the complete relationship between all the interface and classes.


Figure: - Interface diagram for “Resultset” and “RowSet”
As "RowSet" is a disconnected from database it need to maintain Meta data for columns. "RowSet" can also provide scrollable resultsets or updatable resultsets even if the JDBC driver is not supporting the same. Java client can get a “RowSet” manipulate the same and finally send all the results to the database at one go. Sun has provided three implementation of “RowSet” as below:-

CachedRowSet: - Disconnected rowset always keeps the data in memory. It is scrollable and can also be serialized. It’s an ideal choice where we want to pass data between tiers or to do batch updates. "CachedRowSet" are disconnected so can be used for huge number of updates. Get the "CachedRowSet" object manipulate all your data and send the whole bunch of manipulated data in on go to the Database.

JDBCRowSet: - It’s opposite to "CachedRowSet". It maintains a connection to the database while the client has reference to it. So it’s a connected architecture as compared to CachedRowSet.

WebRowSet :- "WebRowSet" is a extension of "CachedRowSet". But the added feature is it can produce XML presentation of the data cached. If you are thinking of exposing your data through web services or to provide data to thin client which are written in different language other than JAVA. Best bet if you want to pass data in XML format over HTTP protocol.

See the following video on FlyWeight Pattern in Java: -



Click to get Java Training

Regards,

Get more Java Training from author’s blog

Wednesday, 29 August 2012

Java Training: - Differences between JNDI context, Initial context, session context and EJB context?


JNDI context
JNDI Context Provides a mechanism to lookup resources on the network

Initial context
Initial Context constructor provides the initial context.

Session context
Session Context has all the information a session bean would require from the container

Entity Context
Entity Context has all the information that an Entity bean would need from a container

EJB context
EJB Context contains the information that is common to both the session and entity bean

See the following video on EJB (Enterprise Java Beans): -



Click to get Java Training

Regards,

Get more Java Training from author’s blog

Wednesday, 22 August 2012

Java Training: - Elaborate stateful and stateless session bean?

Stateful Session bean:-
  • Stateful session bean maintain the state of the conversation between the client and itself
  • When the client invokes a method on the bean the instance variables of the bean may contain a state but only for the duration of the invocation
  • implements the javax.ejb.SessionBean interface and is deployed with the declarative attribute "stateful"
Stateless session bean:-
  • A stateless session bean is an enterprise bean that provides a stateless service to the client.
  • Conceptually, the business methods on a stateless session bean are similar to procedural applications or static methods; there is no instance state, so all the data needed to execute the method is provided by the method arguments
  • implements the javax.ejb.SessionBean interface and is deployed with the declarative attribute "stateless
See the following video on Factory Pattern in Hibernate: -



Click to get Java Training

Regards,

Get more Java Training from author’s blog

Saturday, 18 August 2012

Java Training: - Mention ORM and different levels of ORM quality?

ORM: -

ORM stands for Object/Relational mapping which is mainly used to remove the difference between object oriented and relation model

Different levels of ORM quality: -
  • Pure relational entire application, including the user interface, is designed around the relational model and SQL-based relational operations
  • Light object mapping The entities are represented as classes that are mapped manually to the relational tables
  • Medium object mapping The application is designed around an object model. The SQL code is generated at build time. And the associations between objects are supported by the persistence mechanism, and queries are specified using an object-oriented expression language
  • Full object mapping supports sophisticated object modeling: composition, inheritance, polymorphism and persistence
See the following video on Batch Processing in Hibernate: -



Click to get Java Training

Regards,

Get more Java Training from author’s blog

Monday, 13 August 2012

Java Training: - How will you explain marshaling and unmarshalling?

Marshaling: -

Marshalling creates an XML document from a content tree.

To marshal a content tree,
  • Create a JAXBContext object
  • Create a Marshaller object (Marshaller marshaller = jaxbContext.createMarshaller();)
  • Set required properties using setProperty method of Marshaller marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
  • Call the marshal method marshaller.marshal(collection, new FileOutputStream("jaxbOutput.xml"));
Unmarshalling: -

Unmarshalling an XML document means creating a tree of content objects that represents the content and organization of the document.

To unmarshal an XML document,
  • Create a JAXBContext object(JAXBContext jaxbContext = JAXBContext.newInstance("package name ");
  • Create an Unmarshaller object Unmarshaller unmarshaller = jc.createUnmarshaller();
  • Call the unmarshal method unmarshaller.unmarshal(new File( "xml name"));
  • Use the get methods in the schema-derived classes to access the XML data
See the following video on Introduction to Hibernate and it concepts in Java: -



Click to get Java Training

Regards,

Get more Java Training from author’s blog

Saturday, 11 August 2012

Java Interview Questions: - How will you explain navigation rules and how to declare the same?

Navigation rules tells JSF implementation which page to send back to the browser after a form has been submitted
The above configuration says that navigation will happen to page2.jsp if the action eventAction happens inside page1.jsp

See the following video on Getting Started with Servlet in Java: -



Click to get Java Interview Questions

Regards,

Get more Java Interview Questions from author’s blog

Thursday, 9 August 2012

Java Training: - Differences between DTDs and Schema?

Schema
DTD
Schema document is an XML document i.e., the structure of an XML document is specified by another XML documentDTDs follow SGML syntax
supports variety of data types similar to programming languageIn DTD everything is treated as text
creating relationship among elements is possibleThis is not possible in DTD without invalidating existing documents
Grouping of elements and attributes are possible to form a single logical unitGrouping of elements and attributes is not possible in DTD
it is possible to specify an upper limit for the number of occurrences of an elementIt is not possible to specify an upper limit of an element in DTDs

See the following video on overview on Servlets in Java: -



Click to get Java Training

Regards,

Get more Java Training from author’s blog

Friday, 3 August 2012

Java Training: - Elaborate Bean lifecycle in Spring framework?

  • The spring container finds the beans definition from the XML file and instantiates the bean
  • Using the dependency injection, spring populates all of the properties as specified in the bean definition
  • If the bean implements the BeanNameAware interface, the factory calls setBeanName() passing the bean’s ID
  • If the bean implements the BeanFactoryAware interface, the factory calls setBeanFactory(), passing an instance of itself
  • If there are any BeanPostProcessors associated with the bean, their post ProcessBeforeInitialization() methods will be called
  • If an init-method is specified for the bean, it will be called
  • Finally, if there are any BeanPostProcessors associated with the bean, their postProcessAfterInitialization() methods will be called
See the following video on Inheritance between beans and Spring in Java: -



Click to get Java Training

Regards,

Get more Java Training from author’s blog

Tuesday, 31 July 2012

Java Interview Questions: - Can you explain blocking Queues in Java?

The java.util.concurrent package contains a set of synchronized Queue interfaces and classes. Blocking Queue extends Queue with operations that wait for the queue to become nonempty when retrieving an element and for space to become available in the queue when storing an element. This interface is implemented by the following classes:
  • LinkedBlockingQueue — an optionally bounded FIFO blocking queue backed by linked nodes
  • ArrayBlockingQueue — a bounded FIFO blocking queue backed by an array
  • PriorityBlockingQueue — an unbounded blocking priority queue backed by a heap
  • DelayQueue — a time-based scheduling queue backed by a heap
  • SynchronousQueue — a simple rendezvous mechanism that uses the BlockingQueue interface
See the following video on Front Controller in Java: -



Click to get Java Interview Questions

Regards,

Get more Java Interview Questions from author’s blog

Saturday, 21 July 2012

Java Interview Questions: - Show Passivation and Activation in EJB?

When we are dealing with stateful session beans we need to store the client conversation of the bean so that it can be available in client’s next request. But when we talk about server it has limited resources. If the conversation of the bean is large then the server can run out of resource. So in order to preserve resources EJB server swaps this conversational data in memory to hard disk thus allowing memory to be reclaimed. This process of saving the memory data to hard disk is called as “Passivation”. Now when the client comes back the conversational data is again swapped from the hard disk to the bean. This process is called as “Activation”. The container informs the bean that its about to passivate or activate using the "ejbPassivate()" and "ejbActivate()" methods.

See the following video on Getting started with EJB in Java: -



Click to get Java Interview Questions

Regards,

Get more Java Interview Questions from author's blog

Thursday, 19 July 2012

Java Training: -Elaborate JAXR?

JAXR is a standard API used to access XML registries from the JAVA platform. An XML registry is a listing of services available on the Web. JAXR provides APIs for the client applications to query the registries, or publish their own information in them using the registry standards.

It acts as a pluggable layer that allows access to registries implemented on different standards, such as Universal Description Discovery and Integration (UDDI) and Electronic Business using eXtensible Markup Language (ebXML). You can see from the figure below how JAXR API interacts with the respective provider to get data.


Figure: - JAXR architecture
See the following video on Overview and working of Servlets: -



Click to get Java Training

Regards,

Get more Java training stuffs from author's blog

Saturday, 14 July 2012

Java Training: -Mention about SOAP?

SOAP is an XML-based protocol that enables software components and applications to communicate with one another. It defines rules to translate application and platform-specific data into the XML format. SOAP allows you to communicate with the Web Service using protocols such as HTTP and Simple Mail Transfer Protocol.

SOAP has three main sections:-

Envelope: Contains elements such as the header and body of the SOAP messaging structure. It also includes an encodingStyle attribute that specifies the representation of data in messages.

Header: Encapsulates extended messages without adding or modifying the standard message flow.

Body: Contains Web application-specific data. It defines the purpose of sending the message. The body element should be the first element under the envelope element if there is no header element.

Below is a snippet of a sample SOAP header.

See the following video on Web service in Java: -



Click to get Java Training

Regards,

Get more Java training stuffs from author's blog

Wednesday, 11 July 2012

Java Training: - What are the different scopes of an object can have in a JSP page?

There are four scope which an object can have in a JSP page:-

Page Scope

Objects with page scope are accessible only within the page. Data only is valid for the current response. Once the response is sent back to the browser then data is no more valid. Even if request is passed from one page to other the data is lost.

Request Scope

Objects with request scope are accessible from pages processing the same request in which they were created. Once the container has processed the request data is invalid. Even if the request is forwarded to another page, the data is still available though not if a redirect is required.

Session Scope

Objects with session scope are accessible in same session. Session is the time users spend using the application, which ends when they close their browser or when they go to another Web site. So, for example, when users log in, their username could be stored in the session and displayed on every page they access. This data lasts until they leave the Web site or log out.

Application Scope

Application scope objects are basically global object and accessible to all JSP pages which lie in the same application. This creates a global object that's available to all pages. Application scope variables are typically created and populated when an application starts and then used as read-only for the rest of the application.

See the following video on getting started with servlet: -



Click to get Java Training

Regards,

Get more Java training stuffs from author's blog

Wednesday, 4 July 2012

Java Training: - What do you mean by four essential properties of a transaction?

A transaction is proper if it fulfils ACID properties. These four properties are as below:-

Atomicity: - This rule states if one part of the transaction fails then the entire transaction should fail.

Consistency: - This rule states that only valid data should be written to database. Any invalid data should roll back the whole transaction. If the transaction executes successfully then it should take database from one state which is consistent to other state which is also consistent.

Isolation: - This rule states that multiple transactions occurring at the same time should not impact each other. If “shiv” and “raju” is withdrawing and depositing money both the transaction should operate in an isolated manner. Isolation ensures that transactions do not affect each other.

Durability: - This rule states that any transaction committed should not be lost. It’s ensured through database backups and transaction logs in database. So that if there is a problem at any point we can restore back to the original state.

See the following video on Service Loader in Java : -



Click to get Java Training

Regards,

Get more Java training stuffs from author's blog

Thursday, 28 June 2012

Java Training: - How do you implement inheritance in Java?

To understand this better lets do a small project to get the actual feel of the same.

Below is the project structure of the “Inheritance” project. It has the three classes:-

• “ClsAdd.java”
This class has a basic subroutine which adds two numbers.

• “ClsSpecialAdd.java”
This class inherits from “ClsAdd.java” and overrides the subroutine which does the basic addition. It changes the basic addition functionality a bit by adding the two numbers but also adds up an extra adjustment value “20”.

• “ClsRun.java”
This is the class which has the “public static void main (String [] args)” method. It’s the main class which will run the whole show by creating the objects of “ClsAdd” and “ClsSpecialAdd” and executing the “Add” method.

Figure: - Project File structure of inheritance structure

Figure: - Inheritance in Action


When you run the project you can see two outputs one from the parent class and second from the child class with the adjustment value added up. So the child class has made the parent add class more specialized.

Figure: - Output from Inheritance Project


See the following video on Inheritance between beans and Springin Java: -



Click to get Java Training

Regards,

Get more Java training stuffs from author's blog