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.
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:
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?

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:
- byte: an 8-bit integer data type that can store values from -128 to 127.
- int: a 32-bit integer data type that can store values from -2,147,483,648 to 2,147,483,647.
- 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.
- short: a 16-bit integer data type that can store values from -32,768 to 32,767.
- float: a 32-bit floating-point data type that can store decimal values with up to 7 decimal digits of precision.
- double: a 64-bit floating-point data type that can store decimal values with up to 15 decimal digits of precision.
- boolean: a data type that can store only two values, true or false.
- char: a 16-bit Unicode character data type that can store a single character or a Unicode code point.
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:
- byte, short, int, long: 0
- float, double: 0.0
- boolean: false
- char: '\u0000' (null character)
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?

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:- 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.
- 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.
- Scope: An inner class is visible only within the enclosing class, while a subclass can be used anywhere that the superclass can be used.
- 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.- Java Interview Questions-Core Faq - 1
- Java Interview Questions-Core Faq - 2
- Java Interview Questions-Core Faq - 3
- Important features of Java
- Difference between Java and JavaScript?
- What is the difference between JDK and JRE?
- What gives Java its 'write once and run anywhere' nature?
- What is JVM and is it platform independent?
- What is Just-In-Time (JIT) compiler?
- What is the garbage collector in Java?
- What is NullPointerException in Java
- Difference between Stack and Heap memory in Java
- How to set the maximum memory usage for JVM?
- What is numeric promotion?
- Why do we need Generic Types in Java?
- What does it mean to be static in Java?
- What are final variables in Java?
- How Do Annotations Work in Java?
- How do I use the ternary operator in Java?
- What is instanceof keyword in Java?
- How ClassLoader Works in Java?
- What are fail-safe and fail-fast Iterators in Java
- What are method references?
- "Cannot Find Symbol" compile error
- Difference between system.gc() and runtime.gc()
- How to convert TimeStamp to Date in Java?
- Does garbage collection guarantee that a program will not run out of memory?
- How setting an Object to null help Garbage Collection?
- How do objects become eligible for garbage collection?
- How to calculate date difference in Java
- Difference between Path and Classpath
- Is Java "pass-by-reference" or "pass-by-value"?
- Difference between static and nonstatic methods java
- Why Java does not support pointers?
- What is package in Java?
- What are wrapper classes?
- What is singleton class in Java?
- Difference between Java Local Variable, Instance Variable and a Class Variable?
- Can a top level class be private or protected in java
- Are Polymorphism , Overloading and Overriding similar concepts?
- How to Use Locks in Java
- Why Multiple Inheritance is Not Supported in Java
- Why Java is not a pure Object Oriented language?
- Why can't a Java class be declared as static?
- Difference between Abstract class and Interface in Java
- Why do I need to override the equals and hashCode methods in Java?
- Why does Java not support operator overloading?
- What’s meant by anonymous class in Java?
- Static Vs Dynamic class loading in Java
- Why am I getting a NoClassDefFoundError in Java?
- How to generate random integers within a specific range in Java
- What's the meaning of System.out.println in Java?
- What is the purpose of Runtime and System class?
- What is finally block in Java?
- What is difference between final, finally and finalize?
- What is try-with-resources in java?
- What is a stacktrace?
- What is the meaning of immutable in terms of String?
- What are different ways to create a string object in Java?
- Difference between String and StringBuffer/StringBuilder in Java
- What is the difference between creating String as new() and literal?
- How do I convert String to Date object in Java?
- How do I create a Java string from the contents of a file?
- What actually causes a StackOverflow error in Java?
- Why is char[] preferred over String for storage of password in Java
- What is I/O Filter and how do I use it in Java?
- Serialization and Deserialization in Java
- Understanding transient variables in Java
- What is Externalizable in Java?
- What is the purpose of serialization/deserialization in Java?
- What is the Difference between byte stream and Character streams
- How to append text to an existing file in Java
- Read/convert an InputStream to a String in Java
- What is the difference between Reader and InputStream in Java
- Introduction to Java threads
- What is synchronization Java?
- Static synchronization Vs non static synchronization in Java
- Java Thread Deadlock Tutorial
- What is Daemon thread in Java
- Difference between implements Runnable and extends Thread in Java
- What is the volatile keyword in Java
- What are the basic interfaces of Java Collections Framework
- What are the differences between ArrayList and Vector in Java
- What is the difference between ArrayList and LinkedList?
- What is the difference between List and Set in Java
- Difference between HashSet and HashMap in Java
- Difference between HashMap and Hashtable in Java?
- How does the hashCode() method of java works?
- Difference between capacity() and size() of Vector in Java
- What is a Java ClassNotFoundException?
- How to fix java.lang.UnsupportedClassVersionError