Interview Questions and Answers on Java Related Technologies

Q1.  What are the benefits of using Spring Framework ?

Ans. Spring enables developers to develop enterprise-class applications using POJOs. The benefit of using only POJOs is that you do not need an EJB container product.

Spring is organized in a modular fashion. Even though the number of packages and classes are substantial, you have to worry only about ones you need and ignore the rest.

Spring does not reinvent the wheel instead, it truly makes use of some of the existing technologies like several ORM frameworks, logging frameworks, JEE, Quartz and JDK timers, other view technologies.

Testing an application written with Spring is simple because environment-dependent code is moved into this framework. Furthermore, by using JavaBean-style POJOs, it becomes easier to use dependency injection for injecting test data.

Spring’s web framework is a well-designed web MVC framework, which provides a great alternative to web frameworks such as Struts or other over engineered or less popular web frameworks.

Spring provides a convenient API to translate technology-specific exceptions (thrown by JDBC, Hibernate, or JDO, for example) into consistent, unchecked exceptions.

Lightweight IoC containers tend to be lightweight, especially when compared to EJB containers, for example. This is beneficial for developing and deploying applications on computers with limited memory and CPU resources.

Spring provides a consistent transaction management interface that can scale down to a local transaction

Q2.  Difference between C++ and Java ?

Ans. Java does not support pointers.

Java does not support multiple inheritances.

Java does not support destructors but rather adds a finalize() method. Finalize methods are invoked by the garbage collector prior to reclaiming the memory occupied by the object, which has the finalize() method.

Java does not include structures or unions because the traditional data structures are implemented as an object oriented framework.

C++ compiles to machine language , when Java compiles to byte code .

In C++ the programmer needs to worry about freeing the allocated memory , where in Java the Garbage Collector takes care of the the unneeded / unused variables.

Java is platform independent language but c++ is depends upon operating system.

Java uses compiler and interpreter both and in c++ their is only compiler.

C++ supports operator overloading whereas Java doesn't.

Internet support is built-in Java but not in C++. However c++ has support for socket programming which can be used.

Java does not support header file, include library files just like C++ .Java use import to include different Classes and methods.

There is no goto statement in Java.

There is no scope resolution operator :: in Java. It has . using which we can qualify classes with the namespace they came from.

Java is pass by value whereas C++ is both pass by value and pass by reference.

Java Enums are objects instead of int values in C++

C++ programs runs as native executable machine code for the target and hence more near to hardware whereas Java program runs in a virtual machine.

C++ was designed mainly for systems programming, extending the C programming language whereas Java was created initially to support network computing.

C++ allows low-level addressing of data. You can manipulate machine addresses to look at anything you want. Java access is controlled.

C++ has several addressing operators . * & -> where Java has only one: the .

We can create our own package in Java(set of classes) but not in c and c++.

Q3.  Why Struts 1 Classes are not Thread Safe whereas Struts 2 classes are thread safe ?

Ans. Struts 1 actions are singleton. So all threads operates on the single action object and hence makes it thread unsafe.

Struts 2 actions are not singleton and a new action object copy is created each time a new action request is made and hence its thread safe.

Q4.  What are some Java related technologies used for distributed computing ?

Ans. sockets, RMI. EJB

Q5.  What is the difference between AWT and Swing?

Ans. Swing provides both additional components like JTable, JTree etc and added functionality to AWT-replacement components.

Swing components can change their appearance based on the current “look and feel” library that’s being used.

Swing components follow the MVC paradigm, and thus can provide a much more flexible UI.

Swing provides extras for components, such as icons on many components, decorative borders for components, tool tips for components etc.

Swing components are lightweight than AWT.

Swing provides built-in double buffering ,which means an off-screen buffer is used during drawing and then the resulting bits are copied onto the screen.

Swing provides paint debugging support for when you build your own component.

Q6.  Name few tools for probing Java Memory Leaks ?

Ans. JProbe, OptimizeIt

Q7.  Difference between SAX and DOM Parser ?

Ans. A DOM (Document Object Model) parser creates a tree structure in memory from an input document whereas A SAX (Simple API for XML) parser does not create any internal structure.

A SAX parser serves the client application always only with pieces of the document at any given time whereas A DOM parser always serves the client application with the entire document no matter how much is actually needed by the client.

A SAX parser, however, is much more space efficient in case of a big input document whereas DOM parser is rich in functionality.

Use a DOM Parser if you need to refer to different document areas before giving back the information. Use SAX is you just need unrelated nuclear information from different areas.

Xerces, Crimson are SAX Parsers whereas XercesDOM, SunDOM, OracleDOM are DOM parsers.

Q8.  What is DTD ?

Ans. DTD or Document Type Definition is a standard agreed upon way of communication between two parties.  Your application can use a standard DTD to verify that data that you receive
from the outside world is valid and can be parsed by your parser.

Q9.  What is XSD ?

Ans. XSD or Xml Schema Definition is an extension of DTD. XSD is more powerful and extensible than DTD

Q10.  What is JAXP ?

Ans. Stands for Java API for XML Processing. This provides a common interface for creating and using SAX, DOM, and XSLT APIs in Java regardless of which vendor’s implementation is actually being used.

Q11.  What is JAXB ?

Ans. Stands for Java API for XML Binding. This standard defines a mechanism for writing out Java objects as XML and for creating Java objects from XML structures.

Q12.  What are LDAP servers used for ?

Ans. LDAP servers are typically used in J2EE applications to authenticate and authorise users. LDAP servers are hierarchical and are optimized for read access, so likely to be faster than database in providing read access.

Q13.  Explain Flow of Spring MVC ?

Ans. The DispatcherServlet configured in web.xml file receives the request.

The DispatcherServlet finds the appropriate Controller with the help of HandlerMapping and then invokes associated Controller.

Then the Controller executes the logic business logic and then returns ModeAndView object to the DispatcherServlet.

The DispatcherServlet determines the view from the ModelAndView object.

Then the DispatcherServlet passes the model object to the View.

The View is rendered and the Dispatcher Servlet sends the output to the Servlet container. Finally Servlet Container sends the result back to the user.

Q14.  Difference between socket and servlet ?

Ans. servlet is a small, server-resident program that typically runs automatically in response to user input.
A network socket is an endpoint of an inter-process communication flow across a computer network.

We can think of it as a difference between door and gate. They are similar as they both are entry points but they are different as they are put up at different areas.

Sockets are for low-level network communication whereas Servlets are for implementing websites and web services

Q15.  What is a Listener ?

Ans. In GUI programming, an object that can be registered to be notified when events of
some given type occur. The object is said to “listen” for the events.

Q16.  What is unicode ?

Ans. A way of encoding characters as binary numbers. The Unicode character set includes
characters used in many languages, not just English. Unicode is the character set that is
used internally by Java.

Q17.  What is PermGen or Permanent Generation ?

Ans. The memory pool containing all the reflective data of the java virtual machine itself, such as class and method objects. With Java VMs that use class data sharing, this generation is divided into read-only and read-write areas. The Permanent generation contains metadata required by the JVM to describe the classes and methods used in the application. The permanent generation is populated by the JVM at runtime based on classes in use by the application. In addition, Java SE library classes and methods may be stored here.

Q18.  What is metaspace ?

Ans. The Permanent Generation (PermGen) space has completely been removed and is kind of replaced by a new space called Metaspace. The consequences of the PermGen removal is that obviously the PermSize and MaxPermSize JVM arguments are ignored and you will never get a java.lang.OutOfMemoryError: PermGen error.

Q19.  What is an applet? What is the lifecycle of an applet?

Ans. Applet is a dynamic and interactive program that runs inside a web page displayed by a java capable browser.

Lifecycle methods of Applet -

init( ) method        -  Can be called when an applet is first loaded
start( ) method      -  Can be called each time an applet is started
paint( ) method     -  Can be called when the applet is minimized or maximized
stop( ) method      -  Can be used when the browser moves off the applet's page
destroy( ) method -  Can be called when the browser is finished with the applet

Q20.  What is session tracking and how do you track a user session in servlets?

Ans. Session tracking is a mechanism that servlets use to maintain state about a series requests from the same user across some period of time. The methods used for session tracking are:

User Authentication - occurs when a web server restricts access to some of its resources to only those clients that log in using a recognized username and password

Hidden form fields - fields are added to an HTML form that are not displayed in the client's browser. When the form containing the fields is submitted, the fields are sent back to the server

URL rewriting - every URL that the user clicks on is dynamically modified or rewritten to include extra information. The extra information can be in the form of extra path information, added parameters or some custom, server-specific URL change.

Cookies - a bit of information that is sent by a web server to a browser and which can later be read back from that browser.

HttpSession- places a limit on the number of sessions that can exist in memory.

Q21.  What is connection pooling?

Ans. It's a technique to allow multiple clients to make use of a cached set of shared and reusable connection objects providing access to a database or other resource.

Q22.  How does volatile affect code optimization by compiler?

Ans. Volatile is an instruction that the variables can be accessed by multiple threads and hence shouldn't be cached. As volatile variables are never cached and hence their retrieval cannot be optimized.

Q23.  How Java provide high Performance ?

Ans. Java uses Just-In-Time compiler to enable high performance. Just-In-Time compiler is a program that turns Java bytecode into instructions that can be sent directly to the processor.

Q24.  What are the advantages and Disadvantages of Sockets ?

Ans. Sockets are flexible and sufficient. Efficient socket based programming can be easily implemented for general communications. It cause low network traffic.

Socket based communications allows only to send packets of raw data between applications. Both the client-side and server-side have to provide mechanisms to make the data useful in any way.

Q25.  What are RESTful Web Services ?

Ans. REST or Representational State Transfer is a flexible architecture style for creating web services that recommends the following guidelines -

1. http for client server communication,
2. XML / JSON as formatiing language ,
3. Simple URI as address for the services and,
4. stateless communication.

Q26.  Which markup languages can be used in restful web services ?

Ans. XML and JSON ( Javascript Object Notation ).

Q27.  Why Web services use HTTP as the communication protocol ?

Ans. With the advent of Internet, HTTP is the most preferred way of communication. Most of the clients ( web thin client , web thick clients , mobile apps )  are designed to communicate using http only. Web Services using http makes them accessible from vast variety of client applications.

Q28.  What is Hibernate ?

Ans. Hibernate is a Java ORM Framework.

Q29.  What are the advantages of Hibernate ?

Ans. 1. No need to know SQL, RDBMS, and DB Schema.
2. Underlying Database can be changed without much effort by changing SQL dialect and DB connection.
3.Improved Performance by means of Caching.

Q30.  Difference between TCP and UDP ?

Ans. http://www.cyberciti.biz/faq/key-differences-between-tcp-and-udp-protocols/

Q31.  Name few Java Mocking frameworks ?

Ans. Mockito, PowerMock, EasyMock, JMock, JMockit

Q32.  Should we create system software ( e.g Operating system ) in Java ?

Ans. No, Java runs on a virtual machine called JVM and hence doesn't embed well with the underlying hardware. Though we can create a platform independent system software but that would be really slow and that's what we would never need.

Q33.  What is Java primarily used for ?

Ans. For creating platform independent software applications.

Q34.  What is the difference between JPA and Hibernate ?

Ans. JPA or Java Persistence API is a standard specification for ORM implementations whereas Hibernate is the actual ORM implementation or framework.

Q35.  What is the advantage of JPA ?

Ans. Its a specification that guides the implementation of ORM frameworks. Implementations abiding by the specification would mean that one can be replaced with other in an application without much hassle. Only the Features that are added over the specification needs to be taken care of if any such change is made.

Q36.  Which version control (VC) or software configuration management (SCM) systems work with Merge?

Ans. You can use Tortoise SVN,which has Merge Utility embedded in it.