Tuesday, May 21, 2013

Java Interview Questions and Answers Part 14

Q121. Can you make an instance of an abstract class? For example - java.util.Calender is an abstract class with a method getInstance() which returns an instance of the Calender class.

No! You cannot make an instance of an abstract class. An abstract class has to be subclassed. If you have an abstract class and you 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.

Q122. 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 is necessary to ask back 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

Q123. 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.

Q124. Why Java does not support pointers?

Because pointers are unsafe. Java uses reference types to hide pointers and programmers feel easier to deal with reference types without pointers. This is why Java and C-sharp shine.

Q125. Parsers? DOM vs SAX parser

Parsers are fundamental xml components, a bridge between XML documents and
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.
DOM
  1.  Tree of nodes
  2.  Memory: Occupies more memory, preffered for small XML documents
  3.  Slower at runtime
  4.  Stored as objects
  5.  Programmatically easy
  6.  Ease of navigation
SAX
  1.  Sequence of events
  2.  Doesn't use any memory preferred for large documents
  3. Faster at runtime
  4.  Objects are to be created
  5.  Need to write code for creating objects
  6.  Backward navigation is not possible as it sequentially processes the document

Q126. Can you declare a class as private?

Yes, we can declare a private class as an inner class. For example,
class MyPrivate {
  private static class MyKey {
    String key = "12345";
  }
  public static void main(String[] args) {
    System.out.println(new MyKey().key);//prints 12345
  }
}

Q127. What is the difference between shallow copy and deep copy?


Shallow copy shares the same reference with the original object like cloning, whereas the deep copy get a duplicate instance of the original object. If the shallow copy has been changed, the original object will be reflected and vice versa.

Q128. Can one create a method which gets a String and modifies it?

No. In Java, Strings are constant or immutable; their values cannot be changed after they are created, but they can be shared. Once you change a string, you actually create a new object. For example:
String s = "abc"; //create a new String object representing "abc"
s = s.toUpperCase(); //create another object representing "ABC"

Q129. Why is multiple inheritance not possible in Java?

It depends on how you understand "inheritance". Java can only "extends" one super class, but can "implements" many interfaces; that doesn't mean the multiple inheritance is not possible. You may use interfaces to make inheritance work for you. Or you may need to work around. For example, if you cannot get a feature from a class because your class has a super class already, you may get that class's feature by declaring it as a member field or getting an instance of that class. So the answer is that multiple inheritance in Java is possible.

Q130. What's the difference between constructors and other methods?

Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times.

No comments:

Post a Comment

Automatic Traffic Exchange

YallaTech Facebook page