Java Interview Questions


What is Java?

Qus:- Explain java beans and what are its advantages.

Ans:- It is a software component which can be used on variety of environment

Advantages of beans are:

Beans are written only once and can be used many times
Configuration setting of beans can be saved in persistant storage
We can control the exposure of properties methods and events of beans
It can send and receive events from another objects
We can easily configure beans using auxiliary software and it is not included in run time environment
It may or may not be visible to another user


Qus:- In Java, what are the basic differences between an Interface and an Abstract class?

Ans:- Only a interface can extend a interface but any class can extend abstract class
All the variables in interface are set as final by default
Interface execution is slow as compared to abstract class
Abstract scope is only upto the derived class and scope of Interface is upto any level
Abstract class can implement method but interface cannot because a interface contains only the signature of a method but not the body of a method
In a class we can implement many interfaces but only one abstract class


Qus:- Explain connection pooling in java

Ans:- In connection pooling several context instances have been created for the same database.
As application server starts a pool of connection object has been created
These objects are managed by pool manager
Pool manager provides context instance as the request comes and when the context instance is done with connection, it is retured in the pool for future use
The JDCConnectionPool.java class is use to provide connections available to calling program in its getConnection method. This method searches for a connection and if not available then a new connection is created.


Qus:- Explain difference between get and post method.

Ans:- Visibility - GET request is sent via the URL string which is visible whereas POST request cant be seen as it is encapsulated in the body of the HTTP request
Length – there is limitation of length for GET request as it goes through URL and its character length cant be more than 255 but in POST request no such limitation.
Performance –As no time is spend on encapsulation in GET request so it is comparatively faster and relatively simpler. In addition, the maximum length restriction facilitates better optimization of GET implementation.
Type of Data - GET request can carry only text data as we know URL only contain text data on the other hand post request can carry both text as well as binary
Caching/Bookmarking – as we know now that GET request is nothing but a URL hence it can be cached as well as bookmarked. No such options are available with a POST request.
FORM Default - GET is the default method of the HTML FORM element. To submit a FORM using POST method, we need to specify the method attribute and give it the value "POST".
Data Set - GET requests are restricted to use ASCII characters only whereas no such restrictions are in POST requests


Qus:- What are the ways in which a thread can enter the waiting state?

Ans:- A thread can enter the waiting state by:

By sleep() method invocation
By blocking its input and output
By invoking an object’s wait() method.
It can also enter the waiting state by invoking its suspend() method.


Qus:- Give a brief description of RMI

Ans:- RMI(remote method innovation) allows a java object to be executed on another machine.
It helps in building distributed applications
RMI uses object serialization to marshal and unmarshal objects
RMI mechanism is basically a object oriented RPC mechanism
To deal with client communication Code skeleton is used at skeleton end
small example of RMI which returns the various information about European (EU)countries

import java.rmi.Remote;
import java.rmi.RemoteException;
public interface EUStats extends Remote {
String getMainLanguages(String CountryName)
throws RemoteException;
int getPopulation(String CountryName)
throws RemoteException;
String getCapitalName(String CountryName)
throws RemoteException;
}


Qus:- Which is better from Extending Thread class and implementing Runnable Interface?

Ans:- • Implementing runnable interface is more advantageous because when you are going for multiple inheritance, then only interface can help.
• If you are already inheriting a different class, then you have to go for Runnable Interface. Otherwise you can extend Thread class.
• Also, if you are implementing interface, it means you have to implement all methods in the interface.
• If we implement runnable interface it will provide better object oriented design
• Implementing also gives consistency
• By using runnable interface we can run the class several times whereas thread have start() method that can be called only once.



Qus:- What do you understand by JSP actions?

Ans:- JSP actions are XML tags that forces the server to directly use the server to the existing components or control the behavior of the JSP engine.
JSP actions are executed when a JSP page is requested.
Actions are inserted in the jsp page using XML syntax to control the behavior of the servlet engine.
By using JSP actions, we can reuse bean components, dynamically insert a file and switch the user to another page.
Some of the available actions are as follows:

<jsp: include> – include a file at the time the page is requested.
<jsp: useBean> – find or instantiate a JavaBean.
<jsp: setProperty> – set the property of a JavaBean.
<jsp: getProperty> – insert the property of a JavaBean into the output.
<jsp: forward> – forward the requester to a new page.
<jsp: plugin> – generate browser-specific code that makes an OBJECT or EMBED tag for the Java plugin.


Qus:- Explain daemon thread in Java and which method is used to create the daemon thread?

Ans:- Deamon thread runs in the back ground doing the garbage collection operation for the java runtime system.
It has the low priority
These threads run without the involvement of the user.
We can use the accessor method isDaemon() to determine if a thread is a daemon thread.
Daemon threads exist only to serve user threads.
The setDaemon() method is used to create a daemon thread


Qus:- Define reflection in java

Ans:- It allows you to analyze a software component like java beans
It helps in describing software component capability dynamically, at run time rather than at compile time
This is provided by java.lang.reflect package which includes several interfaces
Various classes in this package and their functions are:

AccessibleObject : Allows you to bypass the default access control checks.
Array : Allows you to dynamically create and manipulate arrays.
Constructor : Provides information about a constructor.
Field : Provides information about a field.
Method : Provides information about a method.
Modifier : Provides information about class and member access modifiers.
Proxy : Supports dynamic proxy classes.
ReflectPermission : Allows reflection of private or protected members of a class.


Qus:- Explain mutable and immutable objects with example

Ans:- Mutable objects have the fields which can be changed even after the object is created.

Instance variables are set as public.

Example:

class Mutable{
private int value;
public Mutable(int value) {
this.value = value;
}
getter and setter for value
}
Immutable objects have no fields to be changed. All the instance variables are private.

Example:
public class ImmutableClass {
private final int value;
public MutableClass(final int aValue) {
//The value is set. Now, and forever.
value = aValue;
}
public final getValue() {
return value;
}}

Qus:- What is an Applet with a example?

Ans:- • It is a small program which is easily transmitted over internet
• Applet can be downloaded whenever a user demand
• It makes some of the functionality to be move from server to client
• It is installed automatically
• It has limited access of resources
• It run as a part of web document
• Lesser risk of viruses
• Applet intract with the user through AWT(abstract window toolkit) not with I/O
Small example to draw a string using applet :

import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet {
public void paint(Graphics g) {
g.drawString("A Simple Applet", 20, 20);
}
}


Qus:- What is the use of finally keyword in exception handling?

Ans:- • In a program where exception is handled using try catch block after this finally keyword is used.
• Finally creates a block that will be executed even after a exception is thrown or not
• If no catch statement matches the exception even then finally block will be executed
• It is executed just before the method return
Example :

class FinallyDemo {
// Through an exception out of the method.
static void procA() {
try {
System.out.println("inside procA");
throw new RuntimeException("demo");
} finally {
System.out.println("procA's finally");
}
}

Qus:- Explain generics use in Java?

Ans:- • Generic means parametrized type
• It makes operation and paramneter to decide the data type of interfaces,classes and methods
• It also add the type safety
• It also streamline the process
• It helps in reusing the code
Example of generics:

class Gen<T> {
T ob; // declare an object of type T
// Pass the constructor a reference to
// an object of type T.
Gen(T o) {
ob = o;
}
// Return ob.
T getob() {
return ob;
}
// Show type of T.
void showType() {
System.out.println("Type of T is " +
ob.getClass().getName());
}
}


Qus:- How are Cookies managed in java?

Ans:- • Cookies are managed by java.net package which includes classes and interfaces.
• It is used to create a stateful HTTP session.
• The classes are CookieHandler,CookieManager, and HttpCookie.
• The interfaces are CookiePolicy and CookieStore.
• All but CookieHandler was added by Java SE 6.


Qus:- What are recent changes to collection?

Ans:- Introduction of Generic in Collections Framework :

• With the addition of generic makes all the Collections Framework has been reengineered for it.
• All the collection now become generic
• one the most important feature that has been added due to genetic is type safety

Autoboxing facilitates the use of primitive types
• It helps in storing of primitive types in collections.

The For-Each Style for Loop
• Now collection can be cycled through by use of the for-eachstyle for loop.
• Although iterators are still needed for some uses, in many cases, iterator-based loops can be replaced by for loops.

Qus:- What is the thee use Inet class in networking?

Ans:- • The InetAddress class binds both the numerical IP address and the domain
• name for that address.
• We can interact with Inet class by using the name of an IP host rather than its IP address.
• InetAddress can used in both IPv4 and IPv6 addresses.
• The InetAddress class has no visible constructors.
• factory methods are used to create an InetAddress object

Commonly used InetAddress factory methods are following:
• static InetAddress getLocalHost( )
• throws UnknownHostException
• static InetAddress getByName(String hostName)
• throws UnknownHostException
• static InetAddress[ ] getAllByName(String hostName)
• throws UnknownHostException



Qus:- What are different ways for character extraction in String class?

Ans:- Number of ways of character extraction in string class are:

charAt( )
• it is used to extract a single character from a String.

Its general form is:
• char charAt(int index)
• Here, index of the character to be extracted it should be non negative and should be with in the character
getChars( )
• To extract more than one character at a time, getChars( ) method is use.

It has this general form:
• void getChars(int sourceStart, int sourceEnd, char target[ ], int targetStart)
• Here, sourceStart specifies the index of the beginning of the substring, and sourceEnd
• specifies an index that is one past the end of the desired substring.
getBytes( )
• it extracts more than one character and stores in array but in byte form by default..

Here is its simplest form:
• byte[ ] getBytes( ).


toCharArray( )

• to convert all the characters in a String object into a character array, the easiest way is to call toCharArray( ).
• It returns an array of characters for the entire string.

It has this general form:
• char[ ] toCharArray( )


Qus:- What is the use of Adapter class in event handling?

Ans:- Use of adapter class are:

• It simplifies the creation of event handling
• Interface holds only abstract methods and requires all of them to be implemented to avoid this adapter is used
• It processes and receives some event which is handled by listener interface
• We can create a new class by extending one of the adapter classes to act as a event listener.
• It provides an empty implementation of all methods
• It is used to create an interface having dummy methods


Qus:- Name and explain the types of specifier in java.

Ans:- Types of specifier are:

Private :

• same class member can access the data
• Same package subclass cannot access it
• Same package non subclass is not able to access the private data
• Different package subclass and different package non class can not access it

Protected

• same class member can access the data
• Same package subclass cannot access it
• Same package non subclass not able to access the private data
• Different package subclass can access the data but not different package non class

No modifier

• same class member can access the data
• Same package subclass can access it
• Same package non subclass able to access the private data
• Different package subclass and different package non class can not access it

Public

• same class member can access the data
• same package subclass cannot access it
• same package non subclass is able to access the private data
• different package subclass and different package non class can access it




0 comments:

Post a Comment