Skip to main content

Java Interview Questions

What is the difference between calling run() method and call run() method through start() method???
Couldn't we call method run() ourselves, without doing double call: first we call start() method which calls run() method? What is a meaning to do things such complicate?
There is some very small but important difference between using start() and run() methods. Look at two examples below:
Example one:
Code:
Thread one = new Thread();
Thread two = new Thread();
one.run();
two.run();
Example two:
Code:
Thread one = new Thread();
Thread two = new Thread();
one.start();
two.start();
The result of running examples will be different.
In Example one the threads will run sequentially: first, thread number one runs, when it exits the thread number two starts.
In Example two both threads start and run simultaneously.
Conclusion: the start() method call run() method asynchronously (does not wait for any result, just fire up an action), while we run run() method synchronously - we wait when it quits and only then we can run the next line of our code.
Static Block and Static Initializer in Java
The static variables or static blocks in a class will be initialized before the class gets instantiated. Also, we all know that the static variables are not tied up with the instances. So even if we have the reference as null, the variable s value had already been initialized to "hello". So calling it that way will print the values Inside Method and Hello as the variable call will be made with the class name as they are tied up with the class. If you make the variable non - static, you will get a NPE.
Describe the lifecycle of a receiver application in order to receive a message?
Use the JNDI to get a ConnectionFactory and Destination Object (either Queue or Topic).Create a Connection object. Create a Session object. Create MessageConsumer object (either QueueReceiver or TopicSubscriber). Start the Connection. Receive the Message+. Close the session and Connection.
Messages are not successful until they have been acknowledged. What are the types of acknowledgments?
1) Acknowledgment by commit. “Acknowledgement happens automatically when a transaction is committed.”
2) Session.AUTO_ACKNOWLEDGE
3) Session.CLIENT_ACKNOWLEDGE “client must call the acknowledge( ) method.
4) Session.DUPS_OK_ACKNOWLEDGE “Session acknowledges the message after it has been delivered. This may result in the delivery of some duplicate messages if the JMS provider fails.
What happens to messages if a transaction is rolled back?
All consumed messages are re-delivered.
What is the Role of the JMS Provider?
The JMS provider handles security of the messages, data conversion and the client triggering. The JMS provider specifies the level of encryption and the security level of the message, the best data type for the non-JMS client.
What is JMS administered object?
A preconfigured JMS object (a resource manager connection factory or a destination) created by an administrator for the use of JMS clients and placed in a JNDI namespace.
What is JMS session?
A single-threaded context for sending and receiving JMS messages. A JMS session can be nontransacted, locally transacted, or participating in a distributed transaction.
What is JMS provider?
A messaging system that implements the Java Message Service as well as other administrative and control functionality needed in a full-featured messaging product.
What is JMS client?
A Java language program that sends or receives messages.
What is message producer?
An object created by a JMS session that is used for sending messages to a destination.
What is message-driven bean?
An enterprise bean that is an asynchronous message consumer. A message-driven bean has no state for a specific client, but its instance variables can contain state across the handling of client messages, including an open database connection and an object reference to an EJB object. A client accesses a message-driven bean by sending messages to the destination for which the bean is a message listener.
What is one-way messaging?
A method of transmitting messages without having to block until a response is received.
What is naming context?
A set of associations between unique, atomic, people-friendly identifiers and objects.
How does JNDI relate to OMG's CORBA standards for naming?
A Java CORBA application can use JNDI to access to the CORBA (COS) name service, as well as other naming and directory services. It offers the application one interface for accessing all these naming and directory services. Using JNDI also paves the way for Java CORBA applications to use a distributed enterprise-level service like LDAP to store object references.
What protocols does JNDI provide an interface to?
JNDI itself is independent of any specific directory access protocol. Individual service providers determine the protocols to support. There will be provider implementations for popular protocols, such as LDAP, NDS, DNS, and NIS(YP), supplied by different vendors.
Who will provide implementations of JNDI?
At the time of this writing, IBM, Novell, Sun, and WebLogic have produced service providers for JNDI. We maintain a listing of publicly available service providers.
Does Sun have a LDIF parser?
No, as of now Sun does not have a LDIF parser. The only open source LDIF parser currently is from Netscape. The source is available from mozilla.org.
What's the format of an LDAP URL?
RFC 2255 defines the basic structure of the LDAP URL. Its basic definition is:
"ldap://" [host [":" port] ] ["/" [dn ["?" [attributeList] ["?" [scope] ["?" [filterString] ["?" extensions]]]]]
host and port specify where the LDAP server is located
dn is the LDAP Distinguished Name to search for
attributeList is a comma-delimited list of what to retrieve
scope is either base, one, or sub [base is the default if unspecified]
filterSearch is the search filter
extensions is a comma-delimited list of extensions
Is there a connection pooling mechanism in JNDI?
JNDI does not provide any connection pooling mechanism. You have to build it yourself if you need one. Netscape's Java SDK for LDAP has a connection pooling mechanism. You could see the way it is implemented (its open source) and build a similar one of your own for JNDI.
How do I set up access control for a number of applications using the same LDAP server?
There are 2 scenarios:
1. All the applications use the same access control.
2. Different applications allow different levels of access to the same user.
In either case, setting the ACL within the server is a very good approach. Only the way you set up the ACL will be different. In the first case, you can specify what operations a user can perform on a particular object. An application accessing the directory can authenticate itself using the user's authentication info. And it can provide access based on the ACL that you have set up. In the second case, you can define a set of special users. Each application connects to the directory as a user from this set of special users. The ACL that you set up on the directory will involve these users.
Information on LDAP security is available in the IBM Redbook, "LDAP Implementation Cookbook". You can download it from www.redbooks.ibm.com
When I try to modify an attribute, why do I get a SchemaViolationException?
Two reasons - You are setting the attribute with the wrong data type. For example, an attribute of type 'Distinguished Name' is being assigned an integer value. Second, you are trying to modify the RDN of the entry. Modifying the RDN of a directory entry is equivalent to renaming the entry. Context.rename() should be used for that.
What's the relationship between LDAP and JNDI?
JNDI has classes provided by Sun that will help your application interact with an LDAP server. JNDI applications work similarly to JDBC applications - write applications once and be free to use "drivers" from different vendors. Sun provides the "driver" that will help interact with the LDAP server. Sun also provides "drivers" for other naming services (like CORBA, Netscape). What Sun has not written is a driver to interact with ADSI.
What do I need to download to start using JNDI?
Download the standard Java extension package for JNDI. This will include the jndi.jar and providerutil.jar files. If you want to access an LDAP server, you also need to download the LDAP service provider interface. You can download Sun's LDAP SPI.
Java Naming and Directory Interface (JNDI)
JNDI stands for Java Naming and Directory Interface. It comes standard with J2EE. With this API, you can access many types of data, like objects, devices, files of naming and directory services, eg. it is used by EJB to find remote objects. JNDI is designed to provide a common interface to access existing services like DNS, NDS, LDAP, CORBA and RMI.
What are the call back methods in Session bean?
Callback methods are called by the container to notify the important events to the beans in its life cycle. The callback methods are defined in the javax.ejb.EntityBean interface.The callback methods example are ejbCreate(), ejbPassivate(), and ejbActivate().
Can Entity Beans have no create() methods?
Entity Beans can have no create() methods. Entity Beans have no create() method, when entity bean is not used to store the data in the database. In this case entity bean is used to retrieve the data from database.

Comments

Popular posts from this blog

Asynchronous Vs. Synchronous Communications

Synchronous (One thread):   1 thread -> |<---A---->||<----B---------->||<------C----->| Synchronous (multi-threaded):   thread A -> |<---A---->| \ thread B ------------> ->|<----B---------->| \ thread C ----------------------------------> ->|<------C----->|

WebSphere MQ Interview Questions

What is MQ and what does it do? Ans. MQ stands for MESSAGE QUEUEING. WebSphere MQ allows application programs to use message queuing to participate in message-driven processing. Application programs can communicate across different platforms by using the appropriate message queuing software products. What is Message driven process? Ans . When messages arrive on a queue, they can automatically start an application using triggering. If necessary, the applications can be stopped when the message (or messages) have been processed. What are advantages of the MQ? Ans. 1. Integration. 2. Asynchrony 3. Assured Delivery 4. Scalability. How does it support the Integration? Ans. Because the MQ is independent of the Operating System you use i.e. it may be Windows, Solaris,AIX.It is independent of the protocol (i.e. TCP/IP, LU6.2, SNA, NetBIOS, UDP).It is not required that both the sender and receiver should be running on the same platform What is Asynchrony? Ans. With messag...

Advantages & Disadvantages of Synchronous / Asynchronous Communications?

  Asynchronous Communication Advantages: Requests need not be targeted to specific server. Service need not be available when request is made. No blocking, so resources could be freed.  Could use connectionless protocol Disadvantages: Response times are unpredictable. Error handling usually more complex.  Usually requires connection-oriented protocol.  Harder to design apps Synchronous Communication Advantages: Easy to program Outcome is known immediately  Error recovery easier (usually)  Better real-time response (usually) Disadvantages: Service must be up and ready. Requestor blocks, held resources are “tied up”.  Usually requires connection-oriented protocol