Home >>Interview Questions >Java Interview Questions and Answers

Java Interview Questions and Answers

Java Interview Questions and Answers

Have you got to attend an interview of the Java? Well, there is no need to worry as we have got an astonishing collection of Java interview questions and answers that basically covers the rare basics of the Java as well as the top canopy of it. These Java questions are generally very simple and easy to understand as these are not just like the whole of the chapter notes rather than, these are just the main texts that can be used as the key notes that are used in the industry in order to judge an individual. Whether you are a beginner or experienced these Java Interview questions and answers for fresher and Java interview questions and answers for experienced will definitely help you understand the very deep aspect of it very easily. If you are a beginner or an experienced professional these core Java interview questions and answers for experienced will help you in getting all the facts and points that are missing from your brain’s library and hence, you can easily brush up your Java’s concepts. Here is the list of Java programming questions and answers for written test as well as the oral interview that are asked very frequently:

1.What do you understand by Java?

Java is basically the high-level, object-oriented, high performance, secure programming language, robust, platform-independent, Multithreaded, and portable programming language that is available to users in recent times. It has been developed by James Gosling in mid-year of 1991. Considering its features, this can also be known as the platform because of the fact that it provides its own JRE and API.

2.Explain the features that the Java Programming language possess.

There are various features of the Java Programming language that are indeed very amazing. Here are some of the features that are present in the Java Programming Language depicted below:

  • Simple: Java is known to be easy to learn. The syntax of the Java is usually based on C++ that makes it easier to write the program in this.
  • Object-Oriented: Java generally follows the object-oriented paradigm that is the reason that permits the user to maintain their code as the combination of different type of objects that usually incorporates both data and behaviour.
  • Portable: Java generally supports read-once-write-anywhere approach. That simply means that the Java users can execute the Java program on every machine. Java program (.java) is basically converted to bytecode (.class) that can be very easily made to run on every machine.
  • Platform Independent: Java is known as a platform independent programming language. It is known to be different from the other programming languages such as C and C++ that generally needs a platform in order to be executed. Whereas, Java comes with its own platform on which its code is generally executed. Java language doesn't depend upon the operating system in order to be executed.
  • Secured: Java is known to be much secured because of the fact that it doesn't use explicit pointers. Java in also known to provides the concept of ByteCode and the Exception handling that generally makes it more secured.
  • Robust: Java is known to be a strong programming language because of the fact that it uses strong memory management. The concepts such as Automatic garbage collection, Exception handling, etc. contribute it make it more robust.
  • Architecture Neutral: Java is generally called as an architectural neutral as this does not dependent on the architecture. In the C language, the size of data types can vary as per the architecture (32 bit or 64 bit) that doesn't exist in Java.
  • Interpreted: Java generally uses the Just-in-time (JIT) interpreter along with the compiler that is for the program execution.
  • High Performance: Java is known to be faster than the other traditional interpreted programming languages because of the fact that the Java bytecode is "close" to native code. This is still a little bit slower in comparison to a compiled language (e.g., C++).
  • Multithreaded: The users can write the Java programs that basically deals with many tasks at once just by defining multiple threads. The crucial advantage of the multi-threading is that this doesn't occlude memory for each of the thread. This generally shares a common memory area. Threads are meant to be important for the multi-media, Web applications, etc.
  • Distributed: Java is generally distributed because of the fact that it facilitates the users to create distributed applications in Java. RMI and EJB are basically used in order to create distributed applications. This feature of the Java generally makes the users able to access files just by calling the methods from any machine that is available on the internet.
  • Dynamic: Java is known as a dynamic language. This generally supports the dynamic loading of the classes. This means that the classes are generally loaded on demand. This also supports the functions from its native languages that are C and C++.

3.What are the main differences that are between C++ and Java?

The differences between C++ and Java are has been depicted in the following table with the difference name:

Comparison Index C++ Java
Platform-independent C++ is known to be platform-dependent language. Java is basically known to be a platform-independent.
Mainly used for C++ is generally used for system programming. Java is generally used for application programming. This is also widely used in window, web-based, enterprise and the mobile applications.
Design Goal C++ was basically designed for systems and applications programming. This was an extension of C programming language. Java was basically designed and created as an interpreter for the printing systems and somehow in the coming time it gets extended as a support network computing. This was basically designed with a goal of being easy in order to use and accessible to a broader audience.
Goto C++ generally supports the goto statement. Java is known not to support the goto statement.
Multiple inheritance C++ basically supports multiple inheritance. Java generally doesn't support the multiple inheritance through class. This can only be achieved by interfaces in java.
Operator Overloading C++ usually supports the operator overloading. Java usually doesn't support the operator overloading.
Pointers C++ basically supports pointers. The users can write pointer program in C++. Java usually supports the pointer internally. However, the users can't write the pointer program in the java. This simply means that java has restricted pointer support in the Java.
Compiler and Interpreter C++ generally uses compiler only. C++ is basically compiled and run by the help of the compiler that converts the source code into machine code so, C++ is platform dependent. Java uses compiler and interpreter both. Java source code is generally gets converted into bytecode at compilation time. The interpreter then executes this bytecode at the runtime and then it produces the output. Java is usually interpreted that is the reason why this is platform independent.
Call by Value and Call by reference C++ basically supports both the call by value and call by reference. Java is known to support call by value only. Because of the fact there is no call by reference in java.
Structure and Union C++ is known to supports structures and unions. Java generally doesn't support structures and unions.
Thread Support C++ basically doesn't have a built-in support for the threads. This only relies on the third-party libraries for thread support. Java generally has a built-in thread support.
Documentation comment C++ basically doesn't support the documentation comment. Java is known to support the documentation comment (/** ... */) that is used to create documentation for java source code.
Virtual Keyword C++ is known to support the virtual keyword that is for the reason that the users can decide whether or not override a function. Java generally does not has any virtual keyword. The users can generally override all the non-static methods by default. In simple words, non-static methods are generally virtual by default.
unsigned right shift >>> C++ is known to doesn't support the >>> operator. Java basically support the unsigned right shift >>> operator that is used to fill the zero at the top for the negative numbers. And for the positive numbers, this works same like >> operator.
Inheritance Tree C++ is known to create a new inheritance tree always. Java generally uses a single inheritance tree always because of the fact that all the classes are the child of Object class in the java language. The object class is basically the root of the inheritance tree in java.
Hardware C++ is known to be nearer to hardware. Java language is not known to be so interactive with hardware.
Object-oriented C++ is basically an object-oriented language. However, in the C language, occurrence of the single root hierarchy is not possible. Java is also known as an object-oriented language. However, everything in the Java (except fundamental types) is an object in Java. This is a single root hierarchy as here everything gets derived from java.lang.Object.

4.What are the differences that are between an Inner Class and a Sub-Class?

An Inner class in Java language is basically a class that is known to be nested within another class. An Inner class in Java has the access rights for the class that is nesting it and this has the ability to access all the variables and the methods that are defined in the outer class of the Java language.

A sub-class in the Java language is however a class that basically inherits from another class that is known as super class. Sub-class have the ability to access all the public and protected methods and fields of its super class.

5.What are the various access specifiers that are for the Java classes?

In the Java language, the access specifiers are basically the keywords that are used before a class name that is known to define the access scope. There are various types of access specifiers for the classes that has been depicted below:

  • Public: Class, Method, Field is generally accessible from anywhere.
  • Protected: Method, Field can generally be accessed from the same class to which they usually belong or from the sub-classes and from the class of exactly same package but they cannot be accessed from outside.
  • Default: Method, Field, class can basically be accessed only from the same package and cannot be accessed from outside of its native package.
  • Private: Method, Field can generally be accessed from the exact same class to which they usually belong.

6.What do you mean by a singleton class?

A singleton class in the java language can have only one of the instance and hence all of its methods and variables generally belong to just one of the instance. Singleton class concept is usually useful for the situations that generally requires a need to limit the number of the objects for a class.

The perfect example of the singleton usage scenario is basically whenever there is a limit of possessing only one connection in to a database that is due to some of the driver limitations or because of any of the licensing issues.

7.What are the Loops in the Java? Describe the three types of loops?

Looping in Java is basically used in programming in order to execute a statement or a block of the statement repeatedly. There are basically three types of loops in the Java that have been depicted below:

For loops are basically used in java in order to execute the statements repeatedly for a provided number of times. For loops are usually used when the number of times of being executed of the statements is known to the programmer.

While loop in Java is basically used whenever certain statements are needed to be executed repeatedly until and unless a condition is being fulfilled. In the while loops, condition is generally checked first beforehand execution of statements starts.

Do While Loop in the Java is generally same as the While loop just with only difference that is condition is generally checked after the execution of block of statements. Hence, in case of the do while loop, the statements are generally executed at least once.

  • For Loops
  • While Loops
  • Do While Loops

8.What do you mean by the Java virtual machine?

Java Virtual Machine is basically a virtual machine as the name suggests that generally enables the computer in order to run the Java program. JVM is usually known to act like a run-time engine that basically calls the main method that are present in the Java code. JVM is also the specification that must be implemented in the computer system to perform well. The Java code is basically compiled by the JVM in order to be a Bytecode that is basically machine independent and is close to the native code.

9.What is the main difference that is present between JDK, JRE, and JVM?

JVM

JVM is basically an acronym for the Java Virtual Machine; this is also an abstract machine that delivers the runtime environment in which the Java bytecode can be operated. This is basically a specification that specifies the working anatomy of the Java Virtual Machine. The implementation of this has been basically delivered by the Oracle and various other companies. The implementation of this is also known as JRE. JVMs are usually available for many of the hardware and software platforms (Hence, JVM is platform dependent). This is basically a runtime instance that has been created when the user run the Java class. There are generally three notions of the JVM that are specification, implementation, and instance.

JRE

JRE generally elaborates to Java Runtime Environment. This is usually the implementation of the JVM. The Java Runtime Environment is usually known as a set of software tools that are used for the purpose of developing Java applications. This is usually used in order to provide the runtime environment. This is basically the implementation of the JVM and it has been known to exist physically. This generally consists of a set of libraries with addition to other files that generally JVM uses at the runtime.

JDK

JDK generally elaborates to the Java Development Kit. This is a software development environment that is basically used in order to develop the Java applications and the applets and this exists physically. This generally consists of the JRE in addition to the development tools. JDK is also known as an implementation of any one of the below mentioned Java Platforms that are released by the Oracle Corporation:

  • Standard Edition Java Platform
  • Enterprise Edition Java Platform
  • Micro Edition Java Platform

10.Explain the types of the memory areas that are allocated by the JVM?

There are various type of memory areas that are generally allocated by the JVM in the Java language. Here are some of those memory areas that are mentioned below:

  • Class(Method) Area: Class Area is generally used to store the per-class structures like the runtime field, constant pool, method data, and the code for methods.
  • Heap: This is basically the runtime data area in which the memory is usually allocated to the objects
  • Stack: Java Stack is used to store the frames. It is known to hold the local variables and the partial results and it plays a part in the method invocation and return. Each of the thread basically has a private JVM stack that is created at the exact same time as the thread. A very new frame is also used to be created each time a method is being invoked. A frame is usually used to be destroyed whenever its method invocation is completed.
  • Program Counter Register: PC (program counter) register is known to contain the address of the Java virtual machine instruction that are currently being executed.
  • Native Method Stack: This generally consists of all the native methods that are used in the application.

11.What do you understand by the JIT compiler?

Just-In-Time(JIT) compiler is basically used in order to improve the performance of the code. JIT is generally used in order to compile the parts of the bytecode that generally have the similar functionality at the same time therefore it reduces the amount of the time that is needed for the compilation. Here are the term “compiler” that generally refers to a translator from which the instruction set of a Java virtual machine (JVM) to the instruction set of a very specific CPU.

12.What is the platform in Java Language?

A platform is known to be the hardware or software environment in which a piece of the software is generally executed. There are basically two types of platforms that are software-based and hardware-based. Java is known to deliver the software-based platform.

13.What are the main differences that are between the Java platform and the other platforms?

There are various differences that are present in the Java platforms and other platforms but here are some of the important differences that has been depicted below:

Java is basically the software-based platform on the other hand the other platforms may be only the hardware platforms or the software-based platforms.

Java is known to be executed on the top of the other hardware platforms on the other hand the other platforms may only possess the hardware components.

14.What is the thing that gives Java its 'write once and run anywhere' nature?

The bytecode. Java compiler is known to convert the Java programs into the class file (Byte Code) that is the intermediate language that is present between the source code and the machine code. This type of bytecode is usually not the platform specific and it can be executed on any of the computer.

15.What do you mean by the classloader?

Classloader is basically a subsystem of the JVM that is generally used in order to load the class files. Whenever the programmer runs the java program then it is first gets loaded by the classloader. There are basically three types of built-in classloaders in the Java language that has been depicted below:

  • Bootstrap ClassLoader: This is basically the first classloader that is generally known as the superclass of Extension classloader. This is used to load the rt.jar file that consists of all the class files of the Java Standard Edition such as java.lang package classes, java.io package classes, java.net package classes, java.util package classes, java.sql package classes, etc.
  • Extension ClassLoader: This is generally known as the child classloader of the Bootstrap and the parent classloader of the System classloader. This is known to load the jar files that are located inside the $JAVA_HOME/jre/lib/ext directory.
  • System/Application ClassLoader: This is basically the child classloader of the Extension classloader. It is used to load the class files that are from the classpath. By default, the classpath is usually set to the current directory. The users or the programmers can change the classpath by the help of "-cp" or "-classpath" switch. This is also called as Application classloader.

16.What is the main purpose of the static methods and the variables in Java?

The methods or the variables are generally defined as the static that are usually shared among all the objects of the class of the Java language. The static is basically the part of the class in the Java and not of the object. The static variables are generally known to be stored in the class area, and the user do not need to create the object in order to access such variables. Therefore, the static is generally used in the case, where the programmer feel a need to define the variables or the methods that are very common to all of the objects of the class in Java.

17.What are the various advantages of the Packages in the Java language?

There are various advantages of defining the packages in the Java language. Here are some of the advantages that have been mentioned below:

  • Packages are known to avoid the name clashes in the Java language.
  • The Package is the one that delivers easier access control.
  • The programmers can also possess the hidden classes that are generally not visible to the outside and used by the package.
  • This is very easier to locate the related classes in the Java language.

18.What do you mean by the object-oriented paradigm in the Java Language?

It is basically a programming paradigm that is generally based on the objects that possess the data and the methods that are defined in the class to which it basically belongs. Object-oriented paradigm generally aims to incorporate the advantages of the modularity and the reusability. Objects are basically known as the instances of the classes that interacts with one another in order to design the applications and various programs. There are various features that belongs to the object-oriented paradigm, some of them has been depicted below:

  • This paradigm generally follows the bottom-up approach in the program design in the Java language.
  • This paradigm is known to focus on the data with the methods in order to operate upon the object's data.
  • This paradigm also includes the concept that is like Encapsulation and the abstraction that generally hides the complexities that are from the user and it shows only the functionality.
  • This is known to implement the real-time approach just like inheritance, abstraction, etc.
  • The various examples of the object-oriented paradigm that are available on the web are C++, Simula, Smalltalk, Python, C#, etc.