Java Questions  «Prev  Next»

Java Interview Questions and Answers

Java Language
  1. Can a private method of a parent class be declared within a subclass?
    No! A private field, method, or inner class belongs to its declared class and is hidden from subclasses.
    It is not possible for private elements to be overridden using the feature of polymorphism.
  2. Why doesn't Java support multiple inheritence ?
    Java DOES support multiple inheritance via interfaces.

  3. What is the difference between final, finally, and finalize?
    1. final - declares a constant
    2. finally - handles exception
    3. finalize - helps in garbage collection
  4. Where and how can you use a private constuctor.
    A private constructor can be used if you do not want any other class to instanstiate the object.
    The instantiation is done from a static public method, and this method is used when dealing with the factory method pattern, when the designer only wants one controller (factory method ) to create the object .
  5. In System.out.println(), what is System, out, and println?
    is a predefined final class, out is a PrintStream object, and println is a built-in overloaded method in the out object.
  6. What is meant by "Abstract Interface"?
    First, an interface is abstract. That means, the interface cannot be implemented.
    All the methods declared in an interface are abstract methods or signatures of the methods.
  7. Can you make an instance of an abstract class?
    For example
    is an abstract class with a method getInstance() which returns an instance of the Calender class.
    An instance of an abstract class cannot be created.
    An abstract class has to be sub-classed.
    If you have an abstract class and want to use a method which has been implemented,
    you may need to subclass that abstract class, instantiate your subclass and then call that method.
  8. What is the output of x<y? a:b = p*q when x=1,y=2,p=3,q=4?
    When this kind of question has been asked, find the problems you think are necessary to ask before you give an answer.
    Ask if variables a and b have been declared or initialized.
    If the answer is yes. You can say that the syntax is wrong.
    If the statement is rewritten as: x<y? a:(b=p*q); the return value would be variable a because the x is 1 and less than y = 2;
    the x < y statement return true and variable a is returned.
  9. What is the difference between Swing and AWT components?
    AWT components are heavy-weight, whereas Swing components are lightweight.
    Heavy weight components depend on the local windowing toolkit.
    For example, java.awt.Button is a heavy weight component, when it is running on the Java platform for Unix platform, it maps to a real Motif button.
  10. For what reason does Java not support pointers?
    Because pointers are unsafe. Java uses reference types to hide pointers and programmers feel it is easier to deal with reference types without pointers.
  11. How do the DOM and SAX parsers differ?
    Parsers are fundamental XML components, and represent a bridge between XML documents and the applications that process that XML.
    The parser is responsible for handling XML syntax, checking the contents of the document against constraints established in a DTD or Schema.
    1. Tree of nodes 1. Sequence of events
    2. Memory: Occupies more memory 2. Does use any memory preferred for small XML documents preferred for large documents
    3. Slower at runtime 3. Faster at runtime
    4. Stored as objects x 4. Objects are to be created
    5. Programmatically easy, since 5. Need to write code for creating objects objects are to reffered
    6. Ease of navigation 6. Backward navigation is not possible as it sequentially processes the document