Java interview ask question

Question: Why Java does not support multiple inheritence ?
Answer: Java DOES support multiple inheritance via interface implementation.


Question:What is the difference between final, finally and finalize?
Answer: o final - declare constant
o finally - handles exception
o finalize - helps in garbage collection


Question: Where and how can you use a private constructor.
Answer: 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, this method is used when dealing with the factory method pattern when the designer wants only one controller (fatory method ) to create the object.


Question: In System.out.println(),what is System,out and println,pls explain?
Answer: System is a predefined final class,out is a PrintStream object and println is a built-in overloaded method in the out object.


Question: What is meant by "Abstract Interface"?
Answer: First, an interface is abstract. That means you cannot have any implementation in an interface. All the methods declared in an interface are abstract methods or signatures of the methods.


Question: 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.
Answer: No! You cannot make an instance of an abstract class. An abstract class has to be sub-classed. 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.


Question: Why Java does not support pointers?
Answer: 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# shine.

Question: Does it matter in what order catch statements for FileNotFoundException and IOExceptipon are written?

Answer: Yes, it does. The FileNoFoundException is inherited from the IOException. Exception's subclasses have to be caught first.

Question: Can an inner class declared inside of a method access local variables of this method?

Answer: It's possible if these variables are final.

Question: What's the main difference between a Vector and an ArrayList

Answer: Java Vector class is internally synchronized and ArrayList is not.

Question: When should the method invokeLater()be used?

Answer: This method is used to ensure that Swing components are updated through the event-dispatching thread.

Question: How can a subclass call a method or a constructor defined in a superclass?

Answer: Use the following syntax: super.myMethod(); To call a constructor of the superclass, just write super(); in the first line of the subclass's constructor.

For senior-level developers:

Question: What's the difference between a queue and a stack?

Answer: Stacks works by last-in-first-out rule (LIFO), while queues use the FIFO rule

Question: You can create an abstract class that contains only abstract methods. On the other hand, you can create an interface that declares the same methods. So can you use abstract classes instead of interfaces?

Answer: Sometimes. But your class may be a descendent of another class and in this case the interface is your only option.

Question: What comes to mind when you hear about a young generation in Java?

Answer: Garbage collection.

Question: What comes to mind when someone mentions a shallow copy in Java?

Answer: Object cloning.

Question: If you're overriding the method equals() of an object, which other method you might also consider?

Answer: hashCode()

Question: You are planning to do an indexed search in a list of objects. Which of the two Java collections should you use: ArrayList or LinkedList?

Answer: ArrayList

Question: How would you make a copy of an entire Java object with its state?

Answer: Have this class implement Cloneable interface and call its method clone().

Question: How can you minimize the need of garbage collection and make the memory use more effective?

Answer: Use object pooling and weak object references.

Question: There are two classes: A and B. The class B need to inform a class A when some important event has happened. What Java technique would you use to implement it?

Answer: If these classes are threads I'd consider notify() or notifyAll(). For regular classes you can use the Observer interface.

Question: What access level do you need to specify in the class declaration to ensure that only classes from the same directory can access it?
Answer: You do not need to specify any access level, and Java will use a default package access level .

1 comment

Popular posts from this blog

Yield Price Sri Lanka - Android Application

Manage SOAPAction of the Out Message

How to clean Registry log (REG_LOG) table