7283499 Ejb Questions

download 7283499 Ejb Questions

of 86

Transcript of 7283499 Ejb Questions

  • 8/3/2019 7283499 Ejb Questions

    1/86

    EJB QUESTIONS

    QUESTION NO : 1

    QUESTION STATEMENT : You have written a web application using EJBs on an application server. Now the

    requirement to migrate

    the EJBs to another application server vendor arises. Which of the following types of Enterprise Java Bean will be most

    likely to be difficult to port from one App Server to another?

    CHOICES :

    a) Stateful Session Bean

    b) Stateless Session Bean

    c) BMP (Bean Managed Persistence) Entity Bean

    d) CMP (Container Managed Persistence) Entity Bean

    CORRECT ANSWER : d

    EXPLANATION :

    Choice D is Correct. EJBs are portable if you write them to be. Session beans and BMP (bean managed

    persistence) entity beans usually port quite easily. In contrast, CMP (container managed persistence) entity beans need a

    good bit of work.

    Quite often, applications that rely on a clustering implementation take longer to port, as clustering is

    a big differentiator between vendors and is also not addressed adequately in the EJB specification. The work might not

    be in

    rewriting code, but in reconfiguring deployment descriptors and container/server configurations. In addition,administration

    and configuration tools and mechanisms are vendor-specific, as are things like startup and shutdown scripts andsometimes

    build scripts.

    --------------------------------------------------------------------

    QUESTION NO : 2QUESTION STATEMENT : Which interface should be implemented by a stateful session bean if it needs to

    synchronize its

    conversational state with the transactional context ?

    CHOICES :

    a) javax.transaction.UserTransactionb) javax.ejb.SessionSynchronization

    1

  • 8/3/2019 7283499 Ejb Questions

    2/86

    c) javax.ejb.EJBContext

    d) javax.transaction.TransactionContext

    e) javax.ejb.SessionTransaction

    CORRECT ANSWER : b

    EXPLANATION :

    Choice B is Correct. If a bean needs to synchronize its conversational state with the transactional context,

    the bean class must implement the javax.ejb.SessionSynchronization interface. This interface contains methods to notify

    the

    session bean when a transaction begins, when it is about to complete, and when it has completed.The enterprise bean

    developer

    can use these methods to synchronize the state of the session enterprise bean instance with ongoing transactions.

    UserTransaction provides an interface to the transaction manager that allows the application developer to manage the

    scope of

    a transaction explicitly. So A is not the correct answer. EJBContext interface is the bean classs interface to the container

    system. It provides information about the security identity and transaction status. So C is also not the right answer. D

    and

    E are interfaces which do not exist.

    --------------------------------------------------------------------

    QUESTION NO : 3QUESTION STATEMENT : For every EJB, you write a Home Interface, Remote Interface and the Bean class. EJB

    doesn't require that

    the bean class implement these interfaces. Its encouraged that the bean class implement these interfaces. True/False?

    CHOICES :

    a) True

    b) False

    CORRECT ANSWER : b

    EXPLANATION :

    False, its not advisable for Bean class to implement the Remote and Home interface. The Home interface extends

    EJBHome and the Remote interface extends EJBObject. These base interfaces (EJBObject and EJBHome) define a lot

    of other

    methods that are maintained by container automatically. In case the Bean class is implementing the Remote and Home

    2

  • 8/3/2019 7283499 Ejb Questions

    3/86

    interfaces, it becomes necessary for it to implement all those methods which are being taken care by the Container. Thus

    its

    not advisable and discouraged for Bean class to implement Remote and Home interface.

    --------------------------------------------------------------------

    QUESTION NO : 4

    QUESTION STATEMENT : You have an enterprise application that needs to display a large list of categories in order

    to let a

    user select from that list. The average size of the list is 100. If you decide to use Enterprise Java Beans to retrieve the

    data, which of the following EJB types would be the best choice in terms of performance and utilization of resources ?

    CHOICES :

    a) Stateless Session Beanb) Stateful Session Bean

    c) BMP Entity Beand) CMP Entity Bean

    CORRECT ANSWER : b

    EXPLANATION :

    Choice B is Correct. When you are retrieving data to display in a list, you usually need only a small subset

    of data. If you use a custom finder method to retrieve a large set of entity beans and utilize only a small set of data

    elements, it would be a huge wastage of resources.So instead of retrieving and iterating over a collection of entitybeans,

    create a Stateful Session Bean that can retrieve only those pieces of data that are necessary through a simple SQL query.

    Also, all entity beans are automatically distributed, transactional and persistent. There is a considerable amount of

    overhead associated with each of these services. So in a situation like the above where a huge amount of data just needs

    to

    be read and not modified, if we use entity beans, it would cause unnecessary overhead. Since the selection made by

    each user

    needs to be stored for further operations, stateful session beans are preferred over stateless beans.

    --------------------------------------------------------------------

    QUESTION NO : 5

    QUESTION STATEMENT : Which of the following are the differences between a Stateless Session bean and a Stateful

    Session Bean?

    3

  • 8/3/2019 7283499 Ejb Questions

    4/86

    CHOICES :

    a) Stateless beans define instance pooling in their lifecycle and stateful beans do not.

    b) Stateful beans can have instance variables while stateless beans cannot.

    c) A stateless session bean only has one ejbCreate() method which takes no arguments while a stateful bean can haveany

    number of overloaded ejbCreate() methods.

    d) Calling the create method of EJBHome does not result in a call to the bean's ejbCreate() method in the case of a

    stateless bean while it does in the case of a stateful bean.

    e) The EJBObject of a stateless bean can be shared by multiple clients concurrently, but in the case of a stateful bean

    each

    client has a dedicated EJBObject.

    CORRECT ANSWER : acd

    EXPLANATION :

    Choice A, C and D are Correct. B and E are false. Stateless session beans can have instance variables similar

    to stateful beans ,but the state of these variables are not preserved between method invocations as it is in the case of

    stateful beans.With session beans of both types, the EJB object is dedicated to one client. A stateless session bean may

    be

    used by multiple clients one after the other, but the same EJB object is not accessed concurrently by more than 1 client.

    Stateless beans have only 2 states in their lifecycle, the Does Not Exist State and Method Ready Pool State. Stateful

    beans

    are not pooled since each one is dedicated for a particular client only and hence cannot be reused. In stateless session

    beans, calling EJBHome's create() method results in the creation of an EJBObject for the bean, but ejbCreate() of the

    bean is

    not invoked. Since the state is not maintained having arguments for the create() method is illegal. The no-argument

    version

    of ejbCreate() is invoked by the container after the bean is instantiated.

    --------------------------------------------------------------------

    QUESTION NO : 6

    QUESTION STATEMENT : Which of the following exceptions is thrown by the ejbLoad() method of an entity bean

    when the database

    row to be loaded is not found ?

    CHOICES :

    4

  • 8/3/2019 7283499 Ejb Questions

    5/86

    a) NoSuchEntityException

    b) EJBException

    c) RemoteException

    d) ObjectNotFoundExceptione) FinderException

    CORRECT ANSWER : a

    EXPLANATION :

    Choice A is Correct. NoSuchEntityException is a system exception thrown by the ejbLoad() method of an entity

    bean when the database row to be loaded is not found and also by the ejbStore() method when the database row to be

    updated

    cannot be found. This is a system exception which causes the container to rollback the transaction automatically.

    EJBException is also a SystemException, but is called by the EJB methods only if any other system problem has

    occurred.

    RemoteException is never thrown directly by the bean, the container throws this exception if the bean throws an

    EJBException.

    ObjectNotfoundException is thrown by a finder method if a requested database row could not be found.

    FinderException is the

    super class of ObjectNotFoundException.

    --------------------------------------------------------------------

    QUESTION NO : 7

    QUESTION STATEMENT : In which of the following cases do we need to use the narrow method of

    PortableRemoteObject to get the

    correct object type from the object reference returned by the method ?

    CHOICES :

    a) When an EJB home reference is obtained using the javax.naming.Context.lookup() method

    b) When an EJB object reference is obtained using the javax.ejb.Handle.getEJBObject() method

    c) When the EJB object reference is obtained using the create() method of the home object

    d) When the EJB object reference is obtained using the findByPrimaryKey() method of the home object.

    CORRECT ANSWER : ab

    EXPLANATION :

    Choice A and B are Correct. The PortableRemoteObject.narrow() method is used to explicitly narrow the remote

    reference returned by a method to a more specific type as is needed in a typical object oriented environment. The

    PortableRemoteObject abstracts the narrowing process so that any underlying protocol (eg: IIOP) can be used for

    5

  • 8/3/2019 7283499 Ejb Questions

    6/86

    narrowing.

    The narrow method takes 2 arguments : the remote reference that is to be narrowed and the type it should be

    narrowed to.The narrow method only needs to be used when a remote reference to an EJB object is returned without a

    specific

    Remote interface type. In the cases A and B, the methods return a remote reference of type Object. In case A, the

    returned

    reference needs to be narrowed to the appropriate home interface type. In case B, the returned reference needs to be

    narrowed

    to the appropriate bean interface type. The create() and findByPrimaryKey methods defined in the home interface do

    not

    require the use of narrow() because these methods already return the correct EJB object type. So C and D are not the

    right

    answers.

    --------------------------------------------------------------------

    QUESTION NO : 8

    QUESTION STATEMENT : Which of the following steps in the lifecycle of a Stateful Session bean occur for a

    Stateless Session

    Bean after the client has invoked the create() method on the EJB home?

    CHOICES :

    a) Container invokes Class.newInstance() method on the bean class

    b) The bean instance is assigned to its EJBObject

    c) Container invokes setSessionContext() method on the bean instanced) Container invokes ejbCreate() method on the bean instance

    e) Container invokes ejbRemove() method on the bean at the end of its lifecycle

    CORRECT ANSWER : be

    EXPLANATION :

    Choice B and E are Correct. The biggest difference between a Stateful session bean and the other bean types

    is that the stateful beans do not use instance pooling since they are dedicated to one client for their entire life. So when

    a client invokes the create() method on an EJB home of a stateful bean, the container creates a new instance of the bean

    by

    calling Class.newInstance(). The bean instance is now assigned to it's EJBObject. The container now invokes

    6

  • 8/3/2019 7283499 Ejb Questions

    7/86

    setSessionContext() on the bean, handing it it's reference to the SessionContext. Finally the container invokes

    ejbCreate()

    which matches with the create() method invoked by the client.

    In the case of the Stateless session bean, the container

    creates a number of stateless instances and keeps them in the method-ready pooled state.So the Class.newInstance(),

    setSessionContext() and ejbCreate() are invoked by the container when the bean moves into the pooled state ready for

    serving

    any method invocations. These methods are not re-invoked whenever a client requests a remote reference to the bean.So A, C

    and D are not correct. So in stateless beans, calling the EJBHome's create() method results only in the assigning of the

    EJBObject to the bean and returning the remote reference of the EJBObject to the client. So B is correct. In the case of

    both

    types of beans, the container invokes ejbRemove() at the end of the lifecycle. So E is also correct.

    --------------------------------------------------------------------

    QUESTION NO : 9

    QUESTION STATEMENT : Which of the following needs to be done to end the client session of a Stateful SessionBean ?

    CHOICES :

    a) Call the remove() method of the home object passing the primary key of the bean as the argument.

    b) Call the bean's remove() method passing the Session Context of the bean as the argument.

    c) Call the remove() method of the home object passing the Handle of the bean as the argument.

    d) Call the remove() method of the home object passing the primary key of the bean as the argument.

    CORRECT ANSWER : c

    EXPLANATION :

    Choice C is Correct. The EJBHome.remove() method is responsible for deleting a bean.The argument is either the

    javax.ejb.Handle in the case of stateful session beans and primary key in the case of entity beans. The Handle isessentially

    a serializable pointer to a specific bean. For session beans, the EJBHome.remove() on the bean ends the session's

    service to

    the client. When EJBHome.remove() is invoked, the remote reference to the session beans becomes invalid and any

    conversational state maintained by the bean is lost. If for some reason the bean can't be removed, RemoveException is

    7

  • 8/3/2019 7283499 Ejb Questions

    8/86

    thrown

    --------------------------------------------------------------------

    QUESTION NO : 10

    QUESTION STATEMENT : If no matching beans are found, the FindByPrimaryKey method defined in the home

    object of an entity bean

    will

    CHOICES :

    a) return a null reference

    b) throw javax.ejb.FindException

    c) throw javax.ejb.ObjectNotFoundException

    d) throw RemoteException

    CORRECT ANSWER : c

    EXPLANATION :

    Choice C is Correct. findByPrimaryKey is a standard method that all home interfaces for entity beans must

    support. With Container Managed Persistence, implementations of find methods are generated automatically at

    deployment

    time.Find methods that return a single remote reference throw a FinderException if an application error occurs and an

    ObjectNotFoundException if a matching bean cannot be found.

    The ObjectNotFoundException is a subtype of FinderException and

    is only thrown by find methods which return single remote references. Find methods that return an Enumeration or

    Collection

    type return an empty collection of no matching beans can be found or throw a FinderException if an application error

    occurs.

    --------------------------------------------------------------------

    QUESTION NO : 11

    QUESTION STATEMENT : Which of the following is NOT true about clients creating and finding enterprise javabeans ?

    CHOICES :

    a) Every find method in the home interface of an entity bean should corresponding to an ejbFind() method in the bean

    itself.

    b) Unlike entity beans, session beans do not implement find methods

    8

  • 8/3/2019 7283499 Ejb Questions

    9/86

    c) An entity bean need not have a create() method in the home interface.

    d) Find methods always return the remote-interface type for the bean

    e) For CMP entity beans, implementations of the find methods are generated automatically at deployment time.

    CORRECT ANSWER : d

    EXPLANATION :

    Choice D is Correct. Find methods of an entity bean can be of 2 types : single-entity or multi-entity

    Single-entity finder methods return a remote interface type appropriate for that bean. Multi-entity finder methods return

    an

    Enumeration or Collection type. Every find method in the home interface of an entity bean should corresponding to an

    ejbFind() method in the bean itself.

    Session beans do not represent an entity in the database, so they do not need find

    methods. An entity bean can be designed without a create() method if it is never meant to be created by a client. Then

    the

    entity can be only by using find() methods on the home interface. For CMP entity beans, implementations of the findmethods

    are generated automatically by the container at deployment time.

    --------------------------------------------------------------------

    QUESTION NO : 12

    QUESTION STATEMENT : Which of the following interfaces provide methods to remove ejbs ?

    CHOICES :

    a) EJBObject

    b) EJBContextc) EJBHome

    d) Context

    e) SessionContext

    f) EntityContext

    CORRECT ANSWER : ac

    EXPLANATION :

    Choices A and C are correct. EJBObject and EJBHome interfaces provide the remove() method which can be used to

    remove the session bean or entity bean. EJBContext interface is the bean class's interface to the container system. It

    provides information about the security identity and transaction status. It also provides access to environment variables

    and

    9

  • 8/3/2019 7283499 Ejb Questions

    10/86

    the bean's EJBHome. But it does not have methods to create, delete or manipulate beans in any other way. So B is not

    the

    right choice.

    SessionContext and EntityContext are specializations of the EJBContext interface. EntityContext has methods

    to get an EntityBean's object reference and primary key. SessionContext provides the session bean instance with an

    interface

    to the container. So E and F are also incorrect. D is incorrect because Context is the starting point for any JNDI lookup.

    This interface represents a naming context, which consists of a set of name-to-object bindings. It contains methods for

    examining and updating these bindings. So D is also a wrong choice.

    --------------------------------------------------------------------

    QUESTION NO : 13QUESTION STATEMENT : In which of the following scenarios would you use BMP in preference to CMP for

    developing an entity bean

    ?

    CHOICES :

    a) The bean's persistent data is stored in more than one data sourceb) The bean's persistent data is stored in a data source that is not supported by the EJB server that you are using

    c) The bean should be defined independently of the database used to store it's state.

    d) The bean should be reusable and flexible across applicationse) Deployment tools are inadequate for mapping the bean instance's state to the database.

    CORRECT ANSWER : abe

    EXPLANATION :

    Choice A, B and E are Correct. Container managed beans are the simplest to develop because they allow you to

    focus on the business logic, delegating the responsibility of persistence to the EJB container.The advantage of container

    managed persistence is that the bean can be defined independent of the database used to store its state. The bean state is

    stored independently which makes the bean more reusable and flexible across applications.

    You must use BMP if any of the

    following is true about an entity bean:-The bean's persistent data is stored in more than one data source or the bean's

    persistent data is stored in a data source that is not supported by the EJB server that you are using. Bean managed

    persistence is also the alternative to container managed persistence when the deployment tools are inadequate for

    mapping

    10

  • 8/3/2019 7283499 Ejb Questions

    11/86

  • 8/3/2019 7283499 Ejb Questions

    12/86

    QUESTION STATEMENT : Which of the following methods should NOT be used in the container managed

    transactions?

    CHOICES :

    a) The commit, setAutoCommit, and rollback methods of java.sql.Connection

    b) The getUserTransaction method of javax.ejb.EJBContextc) getRollbackOnly and setRollbackOnly methods of the EJBContext interface

    d) Any method of javax.transaction.UserTransaction

    CORRECT ANSWER : abd

    EXPLANATION :

    Choice A, B and D are Correct. When you use container managed transactions, we should not invoke any method

    that might interfere with the transaction boundaries set by the container. The commit, setAutoCommit and rollback

    methods of

    java.sql.Connection are used to control JDBC transactions explicitly. The getUserTransaction method of

    javax.ejb.EJBContext

    returns a UserTransaction which is used for explicit transaction management using the Java Transaction APIs. So anymethod

    involving the UserTransaction interface is prohibited in container managed transactions.

    The EJBContext interface provides

    the methods SetRollbackOnly and getRollbackOnly. The getRollbackOnly() method returns true if the current

    transaction has

    been marked for rollback. This can be used to avoid executing work that would not be committed anyway. By invoking

    the

    setRollbackOnly method of the EJBContext interface, the bean method instructs the container to roll back the

    transaction.

    This power can be used if the bean detects a condition that would cause inconsistent data to be committed when the

    transaction completes. These 2 methods can be used only from transactions which are container managed.

    --------------------------------------------------------------------

    QUESTION NO : 16

    QUESTION STATEMENT : The business method of a session bean has the transaction attribute set to Required. Aclient attempts

    to invoke the bean method without a transaction context. What will be the result ?

    CHOICES :

    a) Container creates a new transaction context and invokes the bean method from within that context.

    12

  • 8/3/2019 7283499 Ejb Questions

    13/86

    b) Container invokes bean methods without a transaction context.

    c) TransactionRequiredException is thrown to the client.

    d) TransactionNotSupportedException is thrown to the client

    CORRECT ANSWER : a

    EXPLANATION :

    Choice A is Correct. The transaction attribute defines the transactional manner in which the container invokes

    enterprise bean methods. This attribute is set for individual methods in a bean. Setting the transaction attribute to

    Required, directs the container to invoke the bean method within a transaction context. If a client invokes a beanmethod

    from within a transaction context, the container invokes the bean method within the client transaction context. If a client

    invokes a bean method outside of a transaction context, the container creates a new transaction context and invokes the

    bean

    method from within that context. The transaction context is passed to any enterprise bean objects or resources that areused

    by this bean method.

    --------------------------------------------------------------------

    QUESTION NO : 17

    QUESTION STATEMENT : A method of a stateless session bean which has a transaction attribute of TX_SUPPORTSis invoked by a

    client-initiated transaction. The client receives a TransactionRolledbackException. Which are the possible causes ?

    CHOICES :

    a) The bean method threw a RemoteException

    b) The bean method threw an Application Exception

    c) The bean method threw an EJBException

    d) The bean method threw an Unchecked Exception

    CORRECT ANSWER : d

    EXPLANATION :D is the correct choice. Regardless of the method's transaction attribute, an unchecked exception causes the

    transaction to be rolled back, whether the transaction is container-initiated, client-initiated or bean-managed.Unchecked

    exceptions thrown by a bean in the scope of a transaction always causes a rollback. The container catches the

    unchecked

    13

  • 8/3/2019 7283499 Ejb Questions

    14/86

    exception and rethrows it to the client as a TransactionRolledbackException.

    When a transaction is passed from a client to a

    bean, the client defines the scope of the transaction. So application exceptions and Remote Exceptions thrown by thebeans do

    not automatically cause the transaction to be rolled back. So B and C are not correct. A bean does not throw

    RemoteException,

    it is always thrown by the container itself in response to some exceptions thrown by the bean.

    --------------------------------------------------------------------

    QUESTION NO : 18

    QUESTION STATEMENT : Which of the following method invocations on the EntityContext would cause

    IllegalStateException to be

    thrown in the case of an entity bean ?

    CHOICES :

    a) Invoking getEJBObject() method inside ejbCreate()

    b) Invoking getEJBHome() method inside ejbFind()

    c) Invoking getEJBObject() method inside ebjActivate()

    d) Invoking getPrimaryKey() method inside ejbPostCreate()

    e) Invoking getPrimaryKey() method inside ejbFind()

    CORRECT ANSWER : ae

    EXPLANATION :

    A and E are the correct choices. IllegalStateException is thrown by a method of EntityContext interface if the

    instance invokes this method while the instance is in a state that does not allow the instance to invoke this method.The

    EntityContext is given to the bean instance at the beginning of the lifecycle, before it is made available to service any

    clients. As the bean instance is swapped from one EJB object to the next, the information made available through the

    entitycontext also will change. Thus though the EntityContext is always available to the bean instance, but the instance

    is

    not always assigned to an EJBObject.

    When ejbCreate() method is being executed, the bean is not associated with an

    EJBObject. So calling getEJBObject() within ejbCreate() causes IllegalStateException to be thrown. So A is the right

    choice.

    Calling getPrimaryKey() method within ejbFind method will throw IllegalStateException because when the bean is not

    associated

    14

  • 8/3/2019 7283499 Ejb Questions

    15/86

    with an EJBObject, its has no primary key to return. B does not cause the exception because the home object exists

    when any

    of the bean methods are invoked. C is also a valid invocation because when ejbActivate and ejbPassivate are called, the

    bean

    is already associated with an EJBObject. Though getPrimaryKey() method cannot be invoked within ejbCreate andejbFind

    methods, it is valid to invoke it within ejbPostCreate() method because when this method is called, the bean already has

    a

    valid primary key.

    --------------------------------------------------------------------

    QUESTION NO : 19

    QUESTION STATEMENT : Which of the following ACID properties specifies that a transaction should be committedonly if all the

    tasks in it are completed successfully ?

    CHOICES :

    a) Atomic

    b) Consistent

    c) Isolated

    d) Durable

    CORRECT ANSWER : a

    EXPLANATION :

    A is the right answer. A transaction is a set of operations that transforms data from one consistent state to

    another. The ACID properties that should be followed by a safe transaction are Atomicity, Consistency, Isolation and

    Durability.

    To be atomic, a transaction must execute completely or not at all. If at least one task in the transaction

    fails, the transaction is rolled back. If all the tasks are completed successfully, the transaction is committed. Consistency

    refers to the integrity of the underlying data. A transaction must transition persistent data from one consistent state to

    another. If a failure occurs during processing, the data must be restored to the state it was in prior to the

    transaction.

    Isolation means that a transaction should be allowed to execute without interference from other transactions.

    Durability means that all the data changes during the course of the transaction must be written to some type of physical

    15

  • 8/3/2019 7283499 Ejb Questions

    16/86

    storage before the transaction is successfully completed.This ensures that the changes are not lost if the system crashes.

    --------------------------------------------------------------------

    QUESTION NO : 20

    QUESTION STATEMENT : A web application modeling a chess game is to be developed. The application is to be built

    on J2EE

    platform using different web components like EJB's, JSP's, Servlets. Which of the following type of EJBs is most suitedfor

    representing a game of chess?

    CHOICES :

    a) CMP Entity Bean

    b) BMP Entity Beanc) Stateless Session Bean

    d) Stateful Session Bean

    CORRECT ANSWER : d

    EXPLANATION :

    Choice D is correct. Stateless session beans are those which do not maintain its state between client calling

    its methods. Every method invocation is treated as a new interaction with the client. On the other hand, stateful session

    bean maintains a state that is available to client in the subsequent calls.Entity Beans are convenient components for

    business objects which represent the real life entities. For example, A Customer, A Product, An Order, A Line Item, A

    Campaign Event, A Quotation etc are some entities that you might have in your CRM application.EJB 2.0 specification

    introduced a new type of session bean, which is integrated with a Java Messaging Server (JMS) system.These are calledMessage

    Driven Beans (MDB). It is possible that the JMS associated with the application server, on receiving any message,

    would

    create or invoke a message driven bean.

    A 'Game of Chess' would be represented by a stateful session bean because with every

    call to its move() method, the state is changed and between the call to this method the state would be maintained.

    --------------------------------------------------------------------

    QUESTION NO : 21

    QUESTION STATEMENT : You are responsible for designing and developing various web applications for your

    organization. You

    16

  • 8/3/2019 7283499 Ejb Questions

    17/86

    have an option to choose from various technologies and EJB is one of them. In which of the following scenarios listed

    below,

    EJB is advisable to be used ?

    CHOICES :

    a) Transaction Management is a concern.

    b) You foresee that your application will need to scale beyond initial low usage levels. Also it will need to support

    multiple and concurrent users.

    c) Your application doesn't require platform independence and migration to another vendor is not a concern.d) You want to separate web-tier from business logic.

    e) Your application is a big GUI to a database.

    CORRECT ANSWER : abd

    EXPLANATION :

    Choice A, B and D are correct. A transaction is a unit-of-work or a set of tasks that are executed together.

    Transactions are atomic; in other words, all the tasks in a transaction must be completed together to consider the

    transaction a success. Transactions are managed automatically, so as a bean developer you don't need to use any APIs to

    explicitly manage a bean's involvement in a transaction. Simply declaring the transactional attribute at deployment time

    tells the EJB server how to manage the bean at runtime. Thus choosing EJBs when transaction management is required

    is a wise

    decision. Thus choice A is correct.

    Apart from Transaction Management, the EJB container providers various other facilities

    like Concurrency Service which is very useful in scenarios where a resource is simultaneosly shared by more than one

    user.

    Thus choice B is correct.

    EJBs provide standard APIs for application development. The application developed on one

    Application Server can be migrated to another one with little modifications. Also being written in Java, EJB's provide

    platform independence. A solution like .Net could prove successful in a situation where platform independence is not

    required

    and being locked to single vendor is also acceptable. Thus choice C is not correct.

    If your require your business logic to

    be protected by a firewall, then you can deploy the web server and application server on separate machines and stick a

    firewall in the middle. Thus choice D is correct.

    If your application is just a big GUI to a database--heavy on data logic

    17

  • 8/3/2019 7283499 Ejb Questions

    18/86

    but no business logic--you could achieve a deployment easily using JSPs with tag libraries connecting to a database via

    JDBC.

    Thus EJB is not advisable in this scenario. Thus choice E is not correct.

    --------------------------------------------------------------------

    QUESTION NO : 22

    QUESTION STATEMENT : A stateful session bean which uses bean managed transaction throws IllegalStateException

    when trying to

    commit the transaction. Which of the following is a possible reason for this ?

    CHOICES :

    a) The thread which calls the commit() method is no longer associated with a transaction.

    b) The transaction manager encountered an unexpected error condition.

    c) One of the resources involved in the transaction was unable to perform an update.d) Heuristic decisions were made by one or more resources to roll back the transaction.

    e) Heuristic decisions were made by some resources to rollback the transaction and some resources to commit.

    CORRECT ANSWER : a

    EXPLANATION :

    A is the correct answer. Bean-managed transactions are programmatically demarcated within your bean

    implementation. The transaction is completely controlled by the application. Bean managed transactions make use of

    UserTransaction interface which provides methods like begin,commit and rollback for explicit transaction management.

    The

    commit() method completes the transaction that is associated with the current thread.

    This method can throw several checked

    exceptions. IllegalStateException is thrown if the current thread is not associated with a transaction. So A is correct in

    this case. SystemException is thrown if the transaction manager (the EJB server) encounters an unexpected error

    condition.

    TransactionRolledbackException is thrown when the entire transaction is rolled back instead of committed; this can

    happen if

    one of the resources was unable to perform an update or if the UserTransaction.rollBackOnly() method was called.

    HeuristicRollbackException indicates that heuristic decisions were made by one or more resources to roll back the

    transaction. HeuristicMixedException indicates that heuristic decisions were made by resources to both roll back and

    commit

    the transaction; some resources decided to roll back while others decided to commit.

    18

  • 8/3/2019 7283499 Ejb Questions

    19/86

    --------------------------------------------------------------------

    QUESTION NO : 23

    QUESTION STATEMENT : The following sequence of method calls occurred on a CMP entity bean instance when itsbusiness methods

    method1 and method2 were invoked from the same method of a client which has initiated a transaction.

    entityBean.ejbLoad()

    entityBean.method1()

    entityBean.method2()entityBean.ejbStore()

    Which of the following MUST be true about the bean ?

    CHOICES :

    a) The business methods are configured with the transaction attribute of 'Never'

    b) method1 and method2 are in the same transaction scope

    c) method1 and method2 are in different transaction scopesd) The business methods are NOT configured with a transaction attribute of 'Requires New'

    CORRECT ANSWER : bd

    EXPLANATION :

    B and D are the correct answers. In container managed persistence, the ejbLoad() method is always called after

    the updation of the bean's container managed fields with data from the database. ejbStore() method is called just before

    the

    container writes the container-managed fields to the database. So typically, ejbLoad is called before a transaction and

    ejbStore after a transaction.

    If method1 and method2 had belonged to different transactions, ejbStore() would have been

    called after the first business method invocation and ejbLoad() before the second business method call. Since that did

    not

    happen, B is correct and C is not. The methods method1 and method2 are part of the same transaction scope.

    D is true because

    if the business methods had been configured with 'Requires New' option each of them would have started a new

    transaction of

    their own. They would not have been part of the same transaction. If a bean method is configured with a transaction

    attribute value of 'Never', if the calling client is part of a transaction, the bean throws a RemoteException. Since in the

    above case, the bean did not throw any exception even though the client was part of a transaction, A is not true.

    --------------------------------------------------------------------

    19

  • 8/3/2019 7283499 Ejb Questions

    20/86

    QUESTION NO : 24

    QUESTION STATEMENT : What value should be returned by the ejbCreate() of a CMP entity bean ?

    CHOICES :

    a) null

    b) void

    c) Primary Key object reference

    d) EJBObject reference

    e) Bean reference

    CORRECT ANSWER : a

    EXPLANATION :

    Choice A is correct. The ejbCreate() method returns a null value of the primary key type for the bean.In a

    CMP bean, the ejbCreate() method is called just prior to writing the beans container managed fields to the database. In

    a

    CMP bean, ejbCreate() is only used to initialize the fields of the bean instance. So the return value of the ejbCreate()

    method for a CMP bean is ignored.In the case of BMP beans, the ejbCreate() methods are responsible for adding the

    new entity

    to the database. For BMP beans, it should return the primary key of the newly created entity.

    --------------------------------------------------------------------

    QUESTION NO : 25

    QUESTION STATEMENT : An entity bean needs to invoke a method on another bean, passing itself as one of the

    parameters. Which

    of the following is the best way of achieving this ?

    CHOICES :

    a) Pass a this reference as the parameter for the method

    b) Call the getPrimaryKey() method of the bean, get the EJBObject using the primary key and pass it as the method

    parameter

    c) Call the getHandle() method of the bean, get the EJBObject using the handle and pass it as the method parameterd) Call the getEJBObject() method on the EJBContext and pass it as the method parameter

    CORRECT ANSWER : d

    EXPLANATION :

    Choice D is Correct. The getEJBObject() method in the EJBContext interface returns a remote reference to the

    bean instance's EJB object.The purpose of this method is to provide the bean instance with a reference to itself when it

    20

  • 8/3/2019 7283499 Ejb Questions

    21/86

    needs to pass itself as a method argument.The bean is provided with its entity context by the container by calling the

    method

    setEntityContext() and EntityContext inherits EJBContext. So only one method invocation is required, which makes Dthe best

    option.

    It is illegal for a bean instance to pass a this reference to another bean, instead it passes it's remote reference

    which the bean instance gets from its context.So A is not the right option. The getHandle() method returns the bean

    handle

    which is a serializable reference to the EJB object. So it allows us to create an EJB object remote reference by calling

    it's

    getEjbObject() method.But B is not the right option because getting the handle and then the EJBObject results in 2

    method

    calls.

    The getPrimaryKey() returns a reference to the primary key of the bean. While the primary key can be used to get

    the EJBObject using a finder method, this is also not an efficient way because it involves 2 method calls and a jndi look

    up

    by the client. So D is the right answer.

    --------------------------------------------------------------------

    QUESTION NO : 26

    QUESTION STATEMENT : Which of the following is the last method invoked by the container at the end of the

    lifecycle of an

    entity bean ?

    CHOICES :

    a) The unsetEntityContext() method

    b) The ejbRemove() method

    c) The ejbPassivate() method

    d) The ejbStore() method

    CORRECT ANSWER : a

    EXPLANATION :

    Choice A is Correct. When an entity bean instance leaves the instance pool to be garbage collected, the

    unsetEntityContext() method is invoked by the container to alert the bean instance that it is about to be destroyed. So A

    is

    21

  • 8/3/2019 7283499 Ejb Questions

    22/86

    the right answer.

    B is not correct because ejbRemove() is called when the client application invokes one of the remove()

    methods on the bean's ejbObject or ejbHome. With entity beans, invoking a remove method means that the entity's data

    is

    deleted from the database. Once the ejbRemove() method has finished, the bean instance is moved back to the instance

    pool.

    The ejbPassivate() method is called when the EJB container passivates the bean ie; the container may disassociate the

    bean

    instance from an EJB object when it is not busy. So C is also wrong.

    ejbStore() method is called just before the container

    writes the container-managed fields to the database. So D is also not the right choice.

    --------------------------------------------------------------------

    QUESTION NO : 27

    QUESTION STATEMENT : Which of the following allows an entity bean instance to obtain its own primary key and a

    remote

    reference to the EJB object ?

    CHOICES :

    a) EntityContext

    b) InitialContextc) Home object

    d) EJBContext

    CORRECT ANSWER : a

    EXPLANATION :

    Choice A is Correct. Entity Context is really the bean instance's interface to the container.The EntityContext

    allows the bean instance to obtain its own primary key and a remote reference to the EJB object.The setEntityContext()

    method

    is called prior to the bean instance's entry into the instance pool.

    When a bean instance is associated to an EJB object,

    its EntityContext changes so that the primary key and EJBObject obtainable through the entityContext match the

    EJBObject the

    bean instance is currently associated with.After the bean instance is removed from the instance pool and before the bean

    22

  • 8/3/2019 7283499 Ejb Questions

    23/86

    instance is garbage collected, the unsetEntityContext is invoked which indicates that the bean instance's EntityContext

    is no

    longer implemented by the container.

    --------------------------------------------------------------------

    QUESTION NO : 28

    QUESTION STATEMENT : Which of the following is NOT true about session beans ?

    CHOICES :

    a) Stateless Session beans are neither passivated nor activated.

    b) A session bean does not survive server crashes.

    c) Stateless session beans do not participate in instance pooling.

    d) None of the above

    CORRECT ANSWER : c

    EXPLANATION :

    Choice C is Correct. Passivation is the process of preserving the conversational state of a Stateful Session

    Bean when the client is not using it. Activation is the act of restoring the saved state when the client invokes a method

    of

    the passivated bean. Stateless session beans do not maintain the conversational state between method invocations. So

    they do

    not need to be passivated and activated.

    Instance pooling is a technique used by EJB servers to create multiple copies of

    enterprise beans and then distribute them as needed. It reduces the resources needed at a time.Stateless session beans

    and

    entity beans participate in instance pooling. Stateful session beans do not participate in pooling because they maintain

    the

    unique state of each client and hence cannot be reused.

    --------------------------------------------------------------------

    QUESTION NO : 29

    QUESTION STATEMENT : Which of the following are true for primary keys of entity beans ?

    CHOICES :

    a) Primary keys can be primitive types (int, double, long) etc.

    23

  • 8/3/2019 7283499 Ejb Questions

    24/86

    b) All fields in the primary key class must be declared public.

    c) The primary key must be serializable

    d) The primary key class must implement equals() and hashcode() methods.

    CORRECT ANSWER : bcd

    EXPLANATION :

    Choice B, C and D are Correct. Although primary keys can be primitive wrappers, primary keys cannot be

    primitive types, because some of the semantics of Ejb interfaces prohibit the use of primitives.

    All fields in the primary

    key should be declared public so that the container can read the fields at runtime via java reflection.Since the primary

    key

    would be used in remote invocations,it should be serializable. It should override equals and hashcode methods so that

    they

    behave properly when invoked.

    --------------------------------------------------------------------

    QUESTION NO : 30

    QUESTION STATEMENT : A client invokes a method on a Stateful Session bean which resulted inNoSuchObjectException. What are

    the possible causes ?

    CHOICES :

    a) The bean had already been removed by the container

    b) The bean had timed outc) The ejb server was restarted after the last method call on the bean

    d) The bean is being used by another client

    e) The code in the bean method caused an error

    f) The method parameters passed to the method are invalid

    CORRECT ANSWER : abc

    EXPLANATION :

    A,B and C are the correct answers. NoSuchObjectException is raised by a session bean in three situations. If

    the bean does not exist because it was already removed by the container because of a remove method call or some other

    reason,

    this exception can be thrown when a method is invoked on the bean. Timeout period for a bean is declared at

    deployment time.

    24

  • 8/3/2019 7283499 Ejb Questions

    25/86

    If a bean times out, the container can remove the bean instance from the method ready state. In this case also, a method

    invocation on the bean results in NoSuchObjectException.

    Session beans cannot survive server crashes, so if server is

    restarted after the bean creation, when the method invocation happens, this exception is thrown. Case D will neverhappen

    because the same stateful session bean can never be accessed by more than one client. If the code in the bean method

    caused

    an error, or if the parameters passed to the method are invalid, application exceptions or RemoteException is thrown.So D,E

    and F are not correct.

    --------------------------------------------------------------------

    QUESTION NO : 31

    QUESTION STATEMENT : Which of the following are true about session beans implementing the

    SessionSynchronization interface?

    CHOICES :

    a) A stateless session bean should not implement the SessionSynchronization interface.

    b) The session bean's instance variables can be reset to some initial values if the afterCompletion() method indicatesthat

    the transaction was rolled back.c) The session bean's instance variables can be reset to some initial values if the beforeCompletion() method indicates

    that

    the transaction was rolled back.

    d) The session bean should write the cached data to the database in the beforeCompletion() methode) The beforeCompletion() method is always invoked, but the afterCompletion() method may not be invoked always.

    CORRECT ANSWER : abd

    EXPLANATION :

    Choice A, B and D are Correct. A stateless session bean should not implement the SessionSynchronization

    interface because they have no conversational state. So each method invocation of a stateless session bean must make

    changes

    to the database explicitly. With stateful session beans we might not want to make changes to the database till the

    transaction is complete ie; avoid database updates if the transaction is rolled back and postpone updates till the

    transaction is committed.

    25

  • 8/3/2019 7283499 Ejb Questions

    26/86

    The container invokes the beforeCompletion method after the business method has finished, but just

    before the transaction commits. The beforeCompletion method is the last opportunity for the session bean to roll back

    the

    transaction (by calling setRollbackOnly). If it hasn't already updated the database with the values of the instance

    variables, the session bean may do so in the beforeCompletion method. The afterCompletion method indicates that the

    transaction has completed. It has a single boolean parameter, whose value is true if the transaction was committed and

    false

    if it was rolled back. If a rollback occurred, the session bean can refresh its instance variables from the database in the

    afterCompletion method. So B is true and C is not.

    --------------------------------------------------------------------

    QUESTION NO : 32QUESTION STATEMENT : A developer is writing a bean which has methods which read and update certain rows from

    a database

    table. The only requirement is that no other transaction should be able to make changes to the rows which have been

    read by

    the bean till the current transaction of the bean is completed. Which would be the BEST transaction isolation level for

    the

    bean ? Assume that the transaction attribute is set to Required.

    CHOICES :

    a) Read Only

    b) Read Uncommitted

    c) Read Committedd) Repeatable Read

    e) Serializable

    CORRECT ANSWER : d

    EXPLANATION :

    Choice D is Correct. Since the requirement is that the data read by the transaction once cannot be changed by

    other transactions till this transaction is completed ,we should choose Repeatable Read as the isolation level. It prevents

    dirty reads and non repeatable reads. So you will be able to read each of the rows that you are modifying and then be

    able to

    update each row knowing that none of the rows are being modified by any of the concurrent transactions. So if you

    choose to

    26

  • 8/3/2019 7283499 Ejb Questions

    27/86

    reread the data at any time, the rows would have the same data.

    If we use Read Committed or Read Uncommitted, another

    concurrent transaction may commit data between your reads. There is no isolation level called Read Only. Serializablelevel

    is the slowest and most restrictive. So D is a better option than E.

    --------------------------------------------------------------------

    QUESTION NO : 33

    QUESTION STATEMENT : For Container Managed Transactions, whenever a System exception is thrown from within

    an EJB which of

    the following is handled automatically by the container ?

    CHOICES :

    a) Rollback the transaction

    b) Throw an EJB Exception(or its subtypes) back to the client

    c) Throw a RemoteException(or its subtypes) back to the client

    d) Dereference and garbage collect the bean instance

    e) Lets the client to recover & execute other methods of the same bean.

    CORRECT ANSWER : acd

    EXPLANATION :

    Choice A, C and D are Correct. System exceptions are RuntimeExceptions, RemoteExceptions and their subtypes.

    System exceptions always cause a transaction to roll back when thrown from a bean method. The container

    automatically rolls

    back the transaction,logs the exception, discards the bean instance and throws a RemoteException or its subtypes whena

    System Exception is thrown. The bean instance is dereferenced and garbage collected because the container assumes

    that the

    instance maybe unstable or corrupted. If the client started the transaction, the SystemException is caught by the

    container

    and is rethrown as TransactionRolledBackException. In other cases, the container catches the exception and rethrows its

    as

    RemoteException. EJBException is not thrown as a result of this. The client cannot execute other methods of the same

    bean.

    --------------------------------------------------------------------

    27

  • 8/3/2019 7283499 Ejb Questions

    28/86

    QUESTION NO : 34

    QUESTION STATEMENT : You have a CMP entity bean which includes an array of complex numbers which you wantto save in the

    database in some appropriate format since the database does not support arrays . Which of the following callback

    methods

    would be appropriate to place the formatting code ?

    CHOICES :

    a) ejbCreate

    b) ejbPostCreate

    c) ejbPassivate

    d) ejbActivate

    e) ejbStore

    CORRECT ANSWER : e

    EXPLANATION :

    E is the correct answer. The ejbStore method is called when the container decides that it is a good time to

    write the entity bean's data to the database. In container-managed persistence, the bean's container-managed fields are

    automatically synchronized with the database.

    In most cases, we will not need the ejbLoad() and ejbStore methods because

    persistence in container-managed beans is uncomplicated. We need to provide the ejbLoad and ejbStore callback

    methods in

    container-managed beans only if more sophisticated logic is needed when synchronizing container-managed fields.

    Data intended

    for the database can be reformatted or compressed to conserve space; data just retrieved from the database can be usedto

    calculate derived values for nonpersistent properties.

    If we want to save an array of complex numbers in the database,

    since relational databases do not support arrays, so you need to convert the array into some other format. We can use the

    ejbStore() method to convert the data into the appropriate format before saving, since it will be called by the container

    at

    that time. So E is the correct answer. To read the data back in the form of the array, we can use the ejbLoad() function

    also. ejbCreate is not the right place for this, because it is called only when a new record is inserted, same applies for

    ejbPostCreate. ejbPassivate is called before the bean is passivated ie; the bean instance returns to the pool. It is not

    28

  • 8/3/2019 7283499 Ejb Questions

    29/86

    called before saving the persistent state of the bean. ejbActivate is called when the bean moves from the pooled state to

    the

    ready state. So this is also not the place where we can put the formatting code.

    --------------------------------------------------------------------

    QUESTION NO : 35

    QUESTION STATEMENT : Which of the following methods is used by a client to find out if a particular enterprise

    bean is a

    session bean or an entity bean ?

    CHOICES :

    a) isSession() method of EJBMetaData interface

    b) isEntity() method of EJBMetaData interface

    c) isSessionBean() method of EJBHome interfaced) isEntityBean() method of EJBHome interface

    CORRECT ANSWER : a

    EXPLANATION :

    A is the correct answer. EJBMetaData interface describes the home interface, remote interface and primary key

    classes, and also whether a bean is a session bean or an entity bean. This type of metadata is useful for Java tools like

    IDEs used for building applications that use deployed enterprise Beans, and for clients using a scripting language to

    access

    the enterprise Bean. The isSession method in the EJBMetaData interface returns true if the bean is a session bean and

    false

    if it is an entity bean.

    The EJBMetaData object is obtained by calling the function getEJBMetaData of the EJBHome class.

    The EJBMetaData is not a remote interface. The class that implements this interface (this class is typically generated by

    container tools) must be serializable, and must be a valid RMI/IDL value type.

    --------------------------------------------------------------------

    QUESTION NO : 36

    QUESTION STATEMENT : One of the methods of a stateful session bean which has a transaction attribute of Supports

    is invoked

    by a non-transactional client. The stateful bean in turn invokes a method of a stateless session bean which has a

    transaction

    29

  • 8/3/2019 7283499 Ejb Questions

    30/86

    attribute of Mandatory. What will the possible result ?

    CHOICES :

    a) The container will throw TransactionNotSupported Exceptionb) The container will throw TransactionRequiredException

    c) The container will start a new transaction for the execution of the method of the stateless beand) The container will start a new transaction for the execution of the method of the stateful bean

    e) The container will execute all the methods successfully without any transaction

    CORRECT ANSWER : b

    EXPLANATION :

    B is the correct choice. The container will throw a TransactionRequiredException in this case. 'Supports'

    transactional attribute means that the bean method will be included in the transaction scope if it is invoked within a

    transaction, else it will be executed without a transaction scope.

    When the stateful session bean which has a transaction

    attribute of 'Supports' is invoked by a non-transactional client, it will also execute without a transaction. So when it

    invokes a method of the stateless bean it will not be executing as part of a transaction. But for the second bean, the

    transaction attribute is set to Mandatory. This attribute means that the bean method must always be part of the

    transaction

    scope of the calling client. If the calling client is not part of a transaction, the invocation will fail, throwing a

    TransactionRequiredException. So B is the choice in this case.

    --------------------------------------------------------------------

    QUESTION NO : 37QUESTION STATEMENT : Which of the following EJB transaction isolation attributes ensure that dirty and non-

    repeatable reads

    do not occur?

    CHOICES :

    a) Serializableb) Read Committed

    c) Read Uncommittedd) Repeatable Read

    CORRECT ANSWER : ad

    EXPLANATION :

    A and D are the correct choices. The isolation level measures concurrent transactions' capacity to view data

    30

  • 8/3/2019 7283499 Ejb Questions

    31/86

    that have been updated, but not yet committed, by another transaction. If other transactions were allowed to read data

    that

    are as-yet uncommitted, those transactions could end up with inconsistent data were the transaction to roll back, or endup

    waiting unnecessarily were the transaction to commit successfully.

    The isolation attribute values are Read Uncommitted, Read

    Committed, Repeatable Read and Serializable. Read Uncommitted: Data that have been updated but not yet committed

    by a

    transaction may be read by other transactions. These are called dirty reads.Read Committed: Only data that have been

    committed by a transaction can be read by other transactions. Repeatable Read: Only data that have been committed by

    a

    transaction can be read by other transactions, and multiple reads will yield the same result as long as the data have not

    been committed. Serializable: This, the highest possible isolation level, ensures a transaction's exclusive read-write

    access

    to data. It includes the conditions of ReadCommitted and RepeatableRead and stipulates that all transactions run serially

    to

    achieve maximum data integrity. This yields the slowest performance and least concurrency. Thus dirty reads and

    non-repeatable reads can be avoided by using the trasaction isolation attributes of Serializable or Repeatable Read.So A

    and

    D are correct.

    --------------------------------------------------------------------

    QUESTION NO : 38

    QUESTION STATEMENT : You are adding a Customer EJB to a Telecom Enterprise application. The Operator role

    should be allowed

    to create a customer. Which of the following method permission settings will enable this ?

    CHOICES :

    a) Create a Method Permission mapping the Operator role to all the methods in the Customer EJB

    b) Create a Method Permission mapping the Operator role to all the home methods of the Customer EJBc) Create a Method Permission mapping the Operator role to the ejbCreate method in the Customer EJB

    d) Create a Method Permission mapping the Operator role to the create method in the Customer EJB home

    CORRECT ANSWER : bd

    EXPLANATION :

    31

  • 8/3/2019 7283499 Ejb Questions

    32/86

    B and D are the correct choices. A method permission is a mapping between one or more security roles and one

    or more methods that a member of the role can invoke. To create an instance of the Customer bean, the method to be

    called by

    the client is the create method in the home object of the Customer EJB.

    Method permission can be set to all the bean

    methods, a particular bean method, all the home methods or a particular home method. So in this case to give the

    operator

    role the permission to call the create method in the EJB home, we can either set the Method Permission mapping theoperator

    role to all the home methods of the Customer bean or only to the create method in the home of Customer EJB.Setting

    permission

    to all the bean methods or the ejbCreate method of the bean will not help.So A and C are wrong answers.

    --------------------------------------------------------------------

    QUESTION NO : 39

    QUESTION STATEMENT : A JDBC data source resource for a DB2 database has been configured in the deployment

    descriptor of a

    bean with the logical name of 'mysource'. Which of the following paths would be used to access it from the bean using

    JNDI

    look up ?

    CHOICES :

    a) java:comp/env/mysource

    b) java:comp/env/jdbc/mysource

    c) java:comp/env/db2/jdbc/mysource

    d) java:comp/env/jdbc/db2/mysource

    CORRECT ANSWER : a

    EXPLANATION :

    A is the correct answer. All deployed beans,environment properties and external resources like data sources

    are mapped to a set of JNDI entries which exist in a namespace called the environment naming context(ENC) or defaultJNDI

    context. When developing a bean, the bean developer can identify the types of resources and enterprise beans that will

    be

    referenced in the bean and bind them to the ENC.

    Using java:comp/env namespace within the enterprise bean lets the bean

    32

  • 8/3/2019 7283499 Ejb Questions

    33/86

    locate external information without prior knowledge of how the external information is named and organized in the

    target

    operational environment. Using this naming context will not tie up a bean with the JNDI name of a resource that is

    known only

    at the time of deployment. The default context exists in the namespace called "java:comp/env" and its subdirectories.When

    the bean is deployed all the beans and resources it uses are mapped into this directory. Thus in this case the bean can

    look

    up the data source using its logical name "mysource" by specifying java:comp/env/mysource.

    Though this works, it is

    advisable to keep a subcontext which describes the resource type. ( eg: ejb/ for beans, jdbc/ for JDBC DataSource

    factory

    and so on). So if the resource is given a logical name as jdbc/mysource, to look up the resource, you say

    java:comp/env/jdbc/mysource

    --------------------------------------------------------------------

    QUESTION NO : 40

    QUESTION STATEMENT : The env-ref element in the deployment descriptor of an enterprise bean is used to define

    CHOICES :

    a) references to other beansb) references to external resources

    c) references to environment entries

    d) references to security roles

    CORRECT ANSWER : a

    EXPLANATION :

    A is the correct answer. The env-ref element in the deployment descriptor is used to define references to

    other beans within the JNDI ENC. This makes it much easier for beans to reference other beans, they can use JNDI to

    look up a

    reference to the home interface for any beans they are interested in. So A is right. For external resources, mapping is

    performed by the resource-ref element. To define environmental entries, env-entry element is used.The security-role-ref

    element is used to define the secuirity roles that are used by a bean.

    --------------------------------------------------------------------

    33

  • 8/3/2019 7283499 Ejb Questions

    34/86

    QUESTION NO : 41

    QUESTION STATEMENT : Which of the following are valid for bean managed transactions ?

    CHOICES :

    a) A Stateful Session bean must start and end a transaction within one method, since it serves multiple clients.

    b) A Stateful Session bean's transactions can span multiple methods, since it serves only one client.

    c) A Stateless Session bean's transactions must start and end in the same method, since it serves multiple clients.

    d) A Stateless Session bean's transactions can span multiple methods, since it serves only one client.

    CORRECT ANSWER : bc

    EXPLANATION :

    B and C are the correct answers. Bean managed transactions are explicit transactions managed by the bean, for

    which the bean makes use the UserTransaction interface. In EJB, implicit transaction management is provided on the

    bean

    method level, so transactions execute within the scope of the method. But rarely explicit control on transactions is

    required

    which is achieved by calling the methods of the UserTransaction interface.

    With stateless session beans, transactions that

    are managed using the UserTransaction must be started and completed within the same method. This is required

    because

    stateless session bean instances are shared across multiple clients. With stateful session beans, a transaction can begin in

    one method and be committed in another because a stateful bean is not used by more than one client.Thus a statefulbean

    instance can associate itself with a transaction across different client-invoked methods.

    --------------------------------------------------------------------

    QUESTION NO : 42

    QUESTION STATEMENT : Which of the following enterprise beans allow multithreaded access ?

    CHOICES :

    a) Stateful Session Bean

    b) Reentrant Entity Beanc) Activated Stateless Session Bean

    d) Concurrent Stateless Session Bean

    e) BMP Entity Bean

    CORRECT ANSWER : b

    34

  • 8/3/2019 7283499 Ejb Questions

    35/86

    EXPLANATION :

    B is the right answer. Reentrance is when a thread of control attempts to reenter a bean instance.In EJB, bean

    instances are non-reentrant by default, that means loopbacks are not allowed. Session beans can never be reentrant and

    they

    throw a RemoteException if a loopback is attempted. The same is true of a non-reentrant entity bean. Entity beans canbe

    configured in the deployment descriptor to allow reentrance at deployment time. If you permit reentrance you also

    permit

    multithreaded access to the bean instance.

    Multithreaded access to a bean instance can result in corrupted data because

    threads interfere with each other. So declaring an entity bean as reentrant is discouraged in the EJB specifications,

    unless

    you have a very good reason of doing so.A, C and D are wrong because session beans cannot be reentrant in any case. E

    is

    wrong because entity beans are not reentrant unless they are declared so.

    --------------------------------------------------------------------

    QUESTION NO : 43

    QUESTION STATEMENT : Which of the following are valid differences between ejbCreate() and ejbPostCreate()methods of a CMP

    entity bean ?

    CHOICES :

    a) The bean identity is not available to the bean during the call to ejbCreate(), but is available in the ejbPostCreate()

    method.

    b) The caller's identity is not available to the bean during the call to ejbCreate(), but it is available in the

    ejbPostCreate() method

    c) ejbCreate() returns null while ejbPostCreate() returns void

    d) ejbCreate() should have the same parameters as the calling create() method, but ejbPostCreate() does not take the

    same

    parameters.

    e) ejbCreate() is called before the insertion of a record into the database, while ejbPostCreate() is called after the

    record insertion.

    CORRECT ANSWER : ace

    EXPLANATION :

    35

  • 8/3/2019 7283499 Ejb Questions

    36/86

    A,C and E are the correct answers. In a CMP bean, the ejbCreate() method is called just prior to writing the

    bean's container managed fields to the database. Values passed in to the ejbCreate() method should be used to initialize

    the

    fields of the bean instance. Once the ejbCreate() method completes, the new record with these values are written to the

    database tables. The EntityContext maintained by the bean instance does not provide it with proper identity till the

    ejbCreate() method has completed. So inside the ejbCreate() method, the bean instance doesn't have access to it's

    primary key

    or EJBObject. So A is correct.

    B is not correct because EntityContext methods like getCallerPrincipal() and

    isCallerInRole() can be called from ejbCreate() since user details and environment properties are available at this time.

    So

    B is not correct. C is true because the return value of ejbCreate() is null while ejbPostCreate() does not return anything.

    D

    is not correct because ejbCreate() and ejbPostCreate() both take the same set of parameters. E is also correct,ejbCreate()

    is called prior to insertion of records while ejbPostCreate() is called after that.

    --------------------------------------------------------------------

    QUESTION NO : 44

    QUESTION STATEMENT : Which of the following events take place first after the call of ejbCreate() method on a

    CMP entity bean

    ?

    CHOICES :

    a) Insertion of a record into the database

    b) Calling ejbPostCreate() method

    c) EJB object stub returned to the client

    d) Associating the EJB Object with the bean instance

    CORRECT ANSWER : a

    EXPLANATION :

    A is the correct choice. In a container-managed bean, the ejbCreate() method is called just prior to writing

    the bean's container-managed fields to the database. Values passed in to the ejbCreate() method should be used to

    initialize

    the fields of the bean instance. Once the ejbCreate() method completes, a new record, based on the container-managed

    fields,

    36

  • 8/3/2019 7283499 Ejb Questions

    37/86

    is written to the database. Once the record has been inserted into the database, the bean instance is ready to be assigned

    to

    an EJB object.

    Once the bean is assigned to an EJBobject, the bean's identity is available. This is when the ejbPostCreate()

    method is invoked. Finally, when the ejbPostCreate() processing is complete, the bean is ready to service client

    requests.

    The EJB object stub is created and returned to client application, which will use it to invoke business methods on the

    enterprise bean. So the order of execution in this case would be A,D,B and C.A is the correct answer because the record

    insertion is the first event to take place after ejbCreate().

    --------------------------------------------------------------------

    QUESTION NO : 45QUESTION STATEMENT : Which of the following features should be avoided in the implementation of Enterprise

    Java Beans ?

    CHOICES :

    a) Declaring static, final fields

    b) Loading classes

    c) Using file access

    d) Loading native libraries

    e) thread creation and managementf) Listen to or accepting connections on a socket

    CORRECT ANSWER : cdef

    EXPLANATION :

    Choices C,D,E and F are the correct answers. EJB component providers/developers should be aware of and

    strictly follow certain restrictions in the interest of developing reliable and portable EJB components. Nonfinal static

    class fields are disallowed because enterprise bean instances will behave differently depending on whether or not they

    are

    distributed.

    It is acceptable practice to use static class fields if those fields are marked as final. Since final fields

    cannot be updated, instances of the enterprise bean can be distributed by the container without concern for those fields'

    values becoming unsynchronized. So A is incorrect.

    Loading classes are allowed, so B is incorrect.C is correct- Enterprise

    beans aren't allowed to access files primarily because files are not transactional resources. Allowing EJBs to access files

    or directories in the filesystem, or to use file descriptors, would compromise component distributability, and would be a

    37

  • 8/3/2019 7283499 Ejb Questions

    38/86

    security hazard.

    D is correct because EJBs are not allowed to load native libraries since it affects portability,stability

    and security. E is correct- EJBs are not encouraged to use threads because allowing enterprise bean instances to create

    and

    manage threads would interfere with the container's ability to control its components' lifecycle.F is correct, EJBs are not

    allowed to listen to or accept socket connections. If an enterprise bean is listening on a socket, it can't be passivated -

    it must always be available. Enterprise beans can be network socket clients, and so they can use other network resources

    (including other enterprise bean servers) to do their jobs.

    --------------------------------------------------------------------

    QUESTION NO : 46

    QUESTION STATEMENT : A stateful session bean's conversational state has the following types of variables. Whichof the them

    do NOT get restored after passivation and activation of the bean ?

    CHOICES :

    a) SessionContext

    b) EJBContextc) transient variables

    d) Handle

    e) EJBHomef) EJBObject

    CORRECT ANSWER : adef

    EXPLANATION :

    Choices A,D,E and F are correct. When a stateful bean is passivated, the conversational state of the bean is

    written to the secondary storage associated with the EJBObject. When the bean is activated, the saved state is restored.

    A

    bean's conversational state should consist of only primitive values, objects that are serializable and these special types-

    SessionContext, EJBHome, EJBObject, UserTransaction and Context (only when it references the JNDI ENC). So A,E

    and F are

    correct.

    D is correct because Handle is a serializable reference to the EJBObject.Transient variables will not be preserved

    when the bean is passivated.So C is not the right option. B is not correct because EJBContext does not implement

    38

  • 8/3/2019 7283499 Ejb Questions

    39/86

    Serializable. So it will not be preserved during bean passivation.

    --------------------------------------------------------------------

    QUESTION NO : 47QUESTION STATEMENT : Which method can be called on the EJBContext to obtain information about the user

    making the EJB method

    invocation ?

    CHOICES :

    a) getCallerPrincipal()

    b) getPrincipal()

    c) getRemoteUser()

    d) getUserPrincipal()

    CORRECT ANSWER : a

    EXPLANATION :Choice A is Correct. The servlet and EJB specifications provide different mechanisms to programmatically

    obtain identity information about the user invoking a method on a secure servlet or an EJB. getCallerPrincipal() is the

    method in the EJBContext interface used by a bean to track the identity of the client accessing the bean.

    To get the name

    of the client, we have to only invoke getName() method on the Principal object returned by getCallerPrincipal()

    method. B is

    not correct because there is no such method called getPrincipal(). C and D are applicable only to servlets. Invoking the

    getRemoteUser method on the HttpRequestObject returns the name of the user who is invoking the servlet. The

    getUserPrincipal

    method on the HttpRequestObject returns the Principal object corresponding to the user who is invoking the servlet.

    --------------------------------------------------------------------

    QUESTION NO : 48

    QUESTION STATEMENT : A developer is creating a Java tool which will have wizards for interacting with anenterprise bean from

    a client's perspective. He uses EJBMetaData interface for getting details about the bean. Which of the following

    information

    is NOT directly obtained using this interface ?

    39

  • 8/3/2019 7283499 Ejb Questions

    40/86

    CHOICES :

    a) Whether the EJB is a session or an entity bean

    b) The home interface class of the EJB

    c) The primary key class of the EJBd) The remote interface class of the EJB

    e) The bean class of the EJB

    CORRECT ANSWER : e

    EXPLANATION :

    Choice E is Correct. The javax.ejb.EJBMetaData interface has methods to get the home interface, remote

    interface and primary key classes of the EJB. It also has a method to get the information about the type of the bean -

    whether it's a session bean or an entity bean. There is no way to get the bean class using EJBMetaData because the bean

    class

    is not part of the client API and hence does not belong to the metadata.

    The getEJBMetaData() method of the EJBHome

    interface returns an instance of EJBMetaData. A tool can have wizards which use the class definitions provided by the

    EJBMetaData with Java reflection to interact with a bean. A class which implements EJBMetaData should be

    Serializable, which

    allows these tools to save the EJBMetaData object for later use.

    --------------------------------------------------------------------

    QUESTION NO : 49

    QUESTION STATEMENT : When a BMP entity bean instance is activated from the pooled state to the ready state

    which of the

    following does NOT happen ?

    CHOICES :

    a) The bean leaves the instance pool to be assigned to an EJB object

    b) The non-persistent fields of the bean are deserialized from secondary storage and assigned.

    c) Container invokes ejbLoad() to notify the bean that its persistent fields have been synchronizedd) Non-persistent fields contain arbitrary values (dirty values) and need to be reinitialized in the ejbActivate() method.

    e) Container invokes setEntityContext() method on the bean.

    CORRECT ANSWER : be

    EXPLANATION :

    Choice B and E are correct. When an entity bean is activated, the bean instance leaves the instance pool to be

    40

  • 8/3/2019 7283499 Ejb Questions

    41/86

    assigned to an EJB object. Once assigned to an EJBObject, the ejbActivate() method is called . For entity beans, the

    non-persistent fields are not serialized to secondary storage at the time of passivation as done for Stateful Session

    Beans.

    So at the time of activation, they are not deserialized. These fields contain arbitrary values(dirty values) and must be

    reinitialized in the ejbActivate() method.

    --------------------------------------------------------------------

    QUESTION NO : 50

    QUESTION STATEMENT : Which of the following is NOT true about defining methods in a CMP entity bean class ?

    CHOICES :

    a) The bean class's business methods should throw javax.ejb.RemoteException

    b) For every create() method defined in the entity bean's home interface, there must be a correspondingejbPostCreate()

    method in the bean instance class.

    c) For every create() method defined in the entity bean's home interface, there must be a corresponding ejbCreate()

    method

    in the bean instance class.

    d) The ejbPostCreate() method always returns void.

    CORRECT ANSWER : a

    EXPLANATION :Choice A is Correct. The bean class's business methods are not required to throw RemoteException. This is

    because these methods are not actually invoked remotely, they are invoked by the EJB object. If a communication

    failure

    occurs, the container will throw the RemoteException for the bean automatically.For every create() method defined in

    the home

    interface, there should be matching ejbCreate() and ejbPostCreate() methods defined in the bean class.The

    ejbPostCreate()

    method must have the same parameters as the corresponding Create() method, but returns void.

    --------------------------------------------------------------------

    QUESTION NO : 51

    QUESTION STATEMENT : Which of the following transaction attributes can be specified only for session beans and

    also cannot be

    41

  • 8/3/2019 7283499 Ejb Questions

    42/86

    specified for individual methods ?

    CHOICES :

    a) Bean Managed

    b) Mandatoryc) Supports

    d) Not Supported

    CORRECT ANSWER : a

    EXPLANATION :

    Choice A is correct. The transaction attribute defines the transactional manner in which the container invokes

    enterprise bean methods. If the transaction attribute is set to BeanManaged, it notifies the container that the bean class

    directly handles transaction demarcation. This attribute value can be specified only for session beans and it cannot be

    specified for individual bean methods.

    If the transaction attribute is set to Mandatory, it directs the container to always

    invoke the bean method within the transaction context associated with the client. If the transaction attribute is set to

    Supports, it directs the container to invoke the bean method within a transaction context if the client invokes the bean

    method within a transaction. If the transaction attribute is set to NotSupported, it directs the container to invoke bean

    methods without a transaction context.

    --------------------------------------------------------------------

    QUESTION NO : 52QUESTION STATEMENT : Which of the following statements about EJBs is NOT true ?

    CHOICES :

    a) A single entity bean instance can be shared by multiple clients.

    b) Session beans cannot be reentrant and they throw a RemoteException if loopback is attempted.

    c) A stateful bean instance is swapped among many EJB objects, it is not possible to predict which instance will service

    a

    method call.d) Session beans do not survive server crashes

    CORRECT ANSWER : c

    EXPLANATION :

    Choice C is Correct because it is not true. Entity beans represent data in the database that is shared and

    42

  • 8/3/2019 7283499 Ejb Questions

    43/86

    needs to be accessed concurrently. So many clients are allowed to use the same entity bean instance simultaneously.

    Reentrance is when a thread of control attempts to reenter a bean instance. In EJB, bean instances are reentrant by

    default

    which means that loopbacks are not allowed. Entity beans can be configured in the deployment descriptor to allowreentrance

    at deployment time.

    Session beans can never be reentrant, and they throw a RemoteException if a loopback is attempted.

    Option C is true for a stateless session bean, not for a stateful bean. Since a stateless session bean does not store a

    clients state, it does not matter which bean instance serves a particular client. But in the case of Stateful Beans, it is

    not possible to swap the same instance between different clients since a particular instance serves only one client. Since

    session beans are not persistent, they do not survive server crashes.

    --------------------------------------------------------------------

    QUESTION NO : 53

    QUESTION STATEMENT : Which of the following is true about comparing beans for identity ?

    CHOICES :

    a) The EJBObject.isIdentical method returns true if two EJB object references represent the same bean, even if the EJB

    object stubs are different object instances.b) For stateful session beans, if 2 EJB objects refer to the same type of bean, they are always considered identical.

    c) Even if two entity EJB objects have the same home and same primary key, they may not be identical.

    CORRECT ANSWER : a

    EXPLANATION :

    Choice A is Correct. An EJB object is a distributed object stub and therefore contains a lot of networking and

    other state. As a result, references to 2 EJB objects maybe unequal, even if they both represent the same unique bean.

    The

    EJBObject.isIdentical method returns true if two EJB object references represent the same bean, even if the EJB object

    stubs

    are different object instances.Since stateless beans do not retain the conversational state, they are considered identical if

    they are of the same type. But stateful session beans are not considered to be identical just because they are of the same

    type since they maintain the conversational state of the clients. For entiy beans, if they have the same home and primary

    43

  • 8/3/2019 7283499 Ejb Questions

    44/86

    key, they are considered to be identical.

    --------------------------------------------------------------------

    QUESTION NO : 54

    QUESTION STATEMENT : The Passivation mechanism of stateful session bean is different from that of entity bean.

    True/False?

    CHOICES :

    a) True

    b) False

    CORRECT ANSWER : a

    EXPLANATION :

    Yes, Passivation in stateful session beans is different than for entity beans. In stateful beans, passivation

    means the bean's conversational-state is written to a secondary storage (often disk) and the instance is removed frommemory.

    The client's reference to the bean is not affected by passivation, it remains alive and usable while the bean is passivated.

    When the client invokes a method on a bean that is passivated, the container will activate the bean by instantiating a

    new

    instance and populating its conversational-state with the state written to secondary s