Explain DI or IOC pattern.


A:  Dependency injection (DI) is a programming design pattern and architectural model, sometimes also referred to as inversion of control or IOC, although technically speaking, dependency injection specifically refers to an implementation of a particular form of IOC. Dependancy Injection describes the situation where one object uses a second object to provide a particular capacity. For example, being passed a database connection as an argument to the constructor instead of creating one internally. The term "Dependency injection" is a misnomer, since it is not a dependency that is injected, rather it is a provider of some capability or resource that is injected. There are three common forms of dependency injection: setter-, constructor- and interface-based injection. Dependency injection is a way to achieve loose coupling. Inversion of control (IOC) relates to the way in which an object obtains references to its dependencies. This is often done by a lookup method. The advantage of inversion of control is that it decouples objects from specific lookup mechanisms and implementations of the objects it depends on. As a result, more flexibility is obtained for production applications as well as for testing.

Q. What are the different IOC containers available?
A. Spring is an IOC container. Other IOC containers are HiveMind, Avalon, PicoContainer.

Q. What are the different types of dependency injection. Explain with examples.
A: There are two types of dependency injection: setter injection and constructor injection.
Setter Injection:  Normally in all the java beans, we will use setter and getter method to set and get the value of property as follows:  
    public class namebean {
     String      name;  
     public void setName(String a) {
        name = a; }
     public String getName() {
        return name; }
    }
 
We will create an instance of the bean 'namebean' (say bean1) and set property as bean1.setName("tom"); Here in setter injection, we will set the property 'name'  in spring configuration file as showm below:
<bean id="bean1"   class="namebean">
   <property   name="name" >
       <value>tom</value>
   </property>
</bean>
The subelement <value> sets the 'name' property by calling the set method as setName("tom"); This process is called setter injection.
To set properties that reference other beans <ref>, subelement of <property> is used as shown below,
 <bean id="bean1"   class="bean1impl">
   <property name="game">
       <ref bean="bean2"/>
   </property>
</bean>
<bean id="bean2"   class="bean2impl" /> 
Constructor injection:  For constructor injection, we use constructor with parameters as shown below,
 
 public class namebean {
     String name;
     public namebean(String a) {
        name = a;
     }   
}
 
We will set the property 'name' while creating an instance of the bean 'namebean' as namebean bean1 = new namebean("tom");
 
Here we use the <constructor-arg> element to set the the property by constructor injection as
 <bean id="bean1"  class="namebean">
    <constructor-arg>
       <value>My Bean Value</value>
   </constructor-arg>
</bean>

Q. What is spring? What are the various parts of spring framework? What are the different persistence frameworks which could be used with spring?
A. Spring is an open source framework created to address the complexity of enterprise application development. One of the chief advantages of the Spring framework is its layered architecture, which allows you to be selective about which of its components you use while also providing a cohesive framework for J2EE application development. The Spring modules are built on top of the core container, which defines how beans are created, configured, and managed, as shown in the following figure. Each of the modules (or components) that comprise the Spring framework can stand on its own or be implemented jointly with one or more of the others. The functionality of each component is as follows:

The core container: The core container provides the essential functionality of the Spring framework. A primary component of the core container is the BeanFactory, an implementation of the Factory pattern. The BeanFactory applies the Inversion of Control (IOC) pattern to separate an application’s configuration and dependency specification from the actual application code.

Spring context: The Spring context is a configuration file that provides context information to the Spring framework. The Spring context includes enterprise services such as JNDI, EJB, e-mail, internalization, validation, and scheduling functionality.

Spring AOP: The Spring AOP module integrates aspect-oriented programming functionality directly into the Spring framework, through its configuration management feature. As a result you can easily AOP-enable any object managed by the Spring framework. The Spring AOP module provides transaction management services for objects in any Spring-based application. With Spring AOP you can incorporate declarative transaction management into your applications without relying on EJB components.

Spring DAO: The Spring JDBC DAO abstraction layer offers a meaningful exception hierarchy for managing the exception handling and error messages thrown by different database vendors. The exception hierarchy simplifies error handling and greatly reduces the amount of exception code you need to write, such as opening and closing connections. Spring DAO’s JDBC-oriented exceptions comply to its generic DAO exception hierarchy.

Spring ORM: The Spring framework plugs into several ORM frameworks to provide its Object Relational tool, including JDO, Hibernate, and iBatis SQL Maps. All of these comply to Spring’s generic transaction and DAO exception hierarchies.

Spring Web module: The Web context module builds on top of the application context module, providing contexts for Web-based applications. As a result, the Spring framework supports integration with Jakarta Struts. The Web module also eases the tasks of handling multi-part requests and binding request parameters to domain objects.

Spring MVC framework: The Model-View-Controller (MVC) framework is a full-featured MVC implementation for building Web applications. The MVC framework is highly configurable via strategy interfaces and accommodates numerous view technologies including JSP, Velocity, Tiles, iText, and POI.

Q. What is AOP? How does it relate with IOC? What are different tools to utilize AOP?
A:  Aspect-oriented programming, or AOP, is a programming technique that allows programmers to modularize crosscutting concerns, or behavior that cuts across the typical divisions of responsibility, such as logging and transaction management. The core construct of AOP is the aspect, which encapsulates behaviors affecting multiple classes into reusable modules. AOP and IOC are complementary technologies in that both apply a modular approach to complex problems in enterprise application development. In a typical object-oriented development approach you might implement logging functionality by putting logger statements in all your methods and Java classes. In an AOP approach you would instead modularize the logging services and apply them declaratively to the components that required logging. The advantage, of course, is that the Java class doesn't need to know about the existence of the logging service or concern itself with any related code. As a result, application code written using Spring AOP is loosely coupled. The best tool to utilize AOP to its capability is AspectJ. However AspectJ works at he byte code level and you need to use AspectJ compiler to get the aop features built into your compiled code. Nevertheless AOP functionality is fully integrated into the Spring context for transaction management, logging, and various other features.  In general any AOP framework control aspects in three possible ways:
 
Joinpoints: Points in a program's execution. For example, joinpoints could define calls to specific methods in a class
Pointcuts: Program constructs to designate joinpoints and collect specific context at those points
Advices: Code that runs upon meeting certain conditions. For example, an advice could log a message before executing a joinpoint
Q. What are the advantages of spring framework?
A.  
  1. Spring has layed architecture. Use what you need and leave you don't need now.
  2. Spring Enables POJO Programming. There is no behind the scene magic here. POJO programming enables continous integration and testability.
  3. Dependency Injection and Inversion of Control Simplifies JDBC (Read the first question.)
  4. Open source and no vendor lock-in.
Q. Can you name a tool which could provide the initial ant files and directory structure for a new spring project.
A: Appfuse or equinox.

Q. Explain BeanFactory in spring.
A: Bean factory is an implementation of the factory design pattern and its function is to create and dispense beans. As the bean factory knows about many objects within an application, it is able to create association between collaborating objects as they are instantiated. This removes the burden of configuration from the bean and the client. There are several implementation of BeanFactory. The most useful one is "org.springframework.beans.factory.xml.XmlBeanFactory" It loads its beans based on the definition contained in an XML file. To create an XmlBeanFactory, pass a InputStream to the constructor. The resource will provide the XML to the factory. BeanFactory  factory = new XmlBeanFactory(new FileInputStream("myBean.xml"));
This line tells the bean factory to read the bean definition from the XML file. The bean definition includes the description of beans and their properties. But the bean factory doesn't instantiate the bean yet. To retrieve a bean from a 'BeanFactory', the getBean() method is called. When getBean() method is called, factory will instantiate the bean and begin setting the bean's properties using dependency injection. myBean bean1 = (myBean)factory.getBean("myBean");

Q. Explain the role of ApplicationContext in spring.
A. While Bean Factory is used for simple applications, the Application Context is spring's more advanced container. Like 'BeanFactory' it can be used to load bean definitions, wire beans together and dispense beans upon request. It also provide
 
1) a means for resolving text messages, including support for internationalization.
2) a generic way to load file resources.
3) events to beans that are registered as listeners.
 Because of additional functionality, 'Application Context' is preferred over a BeanFactory. Only when the resource is scarce like mobile devices, 'BeanFactory' is used. The three commonly used implementation of 'Application Context' are
 
1. ClassPathXmlApplicationContext : It Loads  context definition from an XML file located in the classpath, treating context definitions as classpath resources. The application context is loaded from the application's classpath by using the code
ApplicationContext    context = new ClassPathXmlApplicationContext("bean.xml");
2. FileSystemXmlApplicationContext : It loads context definition from an XML file in the filesystem. The application context is loaded from the file system by using the code
ApplicationContext    context = new FileSystemXmlApplicationContext("bean.xml");
3. XmlWebApplicationContext : It loads context definition from an XML file contained within a web application.

Hibernate 3.0

Hibernate is a powerful, high performance object/relational persistence and query service.It is an open-source technology which fits well both with Java and .NET technologies.Hibernate lets developers write persistence classes with hibernate query features of HQL within principles of Object Oriented paradigm.It means one can include association,inheritance,polymorphism,composition and collection of these persisting objects to build applications.
Hibernate Architecture
The main objective of Hibernate is to relieve the developers from manual handling of SQLs,JDBC APIs for resultsets handling and it helps in keeping your data portable to various SQL databases,just by switching the delegate and driver details in hibernate.cfg.xml file.
Hibernate offers sophisticated query options, you can write plain SQL, object-oriented HQL (Hibernate Query Language), or create programmatic criteria and example queries. Hibernate can optimize object loading all the time, with various fetching and caching options.
Some snapshots of Hibernate:
-Free open source
-OO Concepts can be implemented.
-A rich variety of mappings for collections and dependent objects
-No extra code generation or bytecode processing steps in your build procedure
-Great performance, has a dual-layer cache architecture, and may be used in a cluster
-Its own query language support
-Efficient transaction handling
-The Java Persistence API is the standard object/relational mapping and persistence management interface of the Java EE 5.0 platform which are implemented with the Hibernate Annotations and Hibernate EntityManager modules, on top of the Hibernate Core.(As part of EJB3.0 spec)

Hibernate interview question:
What is Hibernate?


Why Hibernate?


What is ORM?


What are core interfaces of Hibernate Framework?


What is dirty checking in Hibernate?


What are different fetch strategies Hibernate have?


Can you compare JDBC/DAO with Hibernate?


Explain different inheritance mapping models in Hibernate

Explain RMI Architecture?

1)Explain RMI Architecture?

RMI uses a layered architecture, each of the layers could be enhanced or replaced without affecting the rest of the system. The details of layers can be summarised as follows:
  • Application Layer: The client and server program
  • Stub & Skeleton Layer: Intercepts method calls made by the client/redirects these calls to a remote RMI service.
  • Remote Reference Layer: Understands how to interpret and manage references made from clients to the remote service objects.
  • Transport layer: Based on TCP/IP connections between machines in a network. It provides basic connectivity, as well as some firewall penetration strategies.

2)What is the difference between RMI & Corba?

The most significant difference between RMI and CORBA is that CORBA was made specifically for interoperability across programming languages. That is CORBA fosters the notion that programs can be built to interact in multiple languages. The server could be written in C++, the business logic in Python, and the front-end written in COBOL in theory. RMI, on the other hand is a total Java solution, the interfaces, the implementations and the clients--all are written in Java.
RMI allows dynamic loading of classes at runtime. In a multi-language CORBA environment, dynamic class loading is not possible. The important advantage to dynamic class loading is that it allows arguments to be passed in remote invocations that are subtypes of the declared types. In CORBA, all types have to be known in advance. RMI (as well as RMI/IIOP) provides support for polymorphic parameter passing, whereas strict CORBA does not. CORBA does have support for multiple languages which is good for some applications, but RMI has the advantage of being dynamic, which is good for other applications.

3)What are the services in RMI ?

An RMI "service" could well be any Java method that can be invoked remotely. The other service is the JRMP RMI naming service which is a lookup service.

4)Does RMI-IIOP support code downloading for Java objects sent by value across an IIOP connection in the same way as RMI does across a JRMP connection?

Yes. The JDK 1.2 support the dynamic class loading.

5)How many types of protocol implementations does RMI have?

RMI has at least three protocol implementations: Java Remote Method Protocol(JRMP), Internet Inter ORB Protocol(IIOP), and Jini Extensible Remote Invocation(JERI). These are alternatives, not part of the same thing, All three are indeed layer 6 protocols for those who are still speaking OSI reference model.

6)Does RMI-IIOP support dynamic downloading of classes?

No, RMI-IIOP doesn't support dynamic downloading of the classes as it is done with CORBA in DII (Dynamic Interface Invocation).Actually RMI-IIOP combines the usability of Java Remote Method Invocation (RMI) with the interoperability of the Internet Inter-ORB Protocol (IIOP).So in order to attain this interoperability between RMI and CORBA,some of the features that are supported by RMI but not CORBA and vice versa are eliminated from the RMI-IIOP specification.

7)Does RMI-IIOP support code downloading for Java objects sent by value across an IIOP connection in the same way as RMI does across a JRMP connection?

Yes. The JDK 1.2 support the dynamic class loading.

8)Can RMI and Corba based applications interact ?

Yes they can. RMI is available with IIOP as the transport protocol instead of JRMP.

50 JavaScript & AJAX interview questions



1.  Why so JavaScript and Java have similar name?
A.  JavaScript is a stripped-down version of Java
B.  JavaScript's syntax is loosely based on Java's
C.  They both originated on the island of Java
D.  None of the above

2.  When a user views a page containing a JavaScript program, which machine actually executes the script?
A.  The User's machine running a Web browser
B.   The Web server
C.  A central machine deep within Netscape's corporate offices
D.  None of the above

3.  ______ JavaScript is also called client-side JavaScript.
A.  Microsoft
B.  Navigator
C.  LiveWire
D.  Native

4.  __________ JavaScript is also called server-side JavaScript.
A.  Microsoft
B.   Navigator
C.  LiveWire
D.  Native

5.  What are variables used for in JavaScript Programs?
A.  Storing numbers, dates, or other values
B.   Varying randomly
C.  Causing high-school algebra flashbacks
D.  None of the above

6.  _____ JavaScript statements embedded in an HTML page can respond to user events such as mouse-clicks, form input, and page navigation.
A.  Client-side
B.   Server-side
C.  Local
D.  Native

7.  What should appear at the very end of your JavaScript?
The <script LANGUAGE="JavaScript">tag
A.   The </script>
B.    The <script>
C.  The END statement
D.  None of the above

8.  Which of the following can't be done with client-side JavaScript?
A.  Validating a form
B.   Sending a form's contents by email
C.  Storing the form's contents to a database file on the server
D.  None of the above

9.  Which of the following are capabilities of functions in JavaScript?
A.  Return a value
B.   Accept parameters and Return a value
C.  Accept parameters
D.  None of the above

10.  Which of the following is not a valid JavaScript variable name?
A.  2names
B.   _first_and_last_names
C.  FirstAndLast
D.  None of the above

11.  ______ tag is an extension to HTML that can enclose any number of JavaScript statements.
A.  <SCRIPT>
B.   <BODY>
C.  <HEAD>
D.  <TITLE>

12.  How does JavaScript store dates in a date object?
A.  The number of milliseconds since January 1st, 1970
B.   The number of days since January 1st, 1900
C.  The number of seconds since Netscape's public stock offering.
D.  None of the above

13.  Which of the following attribute can hold the JavaScript version?
A.  LANGUAGE
B.   SCRIPT
C.  VERSION
D.  None of the above

14.  What is the correct JavaScript syntax to write "Hello World"?
A.  System.out.println("Hello World")
B.   println ("Hello World")
C.  document.write("Hello World")
D.  response.write("Hello World")

15.  Which of the following way can be used to indicate the LANGUAGE attribute?
A.  <LANGUAGE="JavaScriptVersion">
B.   <SCRIPT LANGUAGE="JavaScriptVersion">
C.  <SCRIPT LANGUAGE="JavaScriptVersion">    JavaScript statements…</SCRIPT>
D.  <SCRIPT LANGUAGE="JavaScriptVersion"!>    JavaScript statements…</SCRIPT>

16.  Inside which HTML element do we put the JavaScript?
A.  <js>
B.   <scripting>
C.  <script>
D.  <javascript>

17.  What is the correct syntax for referring to an external script called " abc.js"?
A.  <script href=" abc.js">
B.   <script name=" abc.js">
C.  <script src=" abc.js">
D.  None of the above

18.  Which types of image maps can be used with JavaScript?
A.  Server-side image maps
B.  Client-side image maps
C.  Server-side image maps and Client-side image maps
D.  None of the above

19.  Which of the following navigator object properties is the same in both   Netscape and IE?
A.  navigator.appCodeName
B.   navigator.appName
C.  navigator.appVersion
D.  None of the above

20.  Which is the correct way to write a JavaScript array?
A.  var txt = new Array(1:"tim",2:"kim",3:"jim")
B.   var txt = new Array:1=("tim")2=("kim")3=("jim")
C.  var txt = new Array("tim","kim","jim")
D.  var txt = new Array="tim","kim","jim"

21.  What does the <noscript> tag do?
A.  Enclose text to be displayed by non-JavaScript browsers.
B.   Prevents scripts on the page from executing.
C.  Describes certain low-budget movies.
D.  None of the above

22. If para1 is the DOM object for a paragraph, what is the correct syntax to change the text within the paragraph?
A.  "New Text"?
B.  para1.value="New Text";
C.  para1.firstChild.nodeValue= "New Text";
D.  para1.nodeValue="New Text";

23.  JavaScript entities start with _______ and end with _________.
A.  Semicolon, colon
B.   Semicolon, Ampersand
C.  Ampersand, colon
D.  Ampersand, semicolon

24.  Which of the following best describes JavaScript?
A.  a low-level programming language.
B.   a scripting language precompiled in the browser.
C.  a compiled scripting language.
D.  an object-oriented scripting language.

25.  Choose the server-side JavaScript object?
A.  FileUpLoad
B.   Function
C.  File
D.  Date

26.  Choose the client-side JavaScript object?
A.  Database
B.   Cursor
C.  Client
D.  FileUpLoad

27.  Which of the following is not considered a JavaScript operator?
A.  new
B.  this
C.  delete
D.  typeof

28.  ______method evaluates a string of JavaScript code in the context of the specified object.
A.  Eval
B.   ParseInt
C.  ParseFloat
D.  Efloat

29.  Which of the following event fires when the form element loses the focus: <button>, <input>, <label>, <select>, <textarea>?
A.  onfocus
B.  onblur
C.  onclick
D.  ondblclick

30.  The syntax of Eval is ________________
A.  [objectName.]eval(numeric)
B.  [objectName.]eval(string)
C.  [EvalName.]eval(string)
D.  [EvalName.]eval(numeric)

31.  JavaScript is interpreted by _________
A.  Client
B.   Server
C.  Object
D.  None of the above

32.  Using _______ statement is how you test for a specific condition.
A.  Select
B.  If
C.  Switch
D.  For

33.  Which of the following is the structure of an if statement?
A.  if (conditional expression is true) thenexecute this codeend if
B.   if (conditional expression is true)execute this codeend if
C.  if (conditional expression is true)   {then execute this code>->}
D.  if (conditional expression is true) then {execute this code}

34.  How to create a Date object in JavaScript?
A.  dateObjectName = new Date([parameters])
B.   dateObjectName.new Date([parameters])
C.  dateObjectName := new Date([parameters])
D.  dateObjectName Date([parameters])

35.  The _______ method of an Array object adds and/or removes elements from an array.
A.  Reverse
B.   Shift
C.  Slice
D.  Splice

36.  To set up the window to capture all Click events, we use which of the following statement?
A.  window.captureEvents(Event.CLICK);
B.   window.handleEvents (Event.CLICK);
C.  window.routeEvents(Event.CLICK );
D.  window.raiseEvents(Event.CLICK );

37.  Which tag(s) can handle mouse events in Netscape?
A.  <IMG>
B.  <A>
C.  <BR>
D.  None of the above

38.  ____________ is the tainted property of a window object.
A.  Pathname
B.   Protocol
C.  Defaultstatus
D.  Host

39.  To enable data tainting, the end user sets the _________ environment variable.
A.  ENABLE_TAINT
B.   MS_ENABLE_TAINT
C.  NS_ENABLE_TAINT
D.  ENABLE_TAINT_NS

40.  In JavaScript, _________ is an object of the target language data type that encloses an object of the source language.
A.  a wrapper
B.   a link
C.  a cursor
D.  a form



41.  When a JavaScript object is sent to Java, the runtime engine creates a Java wrapper of type ___________
A.  ScriptObject
B.  JSObject
C.  JavaObject
D.  Jobject

42.  _______ class provides an interface for invoking JavaScript methods and examining JavaScript properties.
A.  ScriptObject
B.  JSObject
C.  JavaObject
D.  Jobject

43.  _________ is a wrapped Java array, accessed from within JavaScript code.
A.  JavaArray
B.   JavaClass
C.  JavaObject
D.  JavaPackage
               
44. A ________ object is a reference to one of the classes in a Java package, such as netscape.javascript .
A.  JavaArray
B.  JavaClass
C.  JavaObject
D.  JavaPackage

45.  The JavaScript exception is available to the Java code as an instance of __________
A.  netscape.javascript.JSObject
B.  netscape.javascript.JSException
C.  netscape.plugin.JSException
D.  None of the above

46. To automatically open the console when a JavaScript error occurs which of the following is added to prefs.js?
A.  user_pref(" javascript.console.open_on_error", false);
B.   user_pref("javascript.console.open_error ", true);
C.  user_pref("javascript.console.open_error ", false);
D.   user_pref("javascript.console.open_on_error", true);

47.  To open a dialog box each time an error occurs, which of the following is added to prefs.js?
A.  user_pref("javascript.classic.error_alerts", true);
B.   user_pref("javascript.classic.error_alerts ", false);
C.  user_pref("javascript.console.open_on_error ", true);
D.  user_pref("javascript.console.open_on_error ", false);

48.  The syntax of a blur method in a button object is ______________
A.  Blur()
B.   Blur(contrast)
C.  Blur(value)
D.  Blur(depth)

49.  The syntax of capture events method for document object is ______________
A.  captureEvents()
B.   captureEvents(args eventType)
C.  captureEvents(eventType)
D.  captureEvents(eventVal)

50.  The syntax of close method for document object is ______________
A.  Close(doc)
B.   Close(object)
C.  Close(val)
D.  Close()