Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Java Interview Q&A

  1. What is Java?
    Java is a high-level, general-purpose programming language that is widely used for developing various types of applications.
  2. Who created Java?
    Java was created by James Gosling and his team at Sun Microsystems, which is now owned by Oracle Corporation.
  3. What are the main features of Java?
    Java is known for its platform independence, object-oriented programming (OOP) approach, automatic memory management (garbage collection), and extensive standard library.
  4. What is the Java Virtual Machine (JVM)?
    The JVM is the runtime environment for Java applications. It executes Java bytecode and provides various services like memory management and security.
  5. What is the difference between JDK and JRE?
    JDK stands for Java Development Kit and includes tools needed for developing Java applications. JRE stands for Java Runtime Environment and provides the necessary runtime components to run Java applications.
  6. What is the difference between a class and an object in Java?
    A class is a blueprint or template that defines the structure and behavior of objects. An object, on the other hand, is an instance of a class.
  7. What is the difference between an abstract class and an interface?
    An abstract class can have both concrete and abstract methods, while an interface can only have abstract methods. A class can implement multiple interfaces but can inherit from only one class (abstract or not).
  8. What is a package in Java?
    A package is a way to organize classes, interfaces, and other resources in Java. It helps in avoiding naming conflicts and provides better modularity and reusability.
  1. What is the difference between checked and unchecked exceptions?
    Checked exceptions are checked at compile-time, and the programmer is required to handle or declare them. Unchecked exceptions, on the other hand, are not checked at compile-time and do not need to be explicitly handled or declared.

  1. What is method overloading in Java?
    Method overloading allows multiple methods with the same name but different parameters to coexist in a class. The appropriate method is selected based on the arguments passed during the method call.
  2. What is method overriding in Java?
    Method overriding occurs when a subclass provides its own implementation of a method defined in its superclass. The method in the subclass must have the same name, return type, and parameters as the method in the superclass.
  3. What are access modifiers in Java?
    Access modifiers control the visibility and accessibility of classes, methods, and variables. The main access modifiers in Java are public, private, protected, and default (no modifier).
  4. What is the difference between a constructor and a method?
    A constructor is a special method used to initialize an object, whereas a method is a regular function that performs a specific action.
  5. What is the difference between static and non-static methods?
    Static methods belong to the class itself and can be called without creating an instance of the class. Non-static methods are associated with an instance of the class and can only be called on objects.
  6. What is the difference between final, finally, and finalize?
    The final keyword is used to restrict modification, the finally block is used to define code that should be executed regardless of exceptions, and the finalize() method is called by the garbage collector before an object is destroyed.
  7. What is the purpose of the static keyword?
    The static keyword is used to define class-level variables and methods that can be accessed without creating an instance of the class.
  8. What are Java annotations?
    Annotations provide metadata about code elements and can be used to add information, instructions, or behavior to the code. They are introduced with the @ symbol.
  9. What is autoboxing and unboxing in Java?
    Autoboxing is the automatic conversion of a primitive type to its corresponding wrapper class, while unboxing is the reverse operation.
  10. What is the difference between == and .equals()?
    The == operator is used to compare the references of objects, while the .equals() method is used to compare the content or values of objects.
  11. What is the this keyword used for?
    The this keyword refers to the current instance of a class. It is used to differentiate between instance variables and method parameters with the same name.
  1. What are generics in Java?
    Generics allow the use of type parameters to create classes, interfaces, and methods that can operate on different data types while maintaining type safety.
  2. What is the super keyword used for?
    The super keyword is used to refer to the superclass or parent class. It can be used to access the superclass’s methods, variables, or constructors.
  3. What is a thread in Java?
    A thread is a lightweight unit of execution within a Java program. It allows concurrent execution of multiple tasks and improves program efficiency.
  4. What is the synchronized keyword used for?
    The synchronized keyword is used to create mutually exclusive blocks of code or methods. It ensures that only one thread can access the synchronized code at a time, preventing data inconsistency.
  1. What is the volatile keyword used for?
    The volatile keyword is used to indicate that a variable’s value may be modified by multiple threads. It ensures that the latest value of the variable is always read and written by all threads.

  1. What are the different types of inner classes in Java?
    Java supports four types of inner classes: static nested classes, non-static nested classes (inner classes), local classes, and anonymous classes.
  2. What is the try-catch-finally block used for?
    The try-catch-finally block is used for exception handling in Java. The try block contains the code that may throw an exception, the catch block handles the exception, and the finally block is executed regardless of whether an exception occurs or not.
  3. What is the purpose of the throws keyword?
    The throws keyword is used to declare that a method may throw one or more exceptions. It is used in the method signature to indicate the types of exceptions that the method can throw.
  4. What is the purpose of the throw keyword?
    The throw keyword is used to explicitly throw an exception in a program. It is followed by an instance of an exception class or a subclass.
  5. What is the difference between the StringBuilder and StringBuffer classes?
    Both StringBuilder and StringBuffer are used to manipulate strings, but StringBuilder is not thread-safe, whereas StringBuffer is thread-safe.
  1. What is the Java Collections Framework?
    The Java Collections Framework provides a set of classes and interfaces to handle collections of objects, such as lists, sets, and maps. It includes data structures and algorithms to manipulate and store the collections efficiently.
  2. What is the difference between ArrayList and LinkedList?
    ArrayList is implemented as a resizable array, allowing fast random access but slower insertion and deletion of elements. LinkedList is implemented as a doubly-linked list, allowing fast insertion and deletion but slower random access.
  3. What is the difference between an Iterator and a ListIterator?
    An Iterator is used to iterate over a collection in one direction, whereas a ListIterator extends the Iterator interface and allows bidirectional traversal of a list.
  4. What is the difference between the Set and List interfaces?
    The Set interface is an unordered collection of unique elements, while the List interface is an ordered collection that allows duplicate elements.
  5. What is the difference between HashMap and HashTable?
    Both HashMap and HashTable are used to store key-value pairs, but HashMap allows null values and is not synchronized, while HashTable does not allow null values and is synchronized.
  1. What is the difference between an int and an Integer?
    int is a primitive data type in Java, while Integer is a wrapper class that wraps the int value and provides additional methods and functionality.

  1. What is the difference between a shallow copy and a deep copy?
    A shallow copy creates a new object but shares the references of the original object’s fields. A deep copy creates a new object and copies the values of the original object’s fields, including any referenced objects.
  2. What is the purpose of the transient keyword?
    The transient keyword is used to indicate that a variable should not be serialized during object serialization. It is commonly used for sensitive data or variables that do not need to be saved.
  3. What is the difference between method overloading and method overriding?
    Method overloading occurs when multiple methods with the same name but different parameters exist in a class. Method overriding occurs when a subclass provides its own implementation of a method defined in its superclass.
  4. What is the difference between composition and inheritance?
    Composition is a design technique where a class contains an instance of another class as a member, whereas inheritance is a mechanism where a subclass inherits the properties and methods of its superclass.
  1. What is the purpose of the finalize() method?
    The finalize() method is called by the garbage collector before an object is garbage collected. It can be overridden to release any resources or perform cleanup operations before the object is destroyed.
  2. What is the difference between a File and a Directory in Java?
    A File represents a file or a directory path, while a Directory represents a directory.
  3. What is the java.lang package?
    The java.lang package is automatically imported in every Java program and contains fundamental classes and interfaces such as String, Object, and Math.
  4. What is the purpose of the Comparable and Comparator interfaces?
    The Comparable interface is used to define a natural ordering of objects, while the Comparator interface provides custom ordering logic for objects.
  5. What is reflection in Java?
    Reflection is a feature in Java that allows programmatic access to class information, such as methods, fields, and constructors, at runtime.
  1. What is the difference between a static variable and an instance variable?
    A static variable is associated with the class itself and has a single shared value among all instances of the class. An instance variable is associated with each instance of the class and has a separate value for each instance.

  1. What is the purpose of the assert statement?
    The assert statement is used for debugging and testing purposes. It checks a boolean expression and throws an AssertionError if the expression is false.
  2. What is the System.out.println() method used for?
    The System.out.println() method is used to print output to the standard output stream (usually the console).
  3. What is the difference between an applet and an application in Java?
    An applet is a small program that runs within a web browser, while an application is a standalone program that can be run independently.
  4. What is the Java Development Kit (JDK)?
    The JDK is a software development environment used for developing Java applications. It includes the necessary tools, compilers, and libraries to write, compile, and run Java code.