Java Interview Questions

Here is a resource that provides a collection of commonly asked questions in Java job interviews. The page includes questions on a wide range of Java topics, including core Java concepts, object-oriented programming, collections, multithreading, exception handling, and more. The questions are designed to test the knowledge and understanding of Java developers at different levels, from entry-level positions to senior and architect roles. Each question is accompanied by a brief explanation or sample code that illustrates the concept being tested.
Java interview questions and answers
The Java Interview Questions are valuable resource for job seekers preparing for Java job interviews, as well as for hiring managers looking to evaluate the technical skills of candidates. The questions cover a broad range of topics and are designed to assess both the depth and breadth of a candidate's knowledge of Java programming.

What is Java?

Java is a popular programming language that is widely used for developing a variety of software applications, including web applications, mobile applications, desktop applications, and enterprise software . It was first released in 1995 by Sun Microsystems and has since become one of the most widely used programming languages in the world. Java is an object-oriented programming language, which means that it is designed to represent real-world objects and concepts in code. It is also platform-independent, which means that code written in Java can run on any platform that has a Java Virtual Machine (JVM) installed, including Windows, Linux, and macOS. Some of the key features of Java include automatic memory management, strong type checking, and exception handling, which make it a relatively safe and easy language to use. It also has a large standard library that provides a wide range of functionality, as well as a large community of developers who contribute to open source libraries and frameworks that extend the capabilities of the language.

What Is a Package in Java?

In Java, a package is a mechanism used to group related classes and interfaces together in a hierarchical structure. Packages are used to organize code and to avoid naming conflicts.

A package can contain multiple classes and interfaces, and it can also contain sub-packages. The naming convention for packages is to use the reverse domain name of the organization or individual responsible for the package, followed by one or more descriptive package names.

For example, if a company named "Neeva" wanted to create a package to contain their customer management classes, they might name the package "com.neeva.customermanagement". The classes within this package would then be organized into a hierarchical structure under this package name.

Packages are defined using the "package" keyword at the beginning of the Java source file. For example, the following code defines a class called "Customer" within the "com.neeva.customermanagement" package:

package com.neeva.customermanagement; public class Customer { // class implementation here }

What is WORA?

WORA (Write Once Run Anywhere) is a concept in Java programming that refers to the ability of Java code to run on any platform without the need for recompilation. This is made possible by the Java Virtual Machine (JVM), which is a platform-independent software component that interprets Java bytecode and translates it into machine code that can be executed by the host platform. When Java code is compiled, it is translated into bytecode, which is a platform-independent format that can be executed on any device that has a compatible JVM installed. This means that Java code can be developed on one platform and run on any other platform that has a JVM, without the need for any platform-specific modifications. WORA is a key feature of Java that makes it popular for developing applications that can run on a variety of devices and platforms, from desktop computers to mobile devices and embedded systems.

What is bytecode?

In Java, bytecode refers to the compiled form of Java source code that is executed by the Java Virtual Machine (JVM). When a Java program is compiled, the Java compiler translates the source code into bytecode, which is a low-level, platform-independent representation of the program. Bytecode is a set of instructions that the JVM can understand and execute, regardless of the platform or architecture on which the program is running. Because Java programs are compiled into bytecode, they can be run on any platform that has a Java Virtual Machine installed. Bytecode is often compared to machine code, which is the binary code that a computer's processor can execute directly. While machine code is specific to a particular hardware architecture, bytecode is designed to be platform-independent, making it more portable and easier to distribute.

Define JIT compiler in Java?

overload main() method in Java
JIT stands for Just-In-Time compiler. In Java, JIT compiler is a component of the Java Virtual Machine (JVM) that improves the performance of Java applications by compiling bytecode into native machine code at runtime. When a Java application is executed, the JVM first interprets the bytecode and executes it. However, interpreting bytecode can be slower than executing native machine code. To overcome this performance bottleneck, the JIT compiler analyzes the application's bytecode and identifies sections of code that are executed frequently. It then compiles these sections into optimized native machine code, which can be executed more quickly than interpreted bytecode. The JIT compiler can also optimize the native machine code it generates based on runtime information about the application's behavior. For example, it can identify frequently executed loops and unroll them to reduce the number of branch instructions, or it can inline frequently called methods to eliminate the overhead of method invocation.

What are primitive data types?

In Java, there are eight primitive data types, which are:

  1. byte: an 8-bit integer data type that can store values from -128 to 127.
  2. int: a 32-bit integer data type that can store values from -2,147,483,648 to 2,147,483,647.
  3. long: a 64-bit integer data type that can store values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
  4. short: a 16-bit integer data type that can store values from -32,768 to 32,767.
  5. float: a 32-bit floating-point data type that can store decimal values with up to 7 decimal digits of precision.
  6. double: a 64-bit floating-point data type that can store decimal values with up to 15 decimal digits of precision.
  7. boolean: a data type that can store only two values, true or false.
  8. char: a 16-bit Unicode character data type that can store a single character or a Unicode code point.
These primitive data types are built into the Java language and are not objects. They are used to represent basic data values and are passed by value, meaning that when they are passed as arguments to methods or assigned to variables, a copy of their value is created.

Is there any default value for local variables?

Yes, in Java, local variables do have a default value. However, unlike instance variables, the default value for local variables is not initialized automatically by the compiler or runtime environment.

Instead, when a local variable is declared, it is assigned a default value based on its data type. The default values for the primitive data types are as follows:

  1. byte, short, int, long: 0
  2. float, double: 0.0
  3. boolean: false
  4. char: '\u0000' (null character)
It is important to note that using a local variable without initializing it first can result in a compile-time error. Therefore, it is always best practice to explicitly initialize local variables with a value before using them.

Can you overload main() method in Java?

Yes, you can overload the main() method in Java. Overloading is the ability to define two or more methods with the same name in the same class, but with different parameters. When a Java program is run, it looks for a main() method with a specific signature: public static void main(String[] args). However, you can define additional main() methods with different parameter types, such as public static void main(int[] args) or public static void main(String arg1, String arg2). When a program is run, the Java Virtual Machine (JVM) will look for the main() method with the correct signature to execute. So, if you define multiple main() methods, you can choose which one to run by specifying the correct arguments when you start the program from the command line.

Note that the main() method is a special method in Java, and it must be defined as public, static, and void. However, when you overload the main() method, you can choose different parameter types and names for each method.

What Is the void Type and When Do you Use It?

In Java, void is a keyword that indicates that a method does not return a value. When you declare a method with void as its return type, it means that the method will perform some operation or task but will not return any value.

Use the void type when you want to declare a method that performs a specific task or operation, but you do not need the method to return any value. Some examples of methods that might have a void return type include methods that print output to the console, methods that modify the state of an object, or methods that perform some other type of side effect. It is important to note that while void methods do not return a value, they can still take arguments as input and can modify the state of the program or objects within it. Additionally, void is not a valid data type for variable declarations, as it is only used to indicate the return type of a method.

Why there are no global variables in Java?

In Java, there are no true global variables in the traditional sense, because all variables must be declared within a class, method, or block scope. Instead, Java provides a concept of "class variables" and "instance variables" that can be accessed by all methods within a class.

Global variables can create issues such as unintended side effects, which can lead to hard-to-debug code. Additionally, they can make it harder to maintain code over time, as it can be difficult to track down all of the places where a global variable is used and modified.

In Java, by declaring variables within classes, you can achieve similar functionality as global variables while also ensuring better encapsulation and reducing the risk of unintended side effects. Class and instance variables can be accessed and modified by all methods within a class, but they have a more limited scope and are generally considered to be better programming practice.

Define Object in Java?

In Java, an object is an instance of a class that represents a particular entity or concept in a program. An object has its own set of properties or attributes, which are defined by its class, and it can perform actions or operations, which are defined by its methods. Objects are used extensively in Java and object-oriented programming to model real-world objects or concepts and to enable code reuse and modularity.

What will be an initial value for an object reference which is defined as an instance variable?

If an object reference is defined as an instance variable in Java, its initial value will be null. When an object is created, memory is allocated to store its instance variables and they are initialized to their default values. For object references, the default value is null, which indicates that the variable does not currently point to any object in memory. It is the responsibility of the programmer to initialize the object reference to a valid object before it is used to avoid NullPointerExceptions or other errors.

Can you override static method in Java?

No, you cannot override a static method in Java. When a static method is declared in a class, it is associated with the class itself rather than with any particular instance of the class. This means that it is not possible to override a static method in a subclass, as the subclass cannot change the static behavior of the superclass. Instead, if you want to change the behavior of a static method in a subclass, you can define a new static method with the same name in the subclass. This will create a separate method that is specific to the subclass and does not override the original static method in the superclass. It is also important to note that static methods can be called directly on the class itself, without needing to create an instance of the class, which further reinforces the idea that they are not tied to any particular instance of the class.

Which class is the superclass for all the classes in Java?

Core Java Interview Questions and answers
The Object class is the superclass for all the classes in Java. Every class in Java implicitly inherits from the Object class, either directly or indirectly. This means that all the methods and variables defined in the Object class are available to all classes in Java, and can be overridden or extended in subclasses as needed. The Object class defines several useful methods, such as equals(), toString(), and hashCode(), which are commonly used in Java programming.

What is the super keyword in Java?

In Java, the super keyword is used to refer to the superclass of a class or to call a method or constructor defined in the superclass.

When used to refer to the superclass, the super keyword is followed by a dot (.) and the name of the method or variable that you want to access. This is often used in subclasses to access or override methods or variables defined in the superclass. When used to call a constructor in the superclass, the super keyword must be used as the first statement in the constructor of the subclass. This is used to initialize the state of the superclass before the subclass constructor continues with its own initialization.

What are the difference between an Inner Class and a Sub-Class?

An inner class is a class defined within another class, while a subclass is a class that inherits from a superclass. Here are some of the key differences between inner classes and subclasses:
  1. Relationship to the enclosing class: An inner class is always associated with an instance of the enclosing class, while a subclass is a separate class that inherits from a superclass.
  2. Access to enclosing class members: An inner class has access to all members (variables and methods) of the enclosing class, even if they are private, while a subclass has access only to protected and public members of the superclass.
  3. Scope: An inner class is visible only within the enclosing class, while a subclass can be used anywhere that the superclass can be used.
  4. Instantiation: An inner class is typically created within an instance of the enclosing class, while a subclass is created separately from the superclass.

What is the default size of the load factor in the hashing-based collection?

In hashing-based collections in Java, such as HashMap and HashSet, the default load factor is 0.75. The load factor is a value that determines when the collection should be resized to maintain performance. When the number of elements in the collection exceeds the load factor multiplied by the current capacity, the collection is resized to increase its capacity and maintain good performance. The default load factor of 0.75 is a good balance between memory usage and performance, and is often suitable for many use cases. However, it is possible to specify a different load factor when creating a hashing-based collection if needed, by passing a different value to the constructor.