Java Programming Technical Interview Questions

What is Java?

Java is a type of programming language that is quite popular. In fact, with C it is one of the most popular programming languages in use. Java was originally developed for handheld devices and set-top boxes as OAK. However, after oak was unsuccessful, it modified to incorporate the World Wide Web, and re-launched as Java by Sun Microsystems. It is developed by James Gosling.

It is a general-purpose computer programming language that incorporates concurrent, class-based, object-oriented elements. It is also designed to have as few implementation dependencies as possible. It has developed much of its syntax from C and C++, and is quite similar to C++ but is designed to be simpler in order to reduce common programming errors.

One of the primary benefits of Java is that it is intended to be a "write once, run anywhere" (WORA) program, which means that once it is compiled it should run on all platforms that support Java and without the need for recompilation.

It should be noted that Java is not the same as Javascript, in fact they are not even related. Javascript is a scripting language for use in web browsers.

Why is Java called the ‘Platform Independent Programming Language’?

Java is called a ‘Platform Independent Programming Language’ because no matter what platform it is programmed on, the code remains the same. Hence, it can be said that its coding is independent of the platform it is coded on.

Java has an inbuilt virtual machine called JVM, which when installed on the platform, allows the Java code to run as is, irrespective of which platform it is. JVM translated the byte code into machine code for the specific machine on which the Java program in running, thus allowing it to be read and run smoothly.

What is difference between JDK, JRE and JVM?

JDK, JRE and JVM are three different components that help with the running of Java coded programs. In fact, these three are termed as core concepts of Java programming language. However, they are three different concepts.

JDK stands for Java Development Kit. It provides all the tools, executables, and binaries required to compile, debug and execute a Java Program. Hence, it is a core component of Java Environment. JDK is platform specific, which means that there is a different JDK for Windows, Mac, Unix, etc. JDK can also be called a superset of JRE, as it contains JRE with Java compiler, debugger and core classes.

JRE stands for Java Runtime Environment. It is primarily used to provide a runtime environment that facilitates the implementation of JVM. It primarily contains the set of libraries and other files that JVM requires at runtime. It is platform dependent.

JVM, on the other hand, stands for Java Virtual Machine. It is an abstract machine that provides a runtime environment where the Java bytecode can be executed. JVM is responsible for loading the code, verifying the code, and executing the code with the runtime environment. Like JDK and JRE, JVM is also platform dependent, as each platform has different specifics and configurations.

Basically, JDK helps compile Java source code, whereas JVM is what actually runs the Java code, and JRE is where the code runs.

What is JIT compiler?

A JIT compiler is a just-in-time (JIT) compiler. It is a program that converts Java bytecode into native machine code that can directly be sent to the processor. This helps improve the performance of Java applications at runtime, as without the conversion, Java applications would run slower than native applications. The conversion makes the code easier to understand, hence faster to execute. It is called JIT as it compiles the code just-in-time to run. The JIT compiler is enabled by default, and is activated when a Java method is called.

A platform is any hardware or software that can be used to host or run an application or service. A typical application platform will generally include hardware, an operating system and coordinating programs, all of which will typically use the instruction set for a particular processor or microprocessor. A platform acts as a base or foundation where the object code will execute.

Differences between C, C++ and Java

C, C++ and Java are three different programming languages. C is the oldest programming language of the three. In fact, the other two are based on C. Still, each has its own advantages and disadvantages when compared with each other.

C

C++

Java

Based on Assembly Language

Based on C

Based on C and C++

Oldest

Newer as compared to C

Newest among the three

Designed for systems and applications programming

Designed for systems and applications programming

General-purpose, concurrent, class-based, object-oriented programming language

Developed by Dennis Ritchie in 1972 for Unix OS

Developed by Bjarne Stroustrup Starting In 1979 At Bell Labs

Developed by James Gosling from Sun Microsystems in 1991

Procedural Programming

Object Oriented Programming

Object Oriented Programming

Top-down approach

Bottom-up approach

Bottom-up approach

Compiled language

Compiled language

Interpreted language

Dependent on Platform

Dependent on Platform

Independent of Platform

Code executed directly

Code executed directly

Code executed by JVM

Supports 32 keywords

Supports 63 keywords

Supports 50 defined keywords

Supports union and structure data types

Supports union and structure data types

Does not support union and structure data types

Supports pre-processor directives such as #include, #define

Supports pre-processor directives such as #include, #define

Does not support pre-processor directives such as #include, #define

Supports header files

Supports header files

Uses packages

Does not support Inheritance

Supports Inheritance

Does not support multiple inheritance

Does not support Operator Overloading

Supports Operator Overloading

Does not support Operator Overloading

Supports pointers

Supports pointers

Does not support pointers

Compiled Code Translation

Compiled Code Translation

Interpreted Code Translation

Uses malloc, calloc for storage allocation

Uses new , delete for storage allocation

Uses garbage collector for storage allocation

Does not support multi-threading and interfaces

Does not support multi-threading and interfaces

Supports multi-threading and interfaces

Does not support exception handling

Supports exception handling

Supports exception handling

Does not support templates

Supports templates

Does not support templates

Supports storage classes like auto, external etc.

Supports storage classes like auto, external etc.

Does not support storage classes like auto, external etc.

Does not support Constructors or Destructors

Supports Constructors or Destructors

Does not support Constructors or Destructors

Does not support Database Connectivity

Does not support Database Connectivity

Supports Database Connectivity

Uses include statement for including the contents of screen

Uses include statement for including the contents of screen

Uses import statement for including the contents of screen

Uses goto

Uses goto

Does not goto

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

The primary difference between Java and other platforms is the fact that Java contains JVM (Java virtual machine) and Java API that run on top of other hardware based platforms. The JVM converts Java code to machine language.

Other benefits that Java has over other platforms include:

  • Java is a software only platform, whereas other platforms can be hardware only or a hardware software platform.
  • Java runs on top of hardware platform, whereas other platforms could have only the hardware component.
  • The Java API supports the writing of Java files, which are then converted to class file, i.e. byte stream by the java compiler. JVM then converts the byte stream to machine language which can then be read by any computer. Other platforms may or may not have this functionality.
  • Java is platform independent, which means that once the code is written, it can be run from anywhere. Most other platforms do not have this capability.
  • A Java programmer may develop Java code on any PC, while this functionality may not be available on other platforms.
How many types of memory areas are allocated by JVM?

There are six main types of memory areas that are allocated by JVM. These include:

1. Class loader – a subsystem of JVM that is used to load class files.

2. Class (Method) Area – stores per-class structures such as the runtime constant pool, field and method data, the code for methods.

3. Heap - is the runtime data area in which objects are allocated.

4. Stack - stores frames and holds local variables and partial results. Participates in method invocation and return.

5. Program Counter (PC) Register - contains the address of the JVM instruction that is currently being executed.

6. Native Method Stack - contains all native methods used in the application.

What is Final keyword in Java?

In Java, the final keyword is used to restrict the user. It can be used in various contexts, which ends up restricting what the user can define and use. There are three primarily ways in which a Final keyword can be applied.

  1. Variable: when a variable is made final, it becomes constant, which means that once assigned a value of the variable can never be changed.
  2. Method:  If any method is made finale, it means that it cannot be overridden by subclasses.
  3. Class: In any class is made final, it means that it cannot be extended.
Is the final method inherited?

Yes, the final method is inherited, which means that it cannot be overridden by the subclasses. Once a method is made final, it is not possible to override it. The benefit of this is that a final method is typically faster than any other method, because it is resolved at complied time.

When is the super keyword used?

In Java, the super keyword is a reference variable that is used to refer to the immediate parent class objects. Whenever the instance of a subclass is created, an instance of parent class is also implicitly created. The super reference variable refers to this parent class.

The super keyword can be used in the following manner:

  1. It can be used to refer to the immediate parent class instance variable. Here the super keyword is used to access the data member or field of parent class. However, the parent class and child class should have same fields.
  2. It can be used to invoke the immediate parent class method. It can only be used if the subclass contains the same method as the parent class. It should be used if the method is overridden.
  3. It can also be used to invoke the immediate parent class constructor. The super keyword can call both parametric as well as non-parametric constructors depending upon the situation.
What is the difference between StringBuffer and String?

Even though String and StringBuffer sound similar, they are not the same. Both are a sequence of characters that are primarily used to create and manipulate strings. However, there are certain differences between. The following are the primary differences between String and StringBuffer.

Sting

StringBuffer

String class is immutable, i.e. once created cannot be changed

StringBuffer class is mutable, i.e. once created can be changed

String is stored in the  Constant String Pool

Stringbuffer Is Stored In The Heap

String is thread safe

StringBuffer is thread safe

String cannot be used by two threads simultaneously.

String cannot be used by two threads simultaneously.

Once assigned String cannot be changed.

StringBuffer value can be changed, i.e. it can be assigned to a new value

String is slow and consumes more memory when one concat too many strings because it creates a new instance every time.

StringBuffer is fast and consumes less memory when one cancat strings.

String class overrides the equals() method of Object class.

StringBuffer class doesn't override the equals() method of Object class.

Cannot be converted.

String Buffer can be converted to the string by using toString() method.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.