Wednesday, October 31, 2012

experienced Core java interview questions and Answers

Can an inner class be built in an Interface?

• Yes,an inner class may be built an Interface
Example :

public interface xyz
static int p=0;
void m();
class c
int q;
public static void main(String c[])
System.out.println("inside ");

Why is explicit object casting needed?

In order to assign a superclass object in a variable to a subclass,one needs to do explicit casting.

For example:

Person person;
Man man;
man = (Man)person;

An automatic casting takes place when we typecast a object in subclass as parent class object.

Define Externalizable.

• Externalizable is coined as an Interface
• It extends the Serializable Interface.
• It also sends data into the Streams.
• Externalizable sends data in a Compressed Format.
• Externalizable is having two methods,for e.g. writeExternal(ObjectOuput out) & readExternal(ObjectInput in)

How can a collection object be sorted?

• // Sort

• // Sort case-insensitive
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);

• // SortReverse-order
Collections.sort(list, Collections.reverseOrder ());

• // Reverse-order sort case-insensitive
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);

Define local, member and a class variable.

• Within a method variables declared are called “local” variables.

• Variables declared in the class i.e not in any methods are “member” variables (global variables).

• Variables declared in the class i.e not in any methods and are called as “static” are class variables

Name the different identifier states of a Thread.

Different types of identifiers of a Thread are:

• R - Running or runnable thread
• S - Suspended thread
• CW - Thread waiting on a condition variable
• MW - Thread waiting on a monitor lock
• MS - Thread suspended waiting on a monitor lock

Define Vector class? Differentiate the Vector and ArrayList.

• Vector canbe said a legacy class which has been introduced to implement the List interface since Java 2 platform v1.2
• Vector is always synchronized but ArrayList is not.
• When Vector class is synchronized, if we will run in multithreading environment we've to use ArrayList with Collections.
• Vector has a default size i.e 10 while arrayList has no default size .
• ArraayList is not having any method returning Enumerations where as vector list is having.

Differentiate between Enumeration and Iterator interface

• In java.util package the Enumeration and Iterator are available.
• The Enumeration interface is replicated by the Iterator interface.
• In preference to Enumeration new implementations should consider using Iterator .

The difference of Iterators from enumerations are:

• Enumeration has 2 methods namely hasMoreElements() & nextElement() where the Iterator contained three methods namely hasNext(), next(),remove().
• An optional remove operation is added in Iterator,and has shorter method names. We Use remove() to delete the objects but the Enumeration interface does not support this feature.
• The legacy classes use Enumeration interface .Vector.elements() & Hashtable.elements() method results Enumeration. All Java Collections Framework classes returns iterator. java.util.Collection.iterator() method returning an instance of Iterator.

What are the alternatives to inheritance?

• Delegation is an alternative to inheritance.

• Delegation denotes that you include an instance of any class as an instance variable, and forward messages to the instance.

• It is safer than inheritance because it ceases you to think about forwarded message , because the instance is of a known class, rather than a new class, and because it doesn’t force you to accept all the methods of the super class: you can provide only the methods that really make sense.

• On the other hand, it makes you write more code, and it is harder to re-use (because it is not a subclass).

Number the bits, used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?

• Unicode requires 16 bits

• ASCII require 7 bits. but it is usually represented as 8 bits.

• UTF-8 represents characters using 8, 16, and 18 bit patterns.

• UTF-16 uses 16-bit and larger bit patterns.

Define reflection

• Reflection allows program related access to information about the fields, methods and constructors of loaded classes

• It use reflected fields, methods, and constructors

• It helps to operate on their underlying counterparts on objects,

• It operates within security restrictions.

What is RMI and how it is useful?

• Remote method invocation is called RMI.

• One can work with remote object using RMI .

• It gives a impression that you are working with a object that resides within your own JVM though it is somewhere.

• The protocol used by RMI is RMI-IIOP

Define a Collection API.

• The set of classes and interfaces supporting the operation on collections of objects is the Collection API .

• Than the vectors, arrays, and hashtables if effectively replaces,these classes and interfaces are more flexible, more powerful, and more regular

• class examples: HashSet, TreeMap, ArrayList, LinkedList,HashMap and TreeMap.

• interface examples: Set,List ,Collection and Map.

How many forms of Polymorphism are there?

polymorphism exists in three different forms in Java:

• Method overloading
• Method overriding through inheritance
• Method overriding through the Java interface

Define the wrapper classes in Java and name a few.

• Wrapper class is wraps around the primitive data type.

• list of the primitive types and the corresponding wrapper classes:

Primitive Wrapper
boolean java.lang.Boolean
byte java.lang.Byte
char java.lang.Character
double java.lang.Double
float java.lang.Float
int java.lang.Integer
long java.lang.Long
short java.lang.Short
void java.lang.Void

Differentiate between JDK ,JRE & JVM

• JDK stands for Java Development Kit.
It is the most widely used Java Software Development Kit.

• JRE stands for Java Runtime Environment
It is an implementation of the Java Virtual Machine which executes Java programs

• JVM stands for Java Virtual Machine
It is an interpreter.

Why will you use Comparator and Comparable interfaces?

• java.util.Comparator

• java.util.Comparator compares some other class’s instances,

• java.lang.Comparable

• java.lang.Comparable compares another object with itself .

Differentiate between final, finally and finalize.

• The keyword is final.sss
It is used for declaring a constant
It prevents a class from producing subclasses.

• finally is a code.
It always executes when the try block is finished,
Unless System.exit() has been called.

• finalize() is a method,
Before discarding by the garbage collector it is invoked .

Differentiate JAR and WAR files

JAR files:

• JAR files is the acronym stands for Java ARchive fles.
• JAR files allow aggregating many files into one,
• JAR is usually used to hold Java classes in a library.

WAR files:

• WAR files is the acronym stands for Web ARchive fles.
• WAR stores XML, java classes, and JavaServer pages
• WAR is mainly used for Web Application purposes.`

In a Java , how can you send program messages on the system console, but error messages, to a file?

• The class System has a variable out that denotes the standard output,
• the standard error device represents the variable err .
• Naturally, they both point at the system console.

In this way, the standard output can be sent to the file:

Stream x = new Stream(new FileOutputStream("error.txt"));

No comments:

Post a Comment