Archive

Posts Tagged ‘java interview questions and answers’

Interview Questions : Java Fundamentals

June 30th, 2009

1. Give a few reasons for using Java?

a.  Java is a fun language. Let’s look at some of the reasons:

  • Built-in support for multi-threading, socket communication, and memory management (automatic garbage collection).
  • Object Oriented (OO).
  • Better portability than other languages across operating systems.
  • Supports Web based applications (Applet, Servlet, and JSP), distributed applications (sockets, RMI. EJB etc) and network protocols (HTTP, JRMP etc) with the help of extensive standardised APIs (Application Program Interfaces).

2. What is the main difference between the Java platform and the other software platforms?

a.  Java platform is a software-only platform, which runs on top of other hardware-based platforms like UNIX, NT etc. ava Virtual Machine (JVM) – ‘JVM’ is a software that can be ported onto various hardware platforms.  Byte codes are the machine language of the JVM.

3. What is the difference between C++ and Java?

a.  Both C++ and Java use similar syntax and are Object Oriented, but:

  • Java does not support pointers. Pointers are inherently tricky to use and troublesome.
  • Java does not support multiple inheritances because it causes more problems than it solves. Instead Java supports multiple interface inheritance, which allows an object to inherit many method signatures from different interfaces with the condition that the inheriting object must implement those inherited methods. The multiple interface inheritance also allows an object to behave polymorphically on those methods.
  • Java does not support destructors but rather adds a finalize() method. Finalize methods are invoked by the garbage collector prior to reclaiming the memory occupied by the object, which has the finalize() method. This means you do not know when the objects are going to be finalized. Avoid using finalize() method to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these resources through the finalize() method.
  • Java does not include structures or unions because the traditional data structures are implemented as an object oriented framework (Collection Framework)
  • All the code in Java program is encapsulated within classes therefore Java does not have global variables or functions.
  • C++ requires explicit memory management, while Java includes automatic garbage collection.

4. What are the advantages of Object Oriented Programming Languages (OOPL)?

a. The Object Oriented Programming Languages directly represent the real life objects like Car, Jeep, Account, Customer etc. The features of the OO programming languages like polymorphism, inheritance and encapsulation make it powerful.

[Tip: remember pie which, stands for Polymorphism, Inheritance and Encapsulation are the 3 pillars of OOPL]

5. What is the difference between aggregation and composition?

a.  Aggregation is an association in which one class belongs to a collection. This is a part of a whole relationship where a part can exist without a whole.
For example a line item is a whole and product is a part. If a line item is deleted then corresponding product need not be deleted. So aggregation has a weaker relationship.

Composition is an association in which one class belongs to a collection. This is a part of a whole relationship where a part cannot exist without a whole. If a whole is deleted then all parts are deleted.
For example An order is a whole and line items are parts. If an order deleted then all corresponding line items for that order should be deleted. So composition has a stronger relationship.

6. How does the Object Oriented approach improve software development?

a. The key benefits are:

  • Re-use of previous work: using implementation inheritance and object composition.
  • Real mapping to the problem domain: Objects map to real world and represent vehicles, customers, products etc: with encapsulation.
  • Modular Architecture: Objects, systems, frameworks etc are the building blocks of larger systems.

The increased quality and reduced development time are the by-products of the key benefits discussed above. If 90% of the new application consists of proven existing components then only the remaining 10% of the code
have to be tested from scratch.

7. Why there are some interfaces with no defined methods (i.e. marker interfaces) in Java?

a. The interfaces with no defined methods act like markers. They just tell the compiler that the objects of the classes implementing the interfaces with no defined methods need to be treated differently. Example Serializable, Cloneable etc

8. When is a method said to be overloaded and when is a method said to be overridden?
a. Method Overloading :

  • Overloading deals with multiple methods in the same class with the same name but different method signatures.

class MyClass {
public void initParams(int param) {…}
public void initParams(int param1, long param2)
{ … }
}

  • Both the above methods have the same method names but different method signatures, which mean the methods are overloaded.
  • Overloading lets you define the same operation in different ways for different data.

Method Overriding:

  • Overriding deals with two methods, one in the parent class and the other one in the child class and has the same name and signatures.

class BaseClass{
public void initParams(int param) {…}
}
class MyClass extends BaseClass {
public void initParams(int param) { …}
}

  • Both the above methods have the same method names and the signatures but the method in the subclass MyClass overrides the method in the superclass BaseClass.
  • Overriding lets you define the same operation in different ways for different object types.

9. What is the main difference between an ArrayList and a Vector? What is the main difference between Hashmap and Hashtable?

a. Vector/Hashtable: Original classes before the introduction of Collections API. Vector & Hashtable are synchronized. Any method that touches their contents is thread-safe.
ArrayList/Hashmap: So if you don’t need a thread safe collection, use the ArrayList or Hashmap. Why pay the price of synchronization unnecessarily at the expense of performance degradation.

10. So which is better?

a. As a general rule, prefer ArrayList/Hashmap to Vector/Hashtable. If your application is a multithreaded application and at least one of the threads either adds or deletes an entry into the collection then use new Java collection API‘s external synchronization facility as shown below to temporarily synchronize your collections as needed:

Map myMap = Collections.synchronizedMap (myMap);
List myList = Collections.synchronizedList (myList);

Java arrays are even faster than using an ArrayList/Vector and perhaps therefore may be preferable. ArrayList/Vector internally uses an array with some convenient methods like add(..), remove(…) etc.

S.Chandru Java , , , , ,

interview Questions : Java

June 15th, 2009

1. what are the Native methods in Java?

a. When you want to call functions or subroutines which are written in some other language other than Java like C, C++ or VB6 etc.., These methods are Native methods in Java.

2. Explain Garbage Collection in Java?

a. Garbage collection is the process of automatically freeing object that are no longer referenced by the program. This frees the programmer from having to keep track of when to free allocated memory, thereby preventing many bugs. Thus making programmers more productive as they can put more effort in coding rather than worrying about memory management. The only disadvantage of garbage collector is it adds overheads. Because the JVM has to keep a constant track of the objects which are not referenced and then free these unreferenced objects on fly. This whole process has slight impact on the application performance.

3. How can we force the garbage collector to run?

a. Garbage collector can be run forcibly using “System.gc()” or “Runtime.gc()”

4. What is use of method “finalize()” in Java?

a. Sometimes the objects needs to perform some action before the object is destroyed. For instance object is holding some non-java resources such as file handle which you might want to make sure are released before the object is destroyed. For such conditions java provides mechanism called finalization. You can add finalizer in a class by simply defining the “finalize()” method. JVM calls that method whenever it is about to recycle an object of that class.

5. Are String objects immutable, can you explain the concept?

a. Yes String object is immutable, Once you have assigned a String a value, that value can never change. Lets try to understand the concept by the below code snippet

String s1 = “hello”;

s1 = s1 + ” World”;

The VM took the value of String s (which was “hello”), and tacked ” world” onto the end, giving us the value “abcdef more stuff”. Since Strings are immutable, the VM couldn’t stuff this new value into the old String referenced by s1, so it created a new String object, gave it the value “hello world”,and made s1 refer to it. At this point in our example, we have two String objects: the first one we created, with the value “hello”, and the second one with the value ” world”. Technically there are now three String objects, because the literal argument to concat, ” world”, is itself a new String object.

6. Why String objects are immutable in Java?

a.To make Java more memory efficient, the JVM sets aside a special area of memory called the “String constant pool.” When the compiler encounters a String literal, it checks the pool to see if an identical String already exists. If a match is found, the reference to the new literal is directed to the existing String, and no new String literal object is created. (The existing String simply has an additional reference.) Now we can start to see why making String objects immutable is such a good idea. If several reference variables refer to the same String without even knowing it, it would be very bad if any of them could change the String’s value.

You might say, “Well that’s all well and good, but what if someone overrides the String class functionality; couldn’t that cause problems in the pool?” That’s one of the main reasons that the String class is marked final. Nobody can override the behaviors of any of the String methods, so you can rest assured that the String objects you are counting on to be immutable will, in fact, be immutable.

7. What is StringBuffer class and how does it different from String class?

a. StringBuffer is a peer class of String that provides almost all functionality of Strings. String represent fixed-length, immutable character sequences. Comparatively StringBuffer represents mutable, growable and write-able character sequences. But StringBuffer does not create new instance as String so it’s more efficient when it comes to intensive concatenation operation.

8. What is the difference between StringBuilder and StringBuffer class?

a. StringBuffer is synchronized and StringBuilder is not.

9. What are the packages?

a. Packages group related class and interfaces together and thus avoiding any name conflicts. Classes are group together in package using “package” keyword.

10. What is use of JAVAP tool?

a. “javap” disassembles compiled Java files and spits out the representation of the Java program. This is a useful option when the original source code is unavailable.

S.Chandru Java ,