O'Reilly logo

Head First EJB by Bert Bates, Kathy Sierra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Coffee Cram: Mock Exam Answers

  1. Which statements about session beans are true? (Choose all that apply.)

    (spec: 71)

    A.

    A stateful session bean is typically used to represent a row in a database

    -that’s what entity beans do

    B.

    The client must call the ejbActivate method before calling any business methods.

    -only the container calls ejbActivate()

    C.

    A stateful session bean’s fields can contain client conversational state.

    D.

    They are typically used as asynchronous message consumers.

  2. Which list(s) correctly sequence some of the steps in a session bean’s lifecycle? (Choose all that apply.)

    (spec: 77)

    A.

    ejbCreate(), newInstance(), setSessionContext()

    B.

    newInstance(), setSessionContext(), ejbCreate()

    C.

    ejbCreate(), setSessionContext(), newInstance()

    D.

    newInstance(), ejbCreate(), setSessionContext().

    E.

    setSessionContext(), newInstance(), ejbCreate()

  3. Which types of session bean instance fields can be successfully passivated and reactivated? (Choose all that apply.)

    (spec: 71)

    A.

    A reference to the remote home interface.

    B.

    A reference to the UserTransaction interface

    C.

    A reference to a database cursor.

    - remember, passivation could be serialization

    D.

    A reference to the SessionContext object.

    E.

    A reference to a socket.

    - remember, passivation could be serialization

  4. Which are valid remote component interfaces for a session bean? (Choose all that apply.)

    (spec: 97)

    A.

    public interface MyBean extends javax.ejb.EJBObject {
      void myMethod();
    }

    - needs RemoteException

    B.

    public interface MyBean extends javax.ejb.EJBObject
    throws RemoteException {
       void myMethod();
    }

    -illegal Java !

    C.

    public interface MyBean extends
    javax.ejb.EJBHomeObject {
       void myMethod() throws RemoteException;
    }

    - nope, gotta be EJBObject

    D.

    public interface MyBean extends javax.ejb.EJBObject {
       void myMethod() throws RemoteException;
    }
  5. If a client makes a call to a session object that has been removed by the container, which exceptions can be thrown? (Choose all that apply.)

    (spec: 79)

    A.

    java.rmi.RemoteException

    B.

    javax.ejb.RemoveException

    - this happens only on remove()

    C.

    java.rmi.NoSuchObjectException

    D.

    javax.ejb.NoSuchEntityException

    - clients don’t see this

    E.

    javax.ejb.ObjectNotFoundException

    - this is for entity finders

  6. Given:

    (spec: 98)

    MyBean create(String name) throws CreateException,
    RemoteException;

    Which session bean interface can contain this method?

    A.

    Only stateful session beans

    B.

    Only stateless session beans

    C.

    Both stateful and stateless session beans

    D.

    Neither stateful nor stateless session beans

    Stateless beans must have only a no-argument create

  7. What is true about a session bean’s lifecycle? (Choose all that apply.)

    (spec: 79, 88)

    A.

    If a business method throws a system exception the bean will be passivated.

    - no, the bean is killed

    B.

    A passivated bean must be activated before it can be removed.

    - nope

    C.

    A stateless session bean’s removal must be initiated by the client not the container.

    D.

    Stateless session beans cannot implement the SessionSynchronization interface.

  8. Which are required of the bean provider to ensure that a session bean is successfully passivated? (Choose all that apply.)

    (spec: 71-72)

    A.

    The provider must call ejbPassivate().

    B.

    The provider must always add business logic to the ejbPassivate() method, if the bean is stateful.

    C.

    The provider must not close any database connections before ejbPassivate()completes.

    D.

    The provider must assume that any state stored in instance fields marked transient will be lost.

    E.

    The provider must assume that any reference to the SessionContext object will not survive passivation if the SessionContext object is not serializable.

    Container MUST passivate your context, no matter what!

  9. Given a stateless session bean with container-managed transaction demarcation, from which method(s) can you access another bean? (Choose all that apply.)

    (spec: 90)

    A.

    ejbCreate()

    B.

    ejbRemove()

    C.

    a business method

    - also true for stateless BMT beans

    D.

    setSessionContext()

  10. (Note: The real exam has several types of ‘drag and drop’ questions, that we’re going to do a lame job of simulating with this question...)

    (API docs)

    Match the methods on the left with the interfaces in which those methods can be found, on the right. A match is correct if the method is either declared in, or inherited by, the interface. Note: There may be some many-to-one and one-to-many relationships in your answer.

    A.

    afterCompletion

    1

    B.

    getUserTransaction

    2

    C.

    afterBegin

    1

    D.

    isCallerInRole

    2

    E.

    getRollBackOnly

    2

    F.

    setSessionContext

    3

    G.

    setRollbackOnly

    2,4

    1. SessionSynchronization

    2. SessionContext

    3. SessionBean

    4. UserTransaction

  11. What is true about a session bean’s lifecycle? (Choose all that apply.)

    (spec: 70)

    A.

    The container will always create a new session bean instance when the client invokes the create() method on the home interface of a stateless bean.

    - Container makes stateless beans whenever it wants

    B.

    The container will always call ejbRemove() when the client invokes the remove() method on a stateless bean’s home interface.

    - Container removes a stateless bean ONLY when it wants to shrink the pool

    C.

    A session bean cannot be passivated while it is in a transaction.

    D.

    The setSessionContext() method is not invoked during a stateless bean’s lifecycle

  12. Which of the following methods are container callback methods? (Choose all that apply.)

    (spec: 73)

    A.

    ejbPassivate()

    B.

    setRollbackOnly()

    C.

    setSessionContext()

    D.

    getRollbackOnly()

    E.

    getUserTransaction()

  13. In what case(s) will the container fail to call ejbRemove() on a session bean? (Choose all that apply.)

    (spec: 82)

    A.

    If the container crashes.

    B.

    If an application exception is thrown from a business method.

    C.

    If a timeout occurs while the bean is in the method ready state.

    D.

    If an application exception is thrown from within a transaction.

  14. Which method(s) allow both stateful and stateless session beans with bean-managed transaction demarcation to access UserTransaction methods? (Choose all that apply.)

    (spec: 80, 90)

    A.

    ejbCreate()

    B.

    ejbRemove()

    - for stateful only

    C.

    a business method

    D.

    setSessionContext()

  15. For which type of bean can the container passivate an instance that is in a transaction?

    (spec: 70)

    A.

    only stateful session beans

    B.

    only stateless session beans

    C.

    both stateful and stateless session beans

    D.

    neither stateful nor stateless session beans

  16. For session beans, which are the responsibility of the Container? (Choose all that apply.)

    A.

    Invoking the local interface create() method.

    - client calls it

    B.

    Invoking the getSessionContext() method.

    - Container calls setSessionContext

    C.

    Invoking the ejbCreate() method.

    D.

    Ensuring that the ejbRemove() method is always invoked.

    - might be missed if passivated bean times out, or bean throws a runtime exception

  17. For this drag and drop type question, you can use each element only once. Which interface should be matched with which fact, so that all four matches are correct?

    1.

    remote component

    b

    2.

    remote home

    a

    3.

    local component

    c

    4.

    local home

    d

    1. Has a getHomeHandle() method

    2. extends 'javax.ejb.EJBObject'

    3. methods must NOT throw 'java.rmi.RemoteException'

    4. can be used to retrieve an EJBLocalObject

  18. Which can be called by the container during the lifecycle of a session bean? (Choose all that apply.)

    (spec: 90, 97)

    A.

    create()

    - client calls

    B.

    bean constructor

    C.

    setRollbackOnly()

    D.

    getUserTransaction()

    - bean calls these

    E.

    ejbRemove()

  19. Which statements about a session bean class are true? (Choose all that apply.)

    (spec: 95)

    A.

    They can be marked ‘final’

    B.

    They do not need a no-argument constructor.

    C.

    Their component interface methods can be ‘private’.

    D.

    Their business method names must start with “ejb”.

    E.

    Their ‘ejbCreate’ methods must not be declared as ‘final’.

  20. For which type of bean can the SessionSynchronization interface be implemented?

    A.

    only stateful session beans

    B.

    only stateless session beans

    C.

    both stateful and stateless session beans

    D.

    neither stateful nor stateless session beans

  21. Which statements are true for stateless session bean instances? (Choose all that apply.)

    (spec: 88)

    A.

    Any instance can be used for any client.

    B.

    Conversational state must be retained across methods

    C.

    Conversational state must be retained across transactions.

    D.

    They can be passivated.

    E.

    They do not support transactions.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required