by Dinesh Thakur Category: All Interview Questions

Q. What is Java?

Java is a high-level, object-oriented, secure programming language. It was created and developed in 1991 by a man named James Gosling. Java is designed to represent the slogan “WORA” - “write once, run anywhere”. It is known for being flexible and operating with high performance.

Q. Is Java fully object-oriented?

It is not entirely object-oriented. Java uses some data types (char, byte, and float) that are not objects.

Q. How many types of java variables.

In java programming, we will recognize three types of variables:

Local Variables - those are variables that can only be used by the program blocks that wrap it or inside it. For example, for a method, etc.

Class Variables (Static Variables) - variables owned by the class (marked by static keywords), meaning that they can be used immediately without creating objects.

Instance Variables (Non-static Variables) - i.e., variables that are owned by the object, meaning you must create the object before you can use the variable.

Q. How many types of Java Modifiers.

Basically there are two types of modifiers in java, namely:

Access Modifiers − default, public , protected, private.
Non-access modifiers - final, abstract.

Q. What is Java Virtual Machine?

The java virtual machine (JVM) is an imaginary machine that emulates an application on a real computer. The JVM allows the portability of Java code; this is because all Java code is compiled to an intermediate format, bytecode, this format is then interpreted by the JVM.
There are several JVMs each intended for a type of operating system (Windows, Linux, Mac, etc.), thus being the Java application code, bytecode, a code interpreted by the JVM, we can develop an application without worrying where it will be executed because we know that if the JVM is installed, our code will be executable.

Q. Is it possible to run a program without the ‘main ()’ method?

Yes, it is possible to do that. One of the most common ways to run a program like this is using a static block.

Q. Name some significant Java resources.

For beginner programmers and programmers, the most essential attribute is simplicity. Java considered very simple to learn and grasp, particularly in comparison to other programming languages. It's a result of an interpreter referred to as a JVM - that interpreter is set up with Java itself and always provides your computer with the most recent security updates in the world wide web.
Moreover, Java is completely portable. Due to this"WORA" principle where Java constructed, it may be downloaded and applied to any device that you need - it's uniquely flexible and multifunctional.

Q. What are the ‘access specifiers’ in Java?

 They are as follows: public, private, protected and standard.

Public specifiers allow access to any class or by any method (hence its name). In contrast, private specifiers allow access only within the specified class. Protected allows access that somehow correlates with the class - either from within the class, from a subclass or only from the same package. Finally, default means the default scope (default) and allows access only from the same package.

Q. What is a ‘constructor’?

A “constructor” is a piece of code that initializes a specific object. Java has two types of constructors - a standard and a parameterized one.

Q. What is an ‘object’?

An object in Java has a state and behaviour. The most common and understandable definition of an object is that it is an instance of a class.

Q. What is 'association' in Java?

Association is when all objects have their life cycles, and no specific owner exists. It can vary between "one" and "many".

Q What is 'cloning of objects'?

The "object cloning" command is used to make an identical copy of the object. It can be done using the clone () method of this object class.

Q. What references to different objects can exist in Java?

In Java, all object references are null.

Q. What is Garbage Collection?

Many programming languages ​​allow us to allocate space in memory at run time; once the program has ended there must be a way to free up this space so that other applications can use it.
In many programming languages, including C and C ++, the responsibility for freeing up space that will no longer be used rests with the programmer, however, it’s not always easy to manage what’s and what’s not being used, poor memory management often causes stack overflow (stack overflow) among other problems.
In the Java programming language, the responsibility for memory management rests with the Garbage Collector, thus Java programmers they are free from the worry of allocating and deallocating memory.
The Garbage Collector is a process that runs in the background and is responsible for releasing memory allocated by variables that will no longer be used by the application.

Q. What are the differences between JDK, JVM and JRE?

The JDK is a Java development kit. It is the primary tool in Java used to compile programs in that language. Within the package, it contains all the tools necessary to start using it.

I mentioned the JVM before - it means Java Virtual Machine. It is a machine that creates the environment in which the Java bytecode can function correctly.

The JRE is the Java Runtime Environment. It is the type of environment that the JVM provides - it allows the Java bytecode to run and function correctly.
Q. What are the Phases of a Java program?

The phases that a Java program went through are related to the following form:

1. Creation of the source code (;
2. Compilation of the source code and generation of the bytecode (Program.class);
3. Interpretation of the bytecode by the virtual machine;
4. Conversion of bytecode into machine language.

Q. What is hotspot in Java?

A hotspot is the Java virtual machine; it provides some very functional features important. Unlike other applications, which are compiled directly for code of the machine on which they will be executed, in Java, these are only transformed into runtime machine code when needed.

At first, this approach brought problems of overload and slowness of systems; however, the JVM has been improving. In many situations, Java applications perform similarly to applications that are previously compiled.

This performance has been improving a lot due to the optimization that a virtual machine can do as the code is executed. Realize that when we program in C, the source code is transformed into a code machine immediately.

In principle, we can think that the fact that the program does not need to pass one more step, interpretation, will make it more efficient. Still, the static compilation often fails to predict situations that will occur during code execution: most used application parts, system load, number of simultaneous users, available memory, etc.

This information, relating to the environment in which the application is being performed, is used by the JVM to make runtime optimizations. If necessary, the code that is being interpreted is transformed into native operating system instructions (machine code) in the process of dynamic build.

JIT, a Just-in-time compiler, perform this runtime transformation. The fact that the code (bytecode) is transformed, at runtime, into machine code allows the JVM to change the build strategy in search of better performance, in a continuous “learning” cycle.

Q What is the difference between JRE and JDK?

JRE: The Java Runtime Environment contains everything that a user frequent need to run a Java application (JVM and libraries), as the name itself says it is the “Java execution environment”;
JDK: The Java Development Kit has composed of the JRE and a set of useful tools for the Java developer.

Q. What are the components of the JDK?

The following is a brief description of the main tools that are part of the JDK:

javac: Java language compiler;
java: Java interpreter;
jdb: Java debugger;
java -prof: Interpreter with the option to generate statistics on the use of methods;
Javadoc: Documentation generator;
jar: Tool that compresses, lists and expands;
appletviewer: Allows the execution and debugging of applets without a browser;
javap: allows to read the public interface of the classes;
extcheck: Detects conflicts in Jar files.

Q What is ‘inheritance’?

The expression is honestly almost self explanatory - inheritance is when an object gets the properties and parameters of a different (of another class). The overlap of this method discussed previously utilizes this - the most important notion of ​​inheritance is that you may construct new classes on present ones. There are five distinct forms of inheritance, however, Java supports only four (multiple inheritances aren't supported). Why are multiple inheritances not encouraged? There's but one special reason - to simplify this program.

Q. Is the data transmitted by reference or by value in Java?

Even Though the Response to this Query is quite simple, That Question could be confusing for novices. First, let us clarify what the issue is:

1. Passing by value - signifies that people
2. Passing by reference - means that people to answer the Query, we must examine two cases. They represent two kinds of information which we can pass into a method: crude and an object.

When we pass the primitives into some method, its value replicated to A new factor. Thus, we could say that Java is a strictly pass-by-value vocabulary.

We can Find out More about this in a few of our posts:

Q. What access modifiers are available in Java and what is your goal?

There are four access modifiers in Java:

1.    private
2.    default (package)
3.    protected
4.    public
• The private modifier guarantees that class members won't be accessible outside the class.
• It may be applied to methods, properties, constructors, nested classes, but not into the top-notch classes themselves.
• Contrary to the private modifier, we could use the default modifier into all kinds of class members and into the class itself. We may use the default visibility by not including any access modifiers.
• When we utilize default visibility, our class or its members will be accessible only inside our class package.
• We have to remember that the default access modifier has nothing in common with all the default keyword.
• Makes classes and members accessible in most packages and throughout all classes.

Q. What other modifiers are available in Java and what is your goal?

There are five other modifiers available in Java:

1.    static
2.    Final
3.    abstract
4.    volatile synchronized

All these don't control visibility. Pupils don't require any case to called. They predicted using the class name rather than the object's reference title.
Thus, we've got the final keyword. We can utilize it together with fields, methods, and classes.
• When final utilized in a field, it usually means that the area reference cannot be changed. For that reason, it cannot reassign to a different object. Whenever the final applied to a class or method, it means that class or method can't be replaced or extended.
The following keyword is abstract. It may explain classes and methods.
• Rather, they need to be sub-classified. When methods are abstract, they're left unimplemented and may override in subclasses.
The synchronized keyword may be the most complex. We can utilize it with the case, in addition to static methods and blocks of code.
• When we use this keyword, we induce Java to use a track lock to give synchronization on a particular code fragment.
The previous keyword we will talk about is volatile. We can use it in combination with the case fields.
• Declares the field value has to be written and read to the main memory - dismissing the CPU cache.
• All writes and readings out of a volatile factor are nuclear.

Q. What is the difference between Steak and Heap?

There are two components of memory in which the JVM shops all variables and objects. The first is that the stack and the next is that the heap.

The stack is a LIFO structure (last to enter, first to leave) Every new method invocation reserves another block. When the methods complete their implementation, the blocks are published from the contrary way they had been launched. Every thread has its stack.

We have to be conscious that the stack has considerably less memory space than the stack. When a stack is complete, the JVM will start a StackOverflowError. It's very likely to happen whenever there's a poor recursive call, and the recursion is quite deep.

Every new object is made in Java heap, which can be used for dynamic allocation. There's a garbage collector accountable for erasing unused objects, split into youthful and children's spaces. The JVM starts an OutOfMemoryError when the heap is complete.

Q. What is the void type and when do we use it?

Each time we write a method in Java, it has to have a return type. If we need the method to return regardless of value, we may use the void keyword.
We must also know that there's a Void class. It's a placeholder class which may be used, by way of instance, when working with generics. The Void class can't be instantiated or lengthy.
Q. What are the methods of the Object class and what do they do?

It is important to know what methods the Object class contains and how they work. It is also advantageous when we want to override these methods:
clone() - returns a copy of this object.
equals() - returns _true when this object is the same as the object passed as a parameter.
finalize() - the garbage collector calls this method while cleaning the memory.
getClass() - returns the runtime class for this object.
hashCode () - returns a hash code for this object. We must be aware that it must be consistent with the equals () method.
 • notify() - send a notification to a single thread waiting for the object's monitor.
 • notifyAll() - send a notification to all threads waiting for the object's monitor.
 • toString() - returns a string representation of this object.
 • wait() - there are three overloaded versions of this method. Forces the current thread to wait for the specified amount of time until another thread calls notify() or notifyAll() on this object.

Q. What is an Enum and how can we use it?

Enum is a type of class that allows developers to specify a set of predefined constant values. To create this class, we need to use the keyword enum. Let's imagine an enumeration of days of the week:
public enum Day {
To iterate over all the constants, we can use the static values ​​() method. Besides, enumerations allow us to define members as properties and methods, as regular classes.
• Although it is a particular type of class, we cannot subclass it.
• An enum can, however, implement an interface.
Another exciting advantage of enums is that they are thread-safe and are therefore popularly used as singletons.

Q. What is JAR?
JAR is a shortcut to Java archive. It is a compressed file using the ZIP format. We can use it to include the class files and auxiliary resources needed for the applications. It has many features:
Security - we can digitally sign JAR files.
Compression - using a JAR, we can compress files for efficient storage.
Portability - we can use the same JAR file on multiple platforms.
Versioning - JAR files can contain metadata about the data they provide.
Sealing - we can seal a package within a JAR file. It means that all classes in a package must be included in the same JAR file.
Extensions - we can use the JAR file format for packaging modules or extensions for existing software.
Q. What is the NullPointerException?
The NullPointerException is most likely the most frequent exception in the Java world. It's an unchecked exception, and so extends RuntimeException. We shouldn't attempt and cope with this.
This exception is thrown when we try to access a variable or call a null reference method, like when:
• invoking a null reference method.
• defining or obtaining a subject by a null reference.
• assessing the length of a null matrix reference.
• placing or obtaining a product out of a null matrix reference playing null.

Q. Why is String an immutable class?

We have to understand that String objects are handled differently from other objects from the JVM. One difference is that String objects are immutable. It means that we can't alter them after making them. There Are lots of reasons why they act this way:
• They're saved in the string pool, and it is a distinctive region of the heap memory. It's in charge of keeping a great deal of space.
• The immutability of the String class helps to ensure your hash code isn't changed. We can make confident that we won't replace any information because of the shift in hash codes.
• They may be used securely on multiple threads. No thread can alter the value of a String object, to find thread security at no cost.
• Strings are immutable to avoid serious security problems. Sensitive data, like passwords, maybe altered through an untrusted origin or new thread.
Q. What is the difference between dynamic link and static link?

Binding in Java is a procedure of linking a method call with the right method body. We can differentiate two types of relations in Java: static and dynamic.
• The primary difference between static and dynamic link is the static link happens at compile-time and also the dynamic link at run time.
Static binding employs class information for binding. It's in charge of controlling the class members that are private or static and final methods and variables. Besides, the static link links overloaded methods.
The dynamic link, on the other hand, uses the object's data to solve the links. That's the reason why it's accountable for solving virtual and substituted methods.

Q. What is JIT?

JIT signifies"just in time". It's a component of the JRE that runs at run time and increases the operation of the program. Specifically, it's a compiler that runs directly after the program begins.

This differs in the typical Java compiler that compiles the code before the program is launched. JIT can quicken the program in various ways.

For example, the JIT compiler is responsible for compiling the bytecode into native instructions dynamically to improve performance. Additionally, it may optimize the code to the target CPU and operating system.

Additionally, it has access to a lot of runtime statistics which may be used to recompile for optimum performance. For this, it may also create a few global code optimizations or reorganize the code for much better utilization of the cache.

Q. What is reflection in Java?

Reflection is a robust mechanism in Java. Reflection is a Java language mechanism that allows programmers to examine or modify the program's internal state (properties, methods, classes, etc.) at run time. The java.lang.reflect package provides all the needed components for utilizing reflection.
By utilizing this attribute, we can access all probable fields, methods, constructors within a class definition. We could access them regardless of their access modifier. It follows that, by way of instance, we can access private members. To do so, we do not need to be aware of their names. All we will need to do is utilize some static Class methods.
It's worth knowing that there's a possibility to limit access via reflection. To do so, we can utilize the Java security manager and the Java security policy document. They enable us to give permissions to classes.
When working with modules because Java 9, we have to understand this, by default, we can't use reflection on classes imported from a different module. To allow other classes to use reflection to access the private members of a package, we have to grant the "Reflection" permission.

Q. What is the difference between static and dynamic class loading?

The static class loading happens when we increase classes available at compile time. We could use it by creating object instances using the new keyword.

Dynamic class loading identifies a scenario where we are not able to deliver a class definition in the time of compilation. But, we could do it at run time. To create an instance of a class, We Must use Class.forName() method:

Q. What is the purpose of the Serializable interface?

• We can utilize the Serializable interface to allow serialization of a class, with the Java serialization API. Serialization is a mechanism for saving an object's state for a string of bytes, while deserialization is a mechanism for restoring the state of the object of a class. The serialized output maintains the state of the object and some metadata about the type and types of its fields.

We must understand that the subtypes of all serializable classes will also be serializable.  But if We Would likes to make a class serializable, however, its supertype isn’t serializable, we must do two things:

• Implement the Serializable interface guarantee that a constructor with no argument is present from the superclass.

Q. Explain the difference between the primitive and reference types.

Reference types inherit in the top java.lang.Object class and therefore are inheritable (except final classes). Primitive types don't inherit and can't be sub classed.

The values of the primitively typed argument are always passed through the stack, which means that they are passed by value, not by reference. It has the following implication: changes made to a primitive argument value within the method do not propagate to the real value of the argument.

Primitive types are often stored with all the underlying hardware worth types.

For example, to store an int value, a 32-bit memory cell can be used. Reference types pose the overhead of this object header, present in most cases of a reference type.

The dimensions of an object's header can be very significant in contrast to a simple numerical value dimension. That is why primitive types have been introduced in the very first place to conserve space on object overload. The drawback is that not everything in Java is technically an object - primitive values don't inherit from the Object class.

Q. Describe the different primitive types and the amount of memory they occupy.

Java contains eight primitive types:

• Boolean - reasonable value of true / false. The dimensions of this Boolean aren’t characterized by the JVM specification and might vary in various implementations.

Byte - signed 8-bit value,
Short - signed 16-bit value,
char - 16-bit unsigned value,
int - signed 32-bit value,
long - 64-bit signed value,
float - 32-bit single-precision floating-point value corresponding to the IEEE 754 standard,

double - 64-bit double-precision floating-point value corresponding to the IEEE 754 standard.

Q. What is the difference between an abstract class and an interface? What are the use cases for both?

An abstract class is a class with all the abstract modifier in its definition. It can't be instantiated, but it could be classified. The interface is a type explained together with the keyword interface. Additionally, it can't be instantiated, but it may be implemented.

The main difference between an abstract class and an interface is that a class can implement multiple interfaces, but extend only one abstract class.

An abstract class is usually used as a foundation type in the specific class hierarchy and signifies an essential intention of classes that inherit from it.

An abstract class can also implement some fundamental methods needed in most subclasses. For example, most map collections in the JDK are inherited from the AbstractMap class, which implements many methods used by subclasses (such as the equals method).
An interface defines a contract where the class agrees. An implemented interface may mean not just the most important intention of this class, but also some extra contracts. By way of instance.

For example, if a class implements the Comparable interface, it implies that instances of this class can be contrasted, whatever the primary intention of this class is.

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.