Friday, June 11, 2010

Java J2EE conceptual questions and answers

Q 01: Give a few reasons for using Java?
A 01: Java is a fun language. Let’s look at some of the reasons:
• Built-in support for multi-threading, socket communication, and memory management (automatic garbage
• collection).
• Object Oriented (OO).
• Better portability than other languages across operating systems.
• Supports Web based applications (Applet, Servlet, and JSP), distributed applications (sockets, RMI. EJB etc) and network protocols



Q 02: What is the main difference between the Java platform and the other software platforms?
A 02: Java platform is a software-only platform, which runs on top of other hardware-based platforms like UNIX, NT etc.
The Java platform has 2 components:
• Java Virtual Machine (JVM) – ‘JVM’ is a software that can be ported onto various hardware platforms. Byte
• codes are the machine language of the JVM.
• Java Application Programming Interface (Java API)


Q 03: What is the difference between C++ and Java?
A 03: Both C++ and Java use similar syntax and are Object Oriented, but:
• Java does not support pointers. Pointers are inherently tricky to use and troublesome.
• Java does not support multiple inheritances because it causes more problems than it solves. Instead Java
supports multiple interface inheritance, which allows an object to inherit many method signatures from
different interfaces with the condition that the inheriting object must implement those inherited methods. The
multiple interface inheritance also allows an object to behave polymorphically on those methods.

• 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. This
means you do not know when the objects are going to be finalized. Avoid using finalize() method to
release non-memory resources like file handles, sockets, database connections etc because Java has only
a finite number of these resources and you do not know when the garbage collection is going to kick in to
release these resources through the finalize() method.

• Java does not include structures or unions because the traditional data structures are implemented as an
object oriented framework
Java
12
All the code in Java program is encapsulated within classes therefore Java does not have global variables or
functions.
C++ requires explicit memory management, while Java includes automatic garbage collection

Q 04: Explain Java class loaders? Explain dynamic class loading? LF
A 04: Class loaders are hierarchical. Classes are introduced into the JVM as they are referenced by name in a class that
is already running in the JVM. So how is the very first class loaded? The very first class is specially loaded with
the help of static main() method declared in your class. All the subsequently loaded classes are loaded by the
classes, which are already loaded and running. A class loader creates a namespace. All JVMs include at least one
class loader that is embedded within the JVM called the primordial (or bootstrap) class loader. Now let’s look at
non-primordial class loaders. The JVM has hooks in it to allow user defined class loaders to be used in place of
primordial class loader. Let us look at the class loaders created by the JVM.
CLASS LOADER reloadable? Explanation
Bootstrap
(primordial)
No Loads JDK internal classes, java.* packages. (as defined in the sun.boot.class.path
system property, typically loads rt.jar and i18n.jar)
Extensions No Loads jar files from JDK extensions directory (as defined in the java.ext.dirs system
property – usually lib/ext directory of the JRE)
System No Loads classes from system classpath (as defined by the java.class.path property, which
is set by the CLASSPATH environment variable or –classpath or –cp command line
options)

Explain static vs dynamic class loading?
Static class loading Dynamic class loading
Classes are statically loaded with Java’s
“new” operator.
class MyClass {
public static void main(String args[]) {
Car c = new Car();
}
}
Dynamic loading is a technique for programmatically invoking the functions of a
class loader at run time. Let us look at how to load classes dynamically.
Class.forName (String className); //static method which returns a Class
The above static method returns the class object associated with the class
name. The string className can be supplied dynamically at run time. Unlike the
static loading, the dynamic loading will decide whether to load the class Car or
the class Jeep at runtime based on a properties file and/or other runtime
conditions. Once the class is dynamically loaded the following method returns an
instance of the loaded class. It’s just like creating a class object with no
arguments.
class.newInstance (); //A non-static method, which creates an instance of a
class (ie creates an object).
Jeep myJeep = null ;
//myClassName should be read from a properties file or Constants interface.
//stay away from hard coding values in your program.
String myClassName = "au.com.Jeep" ;
Class vehicleClass = Class.forName(myClassName) ;
myJeep = (Jeep) vehicleClass.newInstance();
myJeep.setFuelCapacity(50);
A NoClassDefFoundException is
thrown if a class is referenced with
Java’s “new” operator (i.e. static loading)
but the runtime system cannot find the
referenced class.
A ClassNotFoundException is thrown when an application tries to load in a
class through its string name using the following methods but no definition for the
class with the specified name could be found:
• The forName(..) method in class - Class.
• The findSystemClass(..) method in class - ClassLoader.
• The loadClass(..) method in class - ClassLoader.

Q 05: What are the advantages of Object Oriented Programming Languages (OOPL)?
A 05: The Object Oriented Programming Languages directly represent the real life objects like Car, Jeep, Account,
Customer etc. The features of the OO programming languages like polymorphism, inheritance and
encapsulation make it powerful

Q 06: How does the Object Oriented approach improve software development?
A 06: The key benefits are:
Re-use of previous work: using implementation inheritance and object composition.
Real mapping to the problem domain: Objects map to real world and represent vehicles, customers,
products etc: with encapsulation.
Modular Architecture: Objects, systems, frameworks etc are the building blocks of larger systems.

The increased quality and reduced development time are the by-products of the key benefits discussed above.
If 90% of the new application consists of proven existing components then only the remaining 10% of the code
have to be tested from scratch.

Q 07: How do you express an ‘is a’ relationship and a ‘has a’ relationship or explain inheritance and composition? What
is the difference between composition and aggregation?
A 07: The ‘is a’ relationship is expressed with inheritance and ‘has a’ relationship is expressed with composition. Both
inheritance and composition allow you to place sub-objects inside your new class. Two of the main techniques for
code reuse are class inheritance and object composition.

No comments:

Post a Comment