Sunday, November 21, 2010

Difference between Applet and Application program

0 comments
An applet is client side program which is downloaded in client side and run in the browser.  Applet program can not access network connection like opening socket ,writing to socket. A class extending java.awt.Applet class which has methods like init(), start(), stop(), destroy(),paint() overridden. And cannnot write to client side files i.e. hard disk.


An application runs standalone with a support of virtual machine. An application does not have nay restrictions as Applets have over network and file related activities.They are free to open sockets over a network read and write to a file

Tuesday, November 16, 2010

Write a program to get the Host Name on the basis of IP address

0 comments
import java.net.*;
public class HostName
{
public static void main(String args[])
{

InetAddress inetAddress =InetAddress.getByName("127.64.84.2");//get the host Inet using ip
System.out.println ("Host Name: "+ inetAddress.getHostName());//display the host
}
}

Java short Interview Questions

0 comments
Q.If I am extracting data from database which is have redundancy and not sorted, which collection object will you choose in order to make this data unique and sorted?
A. TreeSet
An Example
import java.util.*;

public class TreeSetExample {

public static void main(final String[] args) {
TreeSet treeset = new TreeSet();
treeset.add("Lincoln");
treeset.add("Clinton");
treeset.add("Obama");
treeset.add("Obama");
treeset.add("Regan");
treeset.add("Washington");
treeset.add("Bush");
System.out.println("TreeSet with Prez:");
Iterator iterator = treeset.iterator();
while (iterator.hasNext()) {
Object object = iterator.next();
System.out.println(object);
}
}
}

The Out put of the program is:

TreeSet with Prez:
Bush
Clinton
Lincoln
Obama
Regan
Washington

Q. Is GC a high priority thread?
A. No, it is low priority thread.

Java Interview Questions

1 comments
  1. Why would you use a synchronized block vs. synchronized method?

    A. A synchronized blocks place locks for shorter periods than synchronized methods.
  2. Can you write a Java class that could be used both as an applet as well as an application?

    A. Yes. Add a main() method to the applet.
  3. Can you call one constructor from another if a class has multiple constructors

    A. Yes. Use this() syntax.

What's the difference between the methods sleep() and wait()

0 comments
A. The code sleep(1000); puts thread aside for exactly one second. The code wait(1000), causes a wait of up to one second. A thread could stop waiting earlier if it receives the notify() or notifyAll() call. The method wait() is defined in the class Object and the method sleep() is defined in the class Thread.

Which containers use a border layout as their default layout?

0 comments
The Window, Frame and Dialog classes use a border layout as their default layout.

Monday, November 15, 2010

Does it matter in what order catch statements?

0 comments
Yes, it does.  consider the following eg:

try
{
in=Integer.parseInt(str);
}
catch(Exception e)
{
System.err.println("Error");
}
catch(IOException e)
{
System.err.println("IO error");
}
IOException is subclass of Exception. so "Exception" object can catch the IOException object.


In the above code "Exception catch:" will catch the all exception,so "IOException catch" is unused.

so in order to catch io errors by IOException , we need to change the order


try
{
in=Integer.parseInt(str);
}
catch(IOException e)
{
System.err.println("IO Error");
}
catch(Exception e)
{
System.err.println(" error");
}

Can you write a Java class that could be used both as an applet as well as an application?

0 comments
Yes It is possible.  By adding main function to the applet program you can include application program also.




eg:
import java.awt.*;
import java.applet.*;
import java.util.*;
/*
*/
public class AppApp extends Applet
{
public void init()
{

add(new TextArea("Welcome to Secure.Java.blogspot.com"));
String ar[]=new String[2];
ar[0]="Welcome to debugginquestions.blogspot.com";
main(ar);
}
public void main(String args[])
{
System.out.println(args[0]);
}

}

When is explicit Object casting needed?

0 comments
Whenever you assign super class object to sub class object , explicit object casting is needed.
If you assign sub class object to super class object, no object casting is needed.

Eg:
We know that "Object" class is super class for all class. 
so,
 Object obj=new Object( );
Customer e=(Customer) obj;

What's the difference between an interface and an abstract class?

0 comments
An abstract class may contain concrete methods(methods with body).   In case of Interface concrete methods are not allowed(methods won't have body). 

We can't inherit multiple abstract class in a class.  Java doesn't allow multiple inheritance.   On the other hand You can implement multiple interface in a class.

We can create reference and  can't create Instance for the Interface. 

Visitors

Followers

 

Java Interview Questions. Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted into Blogger Template by Bloganol dot com