www.java-interview-questions.in
Q: What is an Object and how do you allocate memory to it?
A: Object is an instance of a class and it is a software unit that combines a structured 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.
Q: What are Transient and Volatile Modifiers?
A: Transient: The transient modifier applies to variables only and it is not stored as part of its object̢۪s Persistent state. Transient variables are not serialized. Volatile: Volatile modifier applies to variables only and it tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of the program.
Q: What is the difference between this() and super()?
A: this() can be used to invoke a constructor of the same class whereas super() can be used to invoke a super class constructor.
Example,
This:
public Date(long time)
{
this.time = time;
}
public Date(int year, int month, int day)
{
this(year, month, day, 0, 0, 0);
}
Super:
class Superclass {
int x, y;
public Superclass(int x, int y) {
this.x = x;
this.y = y;
}
public void printMethod() {
System.out.println("Printed in Superclass.");
}
}
public class Subclass extends Superclass {
public Subclass(int x, int y) {
super(x, y); //call superclass constructor
}
public void printMethod() { //overrides printMethod in Superclass
super.printMethod();
System.out.println("Printed in Subclass");
}
public static void main(String[] args) {
Subclass s = new Subclass(1, 3);
s.printMethod();
}
}
Q: What is the difference between superclass and subclass?
A: A super class is a class that is inherited whereas sub class is a class that does the inheriting.
Q:What is Anonymous Classes in java ?
A: An anonymous class is a local class without a name. An anonymous class is defined and instantiated in a single succinct expression using the new operator. While a local class definition is a statement in a block of Java code, an anonymous class definition is an expression, which means that it can be included as part of a larger expression, such as a method call. When a local class is used only once, consider using anonymous class syntax, which places the definition and use of the class in exactly the same place.
Example :
public class OuterClass extends Applet
{
Button button = new Button("Click Me !");
public OuterClass ()
{
int i = 0;
button.addActionListener
( // The argument is the object created by the following:
new ActionListener () // no name given to this object
{
public void actionPerformed (ActionEvent e) {
i++;
System.out.println ("Pressed "+i+" times");
}
}
);
add(button);
}
} // class OuterClass
new class-name ( [ argument-list ] ) { body of class }
or:
new interface-name () { body of class }
Q: What is a reflection ?
A:Java's Reflection API's makes it possible to inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time. It is also possible to instantiate new objects, invoke methods and get/set field values using reflection. java. lang. reflect package has the ability to analyze itself in runtime.
Example :
import java.lang.reflect.*;
public class Methods {
public static void main(String args[])
{
try {
Class c = Class.forName(args[0]);
Method method[] = c.getDeclaredMethods();
for (int i = 0; i < method.length; i++)
System.out.println(method[i].toString());
}
catch (Throwable e) {
System.err.println(e);
}
}
}
For an invocation of
java Methods java.util.Stack
OutPut :
public java.lang.Object java.util.Stack.push( java.lang.Object)
public synchronized java.lang.Object java.util.Stack.pop()
public synchronized java.lang.Object java.util.Stack.peek()
public boolean java.util.Stack.empty() public synchronized
int java.util.Stack.search(java.lang.Object)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment