Java Interview questions with answers posted on July 22, 2008 at 8:10
am
Question - What is package in JAVA?
Answer
Packages group together related classes and interfaces. Packages avoid name
conflicts. Classes in java are group together in a package using "package"
keyword.
Example,
package P1;
public class Class1
{
}
public class Class2
{
}
Question - Can we have parameterized constructors in Java?
Answer
Yes, we can have.
This is also called as constructor overloading.
Example
Class clsEmp
{
int age;
clsEmp()
{
age =
21;
}
clsEmp(int a)
{
age = a;
}
}
Question - Explain the difference between static and non-static member of a
class.
Answer
A static member has only one copy of instance variables that share among all
the objects of the class whereas a non-static member has its own copy of
instance variable.
Question - Define inner class in Java
Answer
Class that is nested within a class is called as inner class. The inner class
can access private members of the outer class. It is mainly used to implement
data structure and some time called as a helper class.
Question - Describe the use of “instanceof” keyword.
Answer
“instanceof” keyword is used to check what is the type of object.
Question - What is the purpose of “this” keyword?
Answer
“this” keyword is used to refer current instance of object.
Question - What is the disadvantage of garbage collector?
Answer
Although garbage collector runs in its own thread, still it has impact on
performance. It adds overheads since JVM has to keep constant track of the
object which are not referenced and then free these unreferenced objects.
Question - What are the methods in java to force garbage collector to run?
Answer
Garbage collector can be force to run using “System.gc” or “Runtime.gc”.
Question - Define Applet.
Answer
An applet is a small server side application that can be loaded and controlled
on the browser by the client application. An applet has limited access to
resources in order to ensure security.
Question - Explain the life cycle of an applet.
Answer
Below are sequences of methods that describes the life cycle of an applet.
Init()
This is first method called when applet loads. This method can be used to set
applet attributes like color, fonts etc.
Start()
This method starts a thread in which it runs the paint method.
Paint()
This method is called every time when an applet has to re-display.
Stop()
It is called when the user goes to the other page.
Distroy()
This is called when the browser exits. We can use this method for cleaning non
java resources.
|