Beruflich Dokumente
Kultur Dokumente
2) What is a Class?
Class is a template for a set of objects that share a common structure and a common
behavior.
3) What is an Object?
Object is an instance of a class. It has state,behaviour and identity. It is also called as an
instance of a class.
4) What is an Instance?
An instance has state, behavior and identity. The structure and behavior of similar classes
are defined in their common class. An instance is also called as an object.
6) What are the two types of Set implementations available in the Collections Framework?
HashSet and TreeSet are the two Set implementations available in the Collections
Framework.
8) What is a List?
List is a ordered and non duplicated collection of objects. The List interface extends the
Collection interface.
9) What are the two types of List implementations available in the Collections Framework?
ArrayList and LinkedList are the two List implementations available in the Collections
Framework.
The LinkedList Class implements java.util.List interface and uses linked list for storage.A linked
list allow elements to be added, removed from the collection at any location in the container
by ordering the elements.With this implementation you can only access the elements in
sequentially.
14) What are the two types of Map implementations available in the Collections
Framework?
HashMap and TreeMap are two types of Map implementations available in the Collections
Framework.
18) How do you store a primitive data type within a Vector or other collections class?
You need to wrap the primitive data type into one of the wrapper classes found in the
java.lang package, like Integer, Float, or Double, as in:
Integer in = new Integer(5);
22) Which is faster, synchronizing a HashMap or using a Hashtable for thread-safe access?
Because a synchronized HashMap requires an extra method call, a Hashtable is faster for
synchronized access.
23) In which package would you find the interfaces amd claases defined in the Java
Collection Framework?
java.util
24) What method in the System class allows you to copy elements from one array to
another?
System. arraycopy()
25) When using the System.arraycopy() method,What exception is thrown if the destination
array is smaller than the souce array?
ArrayIndexOutofBoundsException
1) What is an Applet?
Applet is a java program which is included in a html page and executes in java enabled
client browser. Applets are used for creating dynamic and interactive web applications.
5) What is AppletContext?
AppletContext is an interface which provides information about applet's environment.
9) Can a applet invoke public methods of another applet on the same page?
Yes. It's possible.
11) How to let a downloaded applet to read a file in the local system?
To allow any files in the directory /home/user1 to be read by applets loaded into the
appletviewer, add the following line to your <user home directory>/.hotjava/properties file.
acl.read=/home/user1
You can specify one file to be read:
acl.read=/home/user1/somedir/somefile
12) How to let a downloaded applet to write to a file in the local system?
You can allow applets to write to your /tmp directory by setting the acl.write property in
your <user home directory>/.hotjava/properties file:
acl.write=/tmp
You can allow applets to write to a particular file by naming it explicitly:
acl.write=/home/user1/somedir/somefile
13) How do I hide system properties that applets are allowed to read?
To hide the name of the operating system that you are using, add the following line to your
<user home directory>/.hotjava/properties file:
os.name=null
14) How can I allow applets to read system properties that they aren't allowed to read?
To allow applets to record your user name, add the following line to your <user home
directory>/.hotjava/properties file:
user.name.applet=true
15) How can an applet open a network connection to a computer on the internet?
Applets are not allowed to open network connections to any computer,except for the host
that provided the .class files. This is either the host where the html page came from, or the
host specified in the codebase parameter in the applet tag, with codebase taking
precendence.
17) What is the difference between applets loaded over the net and applets loaded via the
file system?
There are two different ways that applets are loaded by a Java system. The way an applet
enters the system affects what it is allowed to do. If an applet is loaded over the net, then it is
loaded by the applet classloader, and is subject to the restrictions enforced by the applet
security manager. If an applet resides on the client's local disk, and in a directory that is on
the client's CLASSPATH, then it is loaded by the file system loader. The most important
differences are :
a. applets loaded via the file system are allowed to read and write files
b. applets loaded via the file system are allowed to load libraries on the client
c. applets loaded via the file system are allowed to exec processes
d. applets loaded via the file system are allowed to exit the virtual machine
e. applets loaded via the file system are not passed through the byte code verifier
18) What's the applet class loader, and what does it provide?
Applets loaded over the net are loaded by the applet class loader. For example, the
appletviewer's applet class loader is implemented by the class sun.applet.AppletClassLoader.
The class loader enforces the Java name space hierarchy. The class loader guarantees that a
unique namespace exists for classes that come from the local file system, and that a unique
namespace exists for each network source. When a browser loads an applet over the net,
that applet's classes are placed in a private namespace associated with the applet's origin.
Thus, applets loaded from different network sources are partitioned from each other. Also,
classes loaded by the class loader are passed through the verifier.The verifier checks that the
class file conforms to the Java language specification - it doesn't assume that the class file was
produced by a "friendly" or "trusted" compiler. On the contrary, it checks the class file for
purposeful violations of the language type rules and name space restrictions. The verifier
ensures that :
a. There are no stack overflows or underflows.
b. All register accesses and stores are valid.
c. The parameters to all bytecode instructions are correct.
d. There is no illegal data conversion.
e. The verifier accomplishes that by doing a data-flow analysis of the bytecode instruction
stream, along with checking the class file format, object signatures, and special analysis of
finally clauses that are used for Java exception handling.
19) What's the applet security manager, and what does it provide?
The applet security manager is the Java mechanism for enforcing the applet restrictions
described above. The appletviewer's applet security manager is implemented by
sun.applet.AppletSecurity.
A browser may only have one security manager. The security manager is established at
startup, and it cannot thereafter be replaced, overloaded, overridden, or extended. Applets
cannot create or reference their own security manager.
20) If other languages are compiled to Java bytecodes, how does that affect the applet
security model?
The verifier is independent of Sun's reference implementation of the Java compiler and the
high-level specification of the Java language. It verifies bytecodes generated by other Java
compilers. It also verifies bytecodes generated by compiling other languages into the
bytecode format. Bytecodes imported over the net that pass the verifier can be trusted to run
on the Java virtual machine. In order to pass the verifier, bytecodes have to conform to the
strict typing, the object signatures, the class file format, and the predictability of the runtime
stack that are all defined by the Java language implementation.
JAVA LANG PACKAGE INTERVIEW QUESTIONS AND ANSWERS
2) What do you think is the logic behind having a single base class for all classes?
1. casting
2. Hierarchial and object oriented structure.
4) What is the importance of == and equals() method with respect to String object?
== is used to check whether the references are of the same object.
.equals() is used to check whether the contents of the objects are the same.
But with respect to strings, object refernce with same content
will refer to the same object.
String str1="Hello";
String str2="Hello";
If you take the same example with Stringbuffer, the results would be different.
Stringbuffer str1="Hello";
Stringbuffer str2="Hello";
7) How many objects are in the memory after the exection of following code segment?
String str1 = "ABC";
String str2 = "XYZ";
String str1 = str1 + str2;
There are 3 Objects.
8) What is the difference between an object and object reference?
An object is an instance of a class. Object reference is a pointer to the object. There can be
many refernces to the same object.
1) What is RMI?
Remote Method Invocation (RMI) is the process of activating a method on a remotely running
object. RMI offers location transparency in the sense that it gives the feel that a method is
executed on a locally running object.
c. Transport layer
This layer is based on TCP/IP connections between machines in a network. It provides basic
connectivity, as well as some firewall penetration strategies.
8) What is the difference between using bind() and rebind() methods of Naming Class?
bind method(String name) binds the specified name to a remote object while rebind(String
name) method rebinds the specified name to a new remote object,any existing binding for
the name is replaced.
11) What are the steps involved to make work a RMI program?
Ans: a. Compile the source files.
b. Generate the stubs using rmic.
c. Start the rmiregistry.
d. Start the RMIServer.
e. Run the client program.
16) What is the difference between Pass By Value and Pass By Reference?
Ans: When an Object is "Passed by Value", it means a copy of the object is passed. So even if
changes are made to that Object/datatype, it doesn't affect the original value.
When an Object is "Passed by Refernce", it means the object is not passed but a reference of
the object is passed. so any changes made in the external method gets reflected in all places.
21) Can a class implementing a Remote interface have non remote methods?
Yes. Those methods behave as normal java methods operating within the JVM.
26) In a RMI Client Program, what are the excpetions which might have to handled?
a. MalFormedURLException
b. NotBoundException
c. RemoteException
SERVLETS INTERVIEW QUESTIONS AND ANSWERS
1) What is a Servlet?
A Servlet is a server side java program which processes client requests and generates dynamic web content.
doPOST Method : In this method we does not have any size limitation. All data passed to server will be hidden,
User cannot able to see this info on the browser.
getSession(false) method will check for the existence of a session. If a session exists, then it will return the
reference of that session object, if not, it will return null.
12) How do you find out what client machine is making a request to your servlet?
The ServletRequest class has functions for finding out the IP address or host name of the client machine.
getRemoteAddr() gets the IP address of the client machine and getRemoteHost()gets the host name of the client
machine.
18) How can a servlet refresh automatically if some new data has entered the database?
You can use a client-side Refresh or Server Push
19) What is the Max amount of information that can be saved in a Session Object?
As such there is no limit on the amount of information that can be saved in a Session Object. Only the RAM
available on the server machine is the limitation. The only limit is the Session ID length(Identifier) , which should
not exceed more than 4K. If the data to be store is very huge, then it's preferred to save it to a temporary file onto
hard disk, rather than saving it in session. Internally if the amount of data being saved in Session exceeds the
predefined limit, most of the servers write it to a temporary cache on hard disk.
22) Is there some sort of event that happens when a session object gets bound or unbound to the session?
HttpSessionBindingListener will hear the events When an object is added and/or remove from the session object,
or when the session is invalidated, in which case the objects are first removed from the session, whether the
session is invalidated manually or automatically (timeout).
23) Is it true that servlet containers service each request by creating a new thread? If that is true, how does a
container handle a sudden dramatic surge in incoming requests without significant performance degradation?
The implementation depends on the Servlet engine. For each request generally, a new Thread is created. But to
give performance boost, most containers, create and maintain a thread pool at the server startup time. To service a
request, they simply borrow a thread from the pool and when they are done, return it to the pool. For this thread
pool, upper bound and lower bound is maintained. Upper bound prevents the resource exhaustion problem
associated with unlimited thread allocation. The lower bound can instruct the pool not to keep too many idle
threads, freeing them if needed.
26) Do objects stored in a HTTP Session need to be serializable? Or can it store any object?
Yes, the objects need to be serializable, but only if your servlet container supports persistent sessions. Most
lightweight servlet engines (like Tomcat) do not support this. However, many EJB-enabled servlet engines do. Even
if your engine does support persistent sessions, it is usually possible to disable this feature.
b. JSP pages are compiled dynamically into servlets when requested, so page authors can easily make updates to
presentation code. JSP pages can also be precompiled if desired.
c. JSP tags for invoking JavaBeans components manage these components completely, shielding the page author from the
complexity of application logic.
d. Developers can offer customized JSP tag libraries that page authors access using an XML-like syntax.
e. Web authors can change and edit the fixed template portions of pages without affecting the application logic. Similarly,
developers can make logic changes at the component level without editing the individual pages that use the logic.
forward is an internal redirection of user request within the web container to a new URL without the knowledge of the
user(browser). The request object and the http headers remain intact.
sendRedirect is normally an external redirection of user request outside the web container. sendRedirect sends response
header back to the browser with the new URL. The browser send the request to the new URL with fresh http headers.
sendRedirect is slower than forward because it involves extra server call.
19) How is Java Server Pages different from Active Server Pages?
JSP is a community driven specification whereas ASP is a similar proprietary technology from Microsoft. In JSP, the dynamic
part is written in Java, not Visual Basic or other MS-specific language. JSP is portable to other operating systems and non-
Microsoft Web servers whereas it is not possible with ASP
20) Can't Javascript be used to generate dynamic content rather than JSP?
JavaScript can be used to generate dynamic content on the client browser. But it can handle only handles situations where
the dynamic information is based on the client's environment. It will not able to harness server side information directly.
EJB INTERVIEW QUESTIONS AND ANSWERS
3) How many types of Enterprise beans are there and what are they?
There are 3 types EJB's and they are:
1. Entity Bean's
2. Session Bean's
3. Message Driven Bean's(MDB's)
4) How many types of Entity beans are there and what are they?
There are 2 types Entity bean's and they are:
1. Container Managed Persistence(CMP) Entity Bean's
2. Bean Managed Persistence(BMP) Entity Bean's
5) How many types of Session beans are there and what are they?
There are 2 types Session bean's and they are:
1. Statefull Session Bean's
2. Stateless Session Bean's
6) How many types of MDB's are there and what are they?
There are no different kinds of Message driven beans.
7) How many java files should a developer code to develop a session bean?
3 java files has to be provided by the developer. They are:
1) an Home Interface
2) a Remote Interface
3) And a Session Bean implementation class.
10) What is the need for a separate Home interface and Remote Interface. Can't they be
defined in one interface?
EJB doesn't allow the client to directly communicate with an enterprise bean. The client has
to use home and remote interfaces for any communication with the bean.
The Home Interface is for communicating with the container for bean's life cycle operations
like creating, locating,removing one or more beans. While the remote interface is used for
remotely accessing the business methods.
16) Why are ejbActivate() and ejbPassivate() included for stateless session bean even
though they are never required as it is a nonconversational bean?
To have a consistent interface, so that there is no different interface that you need to
implement for Stateful Session Bean and Stateless Session Bean. Both Stateless and Stateful
Session Bean implement javax.ejb.SessionBean and this would not be possible if stateless
session bean is to remove ejbActivate and ejbPassivate from the interface.
22) What are the new features that were introduced in EJB version 2.0 specification?
1. New CMP Model. It is based on a new contract called the abstract persistence schema,
that will allow to the container to handle the persistence automatically at runtime.
2. EJB Query Language. It is a sql-based language that will allow the new persistence schema
to implement and execute finder methods.
3. Message Driven Beans. It is a new bean type, that is introduced to handle asynchronous
messaging.
4. Local interfaces. Enabales beans in the same EJB Container to communicate directly.
5. ejbHome methods. Entity beans can declare ejbHome methods that perform operations
related to the EJB component but that are not specific to a bean instance.
23) What are the difference's between a Local Interface and a Remote Interface?
Local Interfaces are new mechanism introduced in EJB 2.0 specification which enables
components in the same container to bypass RMI and call each other's methods directly. In
general, direct local method calls are faster than remote method calls. The downside is a loss
of flexibility: because bean and client must run in the same container, the location of the
bean is not transparent to the client (as it is with remote interfaces). remote interfaces pass
parameters by value, while local interfaces pass them by reference.
28) Where will you mention whether the transaction is container managed or bean
managed?
Transaction management is specified in the deployment descriptor(ejb-jar.xml) using
<transaction-type> tag.
30) What methods are restricted being called inside a method decalred as container
managed?
We should not invoke any method that might interfere with the transaction boundaries set by
the container. The list of restricted methods are as follows:
1) The commit, setAutoCommit, and rollback methods of java.sql.Connection
2) The getUserTransaction method of javax.ejb.EJBContext
3) Any method of javax.transaction.UserTransaction
For stateless session beans, the entering and exiting transaction contexts must match. For
stateful session beans, the entering and exiting transaction contexts may or may not match. If
they do not match, EJB container maintains associations between the bean and the
nonterminated transaction.
Session beans with bean-managed transactions cannot use the setRollbackOnly and
getRollbackOnly methods of the javax.ejb.EJBContext interface.
32) What is the difference between container managed and bean managed transaction?
In container-managed transaction, the transaction boundaries are defined by the container
while in bean managed transaction, the transaction boundaries are defined by the bean.
Entity Beans transactions are always container managed.
33) How many types of transaction attributes are available in EJB and what are they?
There 6 types of Transaction Atributes defined in EJB. They are as follows:
1. NotSupported : If the method is called within a transaction, this transaction is suspended
during the time of the method execution.
2. Required : If the method is called within a transaction, the method is executed in the scope
of this transaction; otherwise, a new transaction is started for the execution of the method
and committed before the method result is sent to the caller.
3. RequiresNew : The method will always be executed within the scope of a new transaction.
The new transaction is started for the execution of the method, and committed before the
method result is sent to the caller. If the method is called within a transaction, this
transaction is suspended before the new one is started and resumed when the new
transaction has completed.
4. Mandatory: The method should always be called within the scope of a transaction, else the
container will throw the TransactionRequired exception.
5. Supports : The method is invoked within the caller transaction scope; if the caller does not
have an associated transaction, the method is invoked without a transaction scope.
6. Never : The client is required to call the bean without any transaction context; if it is not
the case, a java.rmi.RemoteException is thrown by the container.
34) How many types of Isolation Level's are available in EJB and what are they?
35) Are we allowed to change the transaction isolation property in middle of a transaction?
No. You cannot change the transaction isolation level in the middle of transaction.