www.java-interview-questions.in
Q: Way java is platform independent ?
A: All Java programs are compiled into class files that contain bytecodes. These byte codes can be run in any platform and hence java is said to be platform independent.
Q: Which class is a superclass in java ?
A: Oject is a superclass in java.
Q: How to read a text from image file in java ?
A: By using ORC Technology you can read Jpg image or Gif image using java.
Q: What are the main diffrence between c++ and java ?
A: C++ supports pointer and multiple inheritance while java does not support pointer. Java cannot allow pointers, because doing so would allow Java applets to breach the firewall between the Java execution environment and the host computer.
Q: In how many ways we can create an object? Explain with example.
A: 1.Using New KeyWord 2.Using ClassForName.
Example:
new class-name();
class.ForName("Class-Name");
Q: What is the meaning of supplying string[] args to main method?
A: public static void main(String args[]) This array of Strings is the mechanism through which the runtime system passes information to your application. Each String in the array is called a command line argument.
Q: What is a class in java?
A: A class is a specification for any number of objects.
Objects contain both methods (functions) and fields (data).
class List {
// fields
private Object [] items;
private int numItems;
// methods
// constructor function
public List()
{
items = new Object[10];
numItems = 0;
}
// add a given item to the end of the list
public void AddToEnd(Object ob)
{ ... }
}
Q: What if the main method is declared as private?
A: The program compiles properly but at runtime it will give error Message as 'Main method not public'.
Q: What is the purpose of garbage collection in Java ?
A: The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used.
Q: What is meant by pass by reference and pass by value in Java?
A: Pass By Reference means the passing the address itself rather than passing the value. Passby Value means passing a copy of the value to be passed.
public void swap(int var1, int var2)
{
int temp = var1;
var1 = var2;
var2 = temp;
}When swap() returns, the variables passed as arguments will
still hold their original values.The method will also fail if we change the
arguments type from int to Object,since Java passes
object references by value as well. Now, here is where it gets tricky: public void valueSw(Point arg1, Point arg2)
{
arg1.x = 100;
arg1.y = 100;
Point temp = arg1;
arg1 = arg2;
arg2 = temp;
}
public static void main(String [] args)
{
Point pnt1 = new Point(0,0);
Point pnt2 = new Point(0,0);
System.out.println("X: " + pnt1.x + " Y: " +pnt1.y);
System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
System.out.println(" ");
valueSw(pnt1,pnt2);
System.out.println("X: " + pnt1.x + " Y:" + pnt1.y);
System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
}
Q: what is difference between instance and object.?
A: Instance means just creating a reference(copy) .
Object :means when memory location is associated with the object( is a runtime entity of the class)
by using the new operator.