ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Do you have a collection of Interview Questions and interested to share with us!!
Please send that collection to along with your userid / name. ThanQ
Google
 
Categories >> Software >> Java-Related >> Java-J2EE >> Core-Java
 
 


 

Back to Questions Page
 
Question
In Serialization, whether you will use Static variables?
Rank Answer Posted By  
 Question Submitted By :: Rajani
This Interview Question Asked @   HCL
I also faced this Question!!   © ALL Interview .com
Answer
Static variables are not serialized during object serialization.

If u want to serialize static fields, then u must use ur own
serialization overrides and implement the
java.io.Externalizable interface on the class.
 
1
Ranganathkini
 
 
Answer
We cannot serialize the static variables because static is 
specific to the class not for the objects.
 
3
Chandramouli
 
 
Question
Which is the best way to use for String concatenation in 
Java?
Rank Answer Posted By  
 Question Submitted By :: Guest
This Interview Question Asked @   HCL
I also faced this Question!!   © ALL Interview .com
Answer
Java provides the StringBuffer and String classes, and the
String class is used to manipulate character strings that
cannot be changed. Simply stated, objects of type String are
read only and immutable. The StringBuffer class is used to
represent characters that can be modified.

The significant performance difference between these two
classes is that StringBuffer is faster than String when
performing simple concatenations. In String manipulation
code, character strings are routinely concatenated. Using
the String class, concatenations are typically performed as
follows:


     String str = new String ("Stanford  ");
     str += "Lost!!";

If you were to use StringBuffer to perform the same
concatenation, you would need code that looks like this:


     StringBuffer str = new StringBuffer ("Stanford ");
     str.append("Lost!!");

Developers usually assume that the first example above is
more efficient because they think that the second example,
which uses the append method for concatenation, is more
costly than the first example, which uses the + operator to
concatenate two String objects.

The + operator appears innocent, but the code generated
produces some surprises. Using a StringBuffer for
concatenation can in fact produce code that is significantly
faster than using a String. To discover why this is the
case, we must examine the generated bytecode from our two
examples. The bytecode for the example using String looks
like this:


0 new #7 <Class java.lang.String>
3 dup
4 ldc #2 <String "Stanford ">
6 invokespecial #12 <Method java.lang.String(java.lang.String)>
9 astore_1
10 new #8 <Class java.lang.StringBuffer>
13 dup
14 aload_1
15 invokestatic #23 <Method java.lang.String
valueOf(java.lang.Object)>
18 invokespecial #13
21 ldc #1 <String "Lost!!">
23 invokevirtual #15 <Method java.lang.StringBuffer
append(java.lang.String)>
26 invokevirtual #22 <Method java.lang.String toString()>
29 astore_1

The bytecode at locations 0 through 9 is executed for the
first line of code, namely:


     String str = new String("Stanford ");

Then, the bytecode at location 10 through 29 is executed for
the concatenation:


     str += "Lost!!";

Things get interesting here. The bytecode generated for the
concatenation creates a StringBuffer object, then invokes
its append method: the temporary StringBuffer object is
created at location 10, and its append method is called at
location 23. Because the String class is immutable, a
StringBuffer must be used for concatenation.

After the concatenation is performed on the StringBuffer
object, it must be converted back into a String. This is
done with the call to the toString method at location 26.
This method creates a new String object from the temporary
StringBuffer object. The creation of this temporary
StringBuffer object and its subsequent conversion back into
a String object are very expensive.

In summary, the two lines of code above result in the
creation of three objects:

   1. A String object at location 0
   2. A StringBuffer object at location 10
   3. A String object at location 26

Now, let's look at the bytecode generated for the example
using StringBuffer:


0 new #8 <Class java.lang.StringBuffer>
3 dup
4 ldc #2 <String "Stanford ">
6 invokespecial #13 <Method
java.lang.StringBuffer(java.lang.String)>
9 astore_1
10 aload_1
11 ldc #1 <String "Lost!!">
13 invokevirtual #15 <Method java.lang.StringBuffer
append(java.lang.String)>
16 pop

The bytecode at locations 0 to 9 is executed for the first
line of code:


     StringBuffer str = new StringBuffer("Stanford ");

The bytecode at location 10 to 16 is then executed for the
concatenation:


     str.append("Lost!!");

Notice that, as is the case in the first example, this code
invokes the append method of a StringBuffer object. Unlike
the first example, however, there is no need to create a
temporary StringBuffer and then convert it into a String
object. This code creates only one object, the StringBuffer,
at location 0.

In conclusion, StringBuffer concatenation is significantly
faster than String concatenation. Obviously, StringBuffers
should be used in this type of operation when possible. If
the functionality of the String class is desired, consider
using a StringBuffer for concatenation and then performing
one conversion to Strin
 
5
Ginu Thomas
 
 
 
Answer
'+'operator is the best way to concatenate bcz by using 
this operator we can concatenate more than two strings.
   This is not possible by using concat method
 
0
Manasa
 
 
Answer
Hi Manasa,

You can use append to concatenate more than 2 strings, such as

StringBuffer str = new StringBuffer("Stanford ");
str.append("Lost!!").append(" again.");
 
0
Leo Zhao
 
 
Answer
concat()
 
0
Ravikiran
 
 
Answer
class stringadd
{
public static void main(String args[])
{
String a="hello";
System.out.println("the string is::"+a);
a+=" govind";
System.out.println("after concating::"+a);
}
}
 
0
Govind
 
 
Question
What is the use join() in Threads ?
Rank Answer Posted By  
 Question Submitted By :: Harish
This Interview Question Asked @   HCL
I also faced this Question!!   © ALL Interview .com
Answer
It waits for the Parent Thread process to be completed.  
Even though, Child process is completed.  It waits for the 
Parent Thread.
 
2
Harish
 
 
Answer
waits for specified number of milliseconds for thread to die
 
5
Padmaja
 
 
Answer
if we  want  to finish main thread last i.e  to ensure all 
child threads terminate prior to the main thread
 
0
Madan
 
 
Answer
Acoording to join the two Threads for some iterations.
 
0
Venkat
 
 
Question
What is the use of anonymous inner classes ?
Rank Answer Posted By  
 Question Submitted By :: Harish
This Interview Question Asked @   HCL
I also faced this Question!!   © ALL Interview .com
Answer
an anonymous class is a local class that has no name
 
0
Harish
 
 
Answer
anonymous classes help to declare a class and create an 
instance of a class in the same step.
 
0
Chandrarekha
 
 
Answer
Anonymous classes

1.These are one  type of inner class that  having no mame.

2.The syntax will come like 

class a
{
public void mymethod ();

}
class b
{
a obja = new a(){//the anonymous class starts here
public void mymethod (){ //have to implement the methods of
//class 'a'
return "here we are implementing mymethod of class 'a'"
}

}; //when finishing anonymous classes we have to put";"

}

3.Here the anonymous class is consider as a sub class of
class 'a'.We can access all the variables and methods of
super class 'a' through the anonymous block,except local
variables,including private variables.

4.Through anonymous classes we can create instance for
Interfaces.
 
0
Ashokkumar
 
 
Answer
Anonymous class helps in Eventhandling
 
0
Guest
 
 
Answer
Anonymous class exist till the method or block (In which it 
is defined) runs. After that it vanishes so it help to use 
the memory efficiently.

in your code if you want a particular class once and that 
is also in the method/block use anonymous class.

it is the best of my knowledge. if you know more please 
reply me.
 
0
Sumit Bansal
 
 
Answer
We can declare an inner class within the body of a method 
without naming it. These classes are known as anonymous 
inner classes
         Very useful for controlled access to the innards 
of another class. and useful when we need one instance of a 
special class.
 
0
Bindhu
 
 
Answer
Anonymous class is a class which has no methods defined in 
it.
Eg.
class a
{
public void hello();
}
class b
{
a obj = new a();
public void hello()
{
System.out.println("Hello World");
}
}
 
0
Jubin Thomas, Bhilai(mpccet)
 
 
Answer
The use of an anonymous inner class is to create a
non-reusable implementation of an abstract class or
interface.  For example, a UI button that is Runnable can
implement its  run method at the same place in code that it
is created.
 
0
Sam E
 
 
Question
What is the format of Inner Class after it compiled?
Rank Answer Posted By  
 Question Submitted By :: Harish
I also faced this Question!!   © ALL Interview .com
Answer
OuterClass$InnerClass.class
 
0
Harish
 
 
Question
What is the life cycle of Thread ?
Rank Answer Posted By  
 Question Submitted By :: Harish
This Interview Question Asked @   HCL
I also faced this Question!!   © ALL Interview .com
Answer
Once you create a Thread Class.  Instantiate the Thread and 
invoke the method start.  It intern triggers the run method 
on the Thread.  Once the run method is completed, The 
thread will be in dead state.
 
0
Harish
 
 
Answer
1. when the thread create New State
2. after start()  Runnable state
3. Running State
4. Wait/Block/Sleep State
5. Dead State
 
4
Chinnadurai
 
 
Answer
1.create the thread on which object u want, using Thread
interface,
2.then use obj.start() to invoke the thread
3.this method will intern call run() of Thread class 
4.once it's serviced, it will come to dead state.
 
0
Harmeet
 
 
Answer
Multithreading is the mechanism in which more than one 
thread run independent of each other within the process. 
wait (), notify () and notifyAll() methods can be used for 
inter-thread communication and these methods are in Object 
class. wait() : When a thread executes a call to wait() 
method, it surrenders the object lock and enters into a 
waiting state. notify() or notifyAll() : To remove a thread 
from the waiting state, some other thread must make a call 
to notify() or notifyAll() method on the same object.
 
0
Shajin.a.xavier
 
 
Answer
Ready-to-run
A thread starts its life cycle with a call to start(). For 
example

 MyThread aThread = new MyThread();
 aThread.start();

A call to start() will not immediately start thread's 
execution but rather will move it to pool of threads 
waiting for their turn to be picked for execution. The 
thread scheduler picks one of the ready-to-run threads 
based on thread priorities. 
Running
The thread code is being actively executed by the 
processor. It runs until it is swapped out, becomes 
blocked, or voluntarily give up its turn with this static 
method 
 Thread.yield();

Please note that yield() is a static method. Even if it is 
called on any thread object, it causes the currently 
executing thread to give up the CPU. 
Waiting
A call to java.lang.Object's wait() method causes the 
current thread object to wait. The thread remains 
in "Waiting" state until some another thread invokes notify
() or the notifyAll() method of this object. The current 
thread must own this object's monitor for calling the wait
(). 
Sleeping
Java thread may be forced to sleep (suspended) for some 
predefined time.

 Thread.sleep(milliseconds);
 Thread.sleep(milliseconds, nanoseconds);

Please note that static method sleep() only guarantees that 
the thread will sleep for predefined time and be running 
some time after the predefined time has been elapsed.
For example, a call to sleep(60) will cause the currently 
executing thread to sleep for 60 milliseconds. This thread 
will be in ready-to-run state after that. It will be 
in "Running" state only when the scheduler will pick it for 
execution. Thus we can only say that the thread will run 
some time after 60 milliseconds. 
Blocked on I/O.
A java thread may enter this state while waiting for data 
from the IO device. The thread will move to Ready-to-Run 
after I/O condition changes (such as reading a byte of 
data). 
Blocked on Synchronization.
A java thread may enter this state while waiting for object 
lock. The thread will move to Ready-to-Run when a lock is 
acquired. 
Dead
A java thread may enter this state when it is finished 
working. It may also enter this state if the thread is 
terminated by an unrecoverable error condition.
 
0
Shajin.a.xavier
 
 
Answer
start
run
destroy  are the methods

born 
runnable
running
sleep/blocked
dead          are the states

 
0
Ravikiran
 
 
Answer
Born state.
Running state.
Idle state.
Dead state.
 
0
Venkat
 
 
Question
How to set the Heap size in Java ?
Rank Answer Posted By  
 Question Submitted By :: Harish
This Interview Question Asked @   HCL , HCL
I also faced this Question!!   © ALL Interview .com
Answer
set JAVA_OPTS=-server -Xmx700m
 
0
Harish
 
 
Answer
The heap size of the JVM can be set by specifying the
command-line flags -Xms and -Xmx

-Xms sets the initial heap size ( example: -Xms 30m )

-Xmx sets the maximum heap size ( example: -Xmx 70m )

Hope it helps! :)
 
0
Ranganathkini
 
 
Question
Is JRE required to compile Java files ?
Rank Answer Posted By  
 Question Submitted By :: Harish
This Interview Question Asked @   HCL
I also faced this Question!!   © ALL Interview .com
Answer
To compile a Java file, it should have a tools.jar and 
dt.jar in the classpath.  It should have a javac 
executable.  It compiles the class file.  If you want to 
execute the class file, you should have a Java Run-time 
Environment. (JRE)
 
5
Harish
 
 
Answer
no
 
1
Madan
 
 
Answer
see,javac is compiler of java which is use to convert 
the .java file into .class file,but only .class file can 
not exceute the java program for exceuting java program we 
should require a enviorment and a JIT compiler,the 
enviorment over which .class file exceutes itself is called 
jre(java runtime enviorment),Na JIt compliler which works 
as interepter for .class file ,that makes java plate for 
independent
 
0
Abhishek
 
 
Question
How do you set security in applets?
Rank Answer Posted By  
 Question Submitted By :: Janet
This Interview Question Asked @   Wipro
I also faced this Question!!   © ALL Interview .com
Answer
using setSecurityManager() method
 
0
Janet
 
 
Question
What are different type of access modifiers?
Rank Answer Posted By  
 Question Submitted By :: Janet
I also faced this Question!!   © ALL Interview .com
Answer
Public: Any thing declared as public can be accessed from 
any where.
Private: Any thing declared as private can't be seen 
outside of it's class.
Protected: Any thing declared as protected can be accessed 
by classes in the same package and subclasses in the other 
packages.
Default modifier: can be accessed only to classes in the 
same package.
 
0
Janet
 
 
Answer
Access modifiers is different from Access specifiers. The 
access specifiers tells whether member data\methods\classes 
of a class can be accesible or not by other 
classes\subclasses\packages whereas the modifiers tells how 
the memberdata\methods\classes can be used by other classes.

The access specifiers available in java are 
Public,private,protected,default(friend or package).
The modifiers available are 

static:can be applied to member data,methods,inner 
classes.used to define class variables and methods that 
belong to the a class but not to any particular instance of 
class.all the variables share the same static method.

final:(variables,methods,classes) final modifier indicates 
that the variable\method\class cannot be modified in any 
other classes\subclasses\packages.

abstract:(classes) used to declare classes having same 
properties and methods.abstract classes are used to derive 
classes of the same type.

native:(methods) indicates that the code is out of java 
runtime environment,written in some other programming 
language other than java.

synchronized:(methods) used to control access to a 
particular method in a multithreaded java program.
 
0
Chandra Rekha
 
 
Answer
static
final
native
abstract
synchronized
strictfp
transient
volatile
 
0
Ravikiran
 
 
Question
How many ways can an argument be passed to a subroutine?
Rank Answer Posted By  
 Question Submitted By :: Janet
I also faced this Question!!   © ALL Interview .com
Answer
An argument can be passed in two ways. They are passing by 
value and passing by reference.
  Passing by value: This method copies the value of an 
argument in to the formal parameters of the subroutine.
  Passing by reference: In this method, a reference to an 
argument(not the value of the argument) is passed to the 
parameter.
 
0
Janet
 
 
Answer
In java the arguments are passed by pass by value . Using 
this pass by value way u can pass both primitive and object 
type values.
 
0
Vijayakumar Chinnasamy
 
 
Question
what is an object and how do you allocate memory to it?
Rank Answer Posted By  
 Question Submitted By :: Janet
This Interview Question Asked @   Wipro , Anshinsoft
I also faced this Question!!   © ALL Interview .com
Answer
Object is an instance of a class and it is a software unit 
that combines a structered set of data with a set of 
operations for inspecting and manipulating that data. when 
an object is created using new operator,memory is allocated 
to it.
 
2
Janet
 
 
Answer
Object is an instance of the class.There are two ways by 
which a new object is created.

1.using new operator.
example:
classname objectname=new classname;
This allocates the memory to objectname.

This can be classname objectname by which null values are 
set to the objectname and not the values as in the class.

2.without using new operator.
Declare the variables as static and also methods used as 
static and by using the keyword static we can create an 
instance of the class.
Thus, we use public static void main(String args[])in main 
program where theres no need to create a class and also 
instance of class using new operator.
 
0
Tulasi
 
 
Answer
Hi... An object is nothing but instance of the class,or 
basic run-time entity.By using "new" operator we can 
allocate the memory of an object
 
0
Devarathnam
 
 
Question
what are class,constructor and primitive data types?
Rank Answer Posted By  
 Question Submitted By :: Janet
I also faced this Question!!   © ALL Interview .com
Answer
Class is a template for multiple objects with 
similar features and it is a blue print for objects. it 
defines a type of object according to the data the object 
can hold and the operations the object can perform.
             Constructor is a special kind of method that 
determines how an object is initialized when created.
            primitive data types are 8 types and they are:
byte,short,int,long,float,double,boolean,char.

 
0
Janet
 
 
Answer
class is a template for members and methods to be places
constructor is used to initialize the instance variables.
primitive datatypes are the memory holders for diffrent 
type of data to hold
 
0
Ravikiran
 
 
Question
what is a static block?
Rank Answer Posted By  
 Question Submitted By :: Sumalatha
I also faced this Question!!   © ALL Interview .com
Answer
A static block or a static initializer is a block that
appears within a class definition but outside any member
definition with the keyword static ahead of it. Example:


class MyClass {
    private static double myValue;
    static {
        myValue = Double.parseDouble( System.getProperty(
"version" ) );
    }
}

It is mainly used to perform static construction i.e.
initialization of static variables (see above example). The
static initializer is executed when the class is loaded by
the JVM.
 
0
Ranganathkini
 
 
Answer
static block contains  a code which is executed without 
object of  a class.i.e is excuted at the time of class 
loading
 
0
Madan
 
 
Answer
static block is used to initialization during the JVM start 
up
 
0
Ravikiran
 
 
Answer
Static block is executed when the class is loaded into the 
memory before the main(). Only once it will be executed.
 
0
Srinivasa
 
 
Question
Functionality of JVM?
Rank Answer Posted By  
 Question Submitted By :: Sumalatha
This Interview Question Asked @   Infosys
I also faced this Question!!   © ALL Interview .com
Answer
The JVM is the core of the Java platform and is responsible for:

1. Loading bytecodes from the class files
2. Verifying the loaded byte codes
3. Linking the program with the necessary libraries 
4. Memory Management by Garbage Collection
5. Managing calls between the program and the host environment.
 
3
Ranganathkini
 
 
Answer
Java sovles the problem of platform independence by using 
byte code.Java complier does not produce native executable 
code.Instead it produces a special format called byte code.

Byte code is a highly optimized set of instructions 
designed to executed by a java runtime system called Java 
Virtual Machine(JVM).JVM is an interpreter for byte code.

This interpreter reads or understands the bytecode and 
executes the corresponding native machine instructions.

Thus to port java programs to a new platform ,all that 
needed is to port the interperter and some of the library 
routines.Even the complier is written in java.The byte 
codes are precisely defined and remain the same on all 
platforms.

The use of byte code  enables the java runtime system to 
execute programs much faster.
 
1
Shakir Khan
 
 
Answer
JVM will convert the byte code to user understandable code.
Provides the services like class loading,garbage collection
 
0
Ravikiran
 
 
Answer
JVM works is only to provide memory and resource allocation
.the conversion of bytecode into machine code is done by
J.I.T(just in time compiler)
if a program is executed on the same machine then the full
resources used by JVM else if on other system then less
resources used by JVM.
 
0
Jitesh Singh
 
 
 
Back to Questions Page
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com