Interview Questions : Java Fundamentals
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.










This article explains what are Java Annotations, How to define and use Java Annotation and how Java Annotations works. Java annotations are meta data provides data about a program that is not part of the program itself. They have no direct impact on the operations of the code they annotate. An annotation indicates that the declared element( can be field, class, method, package etc..,) should be processed in some special way by a compiler, development tool, deployment tool, or during runtime. To develop APIs require a fair amount of coding. For example, in order to write a JAX-RPC web service, we must provide a paired interface and implementation. This code could be generated automatically by a tool if the program were “decorated” with annotations indicating which methods were remotely accessible.