HOME RESOURCES PLACEMENTS CONTACT FEEDBACK SUBSCRIBE
QUICK LINKS .NET Articles
.NET Training Tutorial
HR Interview Questions
.NET Interview Questions
SQL Interview Questions
JAVA Interview Questions

JAVA Interview Questions 1 - 5
Back to the list of all Java Interview Questions
Question 1: What is the main difference between the Java platform and the other software platforms?
Java platform is a software-only platform, which runs on top of other hardware-based platforms like UNIX, NT etc.
The Java platform has 2 components:
  • Java Virtual Machine (JVM) – ‘JVM’ is a software that can be ported onto various hardware platforms. Byte codes are the machine language of the JVM.
  • Java Application Programming Interface (Java API) – set of classes written using the Java language and run on the JVM.

Question 2: What is the difference between C++ and Java?
  • 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 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 nonmemory 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
  • 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.
Question 3: What are the usages of Java packages?
It helps resolve naming conflicts when different packages have classes with the same names. This also helps you organize files within your project.

For example: java.io package do something related to I/O and java.net package do something to do with network and so on. If we tend to put all .java files into a single package, as the project gets bigger, then it would become a nightmare to manage all your files.

You can create a package as follows with package keyword, which is the first keyword in any Java program followed by import statements. The java.lang package is imported implicitly by default and all the other packages must be explicitly imported.

package com.xyz.client ;
import java.io.File;
import java.net.URL;


Question 4: Explain Java class loaders? If you have a class in a package, what do you need to do to run it? Explain dynamic class loading?
Class loaders are hierarchical. Classes are introduced into the JVM as they are referenced by name in a class that is already running in the JVM. So, how is the very first class loaded? The very first class is especially loaded with the help of static main( ) method declared in your class. All the subsequently loaded classes are loaded by the classes, which are already loaded and running. A class loader creates a namespace. All JVMs include at least one class loader that is embedded within the JVM called the primordial (or bootstrap) class loader. Now let’s look at non-primordial class loaders. The JVM has hooks in it to allow user defined class loaders to be used in place of primordial class loader. Let us look at the class loaders created by the JVM.

Class Loader Reloadable? Explanantion
BootStrap No Loads JDK internal classes, java.* packages. (as defined in the sun.boot.class.path system property, typically loads rt.jar and i18n.jar)
Extensions No Loads jar files from JDK extensions directory (as defined in the java.ext.dirs system property – usually lib/ext directory of the JRE)
System No Loads classes from system classpath (as defined by the java.class.path property, which is set by the CLASSPATH environment variable or –classpath or –cp command line options)
Class loaders are hierarchical and use a delegation model when loading a class. Class loaders request their parent to load the class first before attempting to load it themselves. When a class loader loads a class, the child class loaders in the hierarchy will never reload the class again. Hence uniqueness is maintained. Classes loaded by a child class loader have visibility into classes loaded by its parents up the hierarchy but the reverse is not true as explained in the above diagram.

Question 5: What do you need to do to run a class with a main() method in a package?
Example: Say, you have a class named “Pet” in a project folder “c:\myProject” and package named com.xyz.client, will you be able to compile and run it as it is?

package com.xyz.client;

public class Pet

{

    public static void Main(String[] args)

    {

        System.out.println("I am found in the classpath");

    }

}
To run -> c:\myProject> java com.xyz.client.Pet
The answer is no and you will get the following exception: “Exception in thread "main" java.lang.- NoClassDefFoundError: com/xyz/client/Pet”. You need to set the classpath. How can you do that? One of the following ways:
  • Set the operating system CLASSPATH environment variable to have the project folder “c:\myProject”.
  • Set the operating system CLASSPATH environment variable to have a jar file “c:/myProject/client.jar”, which has the Pet.class file in it.
  • Run it with –cp or –classpath option as shown below:
    c:\>java –cp c:/myProject com.xyz.client.Pet

    Or

    c:\>java -classpath c:/myProject/client.jar com.xyz.client.Pet
Important: Two objects loaded by different class loaders are never equal even if they carry the same values, which mean a class is uniquely identified in the context of the associated class loader. This applies to singletons too, where each class loader will have its own singleton.

Back to the list of all Java Interview Questions