1Z0-895 | The Secret of Oracle 1Z0-895 exam question


Q31. While excepting a business method in a stateless session bean the container rolls back the method’s transaction. Which three are possible causes for the container’s behavior? (Choose three.) 

A. The bean uses container-managed transactions and invokes EJBContext.setRollbackOnly. 

B. The bean uses container-managed transactions and invokes EJBContext.getRollbackOnly. 

C. The business method throws a java.lang.NullPointerException. 

D. The business method throws a checked exception of a class type that is marked with the @ApplicationException annotation with the rollback element value true. 

E. The business method throws a unchecked exception of a class type that is marked with the @ApplicationException annotation with the rollback element value true. 

F. The bean uses container-managed transactions and throws a checked exception of a class type that is marked with the @ApplicationException annotation with the rollback element value false. 

Answer: ADF 

Explanation: A: setRollbackOnly 

Mark the current transaction for rollback. The transaction will become permanently marked for rollback. A transaction marked for rollback can never commit. Only enterprise beans with container-managed transactions are allowed to use this method. 

Note: 

* In a stateless session bean with bean-managed transactions, a business method must commit or roll back a transaction before returning. 

* Bean-Managed Transactions In bean-managed transaction demarcation, the code in the session or message-driven bean explicitly marks the boundaries of the transaction. Although beans with container-managed transactions require less coding, they have one limitation: When a method is executing, it can be associated with either a single transaction or no transaction at all. If this limitation will make coding your bean difficult, you should consider using bean-managed transactions. 

* (incorrect) Unchecked runtime exceptions represent conditions that, generally speaking, reflect errors in your program's logic and cannot be reasonably recovered from at run time. 

Incorrect: 

B: getRollbackOnly 

Test if the transaction has been marked for rollback only. An enterprise bean instance can use this operation, for example, to test after an exception has been caught, whether it is fruitless to continue computation on behalf of the current transaction. Only enterprise beans with container-managed transactions are allowed to use this method. 

Q32. You have been tasked to build a jar file that can be used by a Java SE client to access the remote instance of the OrderProcessingBean. Given the following design: 

Which classes would need to be included in the client jar file? 

A. B, Order 

B. A, Order 

C. A, B, Order 

D. A, B, Order, OrderProcessingBean 

Answer:

Explanation: 

Note: 

* An EJB client JAR file is an optional JAR file that can contain all the class files that a client program needs to use the client view of the enterprise beans that are contained in the EJB JAR file. 

* If all your EJBs are in the same EAR then you can use local interfaces, if not you need remote interfaces. 

Q33. Which two statements are correct about stateless session beans? (Choose two.) 

A. The bean class may declare instance variables. 

B. The lifetime of the bean instance is controlled by the client. 

C. The container may use the same bean instance to handle multiple business method invocations at the same time. 

D. The container may use the same bean instance to handle business method invocations requested by different clients, but not concurrently. 

Answer: AC 

Explanation: * A: Stateless session beans are EJB's version of the traditional transaction processing applications, which are executed using a procedure call. The procedure executes from beginning to end and then returns the result. Once the procedure is done, nothing about the data that was manipulated or the details of the request are remembered. There is no state. 

These restrictions don't mean that a stateless session bean can't have instance variables and therefore some kind of internal state. There's nothing that prevents you from keeping a variable that tracks the number of times a bean has been called or that tracks data for debugging. An instance variable can even hold a reference to a live resource like a URL connection for writing debugging data, verifying credit cards, or anything else that might be useful. 

C: A stateless session bean is relatively easy to develop and also very efficient. Stateless session beans require few server resources because they are neither persistent nor dedicated to one client. Because they aren't dedicated to one client, many EJB objects can use just a few instances of a stateless bean. A stateless session bean does not maintain.conversational state relative to the EJB object it is servicing, so it can be swapped freely between EJB objects. As soon as a stateless instance services a method invocation, it can be swapped to another EJB object immediately. Because there is no conversational state, a stateless session bean doesn't require passivation or activation, further reducing the overhead of swapping. In short, they are lightweight and fast! 

* The Lifecycle of a Stateless Session Bean Because a stateless session bean is never passivated, its lifecycle has only two stages: nonexistent and ready for the invocation of business methods. The EJB container typically creates and maintains a pool of stateless session beans, beginning the stateless session bean’s lifecycle. The container performs any dependency injection and then invokes the method annotated @PostConstruct, if it exists. The bean is now ready to have its business methods invoked by a client. 

At the end of the lifecycle, the EJB container calls the method annotated @PreDestroy, if it exists (not B). The bean’s instance is then ready for garbage collection. 

Q34. You are writing a client that sends a message to a JMS queue. Which statement is true? 

A. You use a connection factory to create a session. 

B. When you create a session, you specify whether or not it is transacted. 

C. When you create a connection, you specify the acknowledgment mode. 

D. When you create a message producer, you must specify the name of the destination to which you will send messages. 

Answer:

Explanation: Note: 

The SimpleMessageClient sends messages to the queue that the SimpleMessageBean listens to. 

The client starts by injecting the connection factory and queue resources: 

@Resource(mappedName="jms/ConnectionFactory") 

private static ConnectionFactory connectionFactory; 

@Resource(mappedName="jms/Queue") 

private static Queue queue; 

Next, the client creates the connection, session, and message producer: 

connection = connectionFactory.createConnection(); 

session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 

messageProducer = session.createProducer(queue); 

Finally, the client sends several messages to the queue: 

message = session.createTextMessage(); 

for (int i = 0; i < NUM_MSGS; i++) { 

message.setText("This is message " + (i + 1)); 

System.out.println("Sending message: " + message.getText()); 

messageProducer.send(message); 

Q35. A developer is writing client code to access a session bean deployed to a server instance. 

The client can access the session bean under which of these circumstances? (Choose three) 

A. The client is deployed in the same JVM as the session bean and the session bean has a local interface. 

B. The client is deployed in the same JVM as the session bean and the session bean exposes a no-interface view. 

C. The client is deployed in a different JVM from the session bean and the session bean has a local interface. 

D. The client is deployed in a different JVM from the session bean and the session bean has a remote interface. 

E. The client is deployed in a different JVM from the session bean and the session bean has a no-interface implementation. 

Answer: ABD 

Explanation: D: If your architecture has a requirement whereby the client application (web application or rich client) has to run on a different JavaVirtual Machine (JVM) from the one that is used to run the session beans in an EJB container, then you need to use the remote interface. 

Q36. A developer writes a stateful session bean called FooBean. 

Which code can be inserted before Line 11 of the FooBean class to define a TYPE-level environment dependency on a JMS Topic? 

11. public class FooBean { 

12. 

13. public void foo() () 

14. 

15. } 

A. @Resource (type=Topic.class) 

B. @Resource (name=“topicRef”) Private static Topic topic; 

C. @Resource private Topic topic 

D. @Resource(name=“topicRef”, type=Topic.class) 

Answer:

Explanation: @Resource can decorate a class, a field or a method at runtime/init through injection. (name, type, authenticationType, shareable, mappedName, description) 

Type- level env dependency on a Topic - @Resource(name=”topicRef”, type=Topic.class) 

Q37. Given a session bean defines: 

11. @Stateless (name = “MrBean”) 

12. public class source SecureBean implements local business, remoteBusiness { 

Where LocalBusiness is a local business interface and RemoteBusines is a remote business interface. 

The following deployment descriptor entries represent the only security-related metadata: 

Which is true about the security roles of clients MrBean? 

A. Only LocalBusiness clients must be in role A. 

B. Only LocalBusiness clients must be in role B. 

C. Both LocalBusiness and RemoteBusiness Clients must be in role A. 

D. Both LocalBusiness and RemoteBusiness clients must NOT be in role A. 

Answer:

Q38. Suppose an EJB named HelloWorldBean is deployed as a standalone ejb-jar. Assuming the HelloWorldBean is implemented as follows: Which HelloWorldBean methods are accessible by another EJB within the same ejb-jar? 

A. All of the methods declared in HelloWorldBean 

B. All of the methods declared in HelloWorld and HelloWorldBean 

C. All of the public methods declared in HelloWorldBean 

D. All of the public methods declared in HelloWorld and all of the methods declared in HelloWorldBean 

Answer:

Q39. Which two are true about the client view of a message-driven bean? (Choose two.) 

A. References to message destinations can be infected. 

B. References to message destinations cannot be looked up in the client's JNDI namespace. 

C. Clients of a message destination need to know that the destination is listened to by a pool of message consumers, 

D. Clients of a message destination do NOT need to know that the destination is listened to by a message-driven bean. 

Answer: BC 

Explanation: Client components do not locate message-driven beans and invoke methods directly on them. Instead, a client accesses a message-driven bean through, for example, JMS by sending messages to the message destination for which the message-driven bean class is the MessageListener. 

Reference: The Java EE 6 Tutorial, What is a Message-Driven Bean? 

Q40. Given the following stateless session bean: How would you change the EJB to prevent multiple clients from simultaneously accessing the sayHello method of a single bean instance? 

A. Convert sayHello into a synchronized method 

B. Execute the call to generateLocalizedHello in a synchronized block 

C. Convert generateLocalizehello into a synchronized method 

D. Convert HelloWordBean into a singleton bean 

E. No changes are needed 

Answer:

Explanation: * It is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object. 

* When a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads. 

Reference: The Java Tutorial, Synchronized Methods