10 Interview Questions in Java

10 Interview Questions in Java

Java:

  1. What is Java?
    • Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle). It is designed to be platform-independent, allowing developers to write code once and run it anywhere.
  2. What are the main features of Java?
    • Java features include:
      • Object-oriented: Everything in Java is an object.
      • Platform-independent: Java code runs on any platform with the Java Virtual Machine (JVM).
      • Simple and easy to learn.
      • Robust and secure.
      • Multithreaded and scalable.
      • Architecture-neutral.
  3. What is the difference between JDK, JRE, and JVM?
    • JDK (Java Development Kit) is a software development kit that includes tools for developing, debugging, and monitoring Java applications.
    • JRE (Java Runtime Environment) is a runtime environment that is required to run Java applications. It includes the JVM and other libraries.
    • JVM (Java Virtual Machine) is an abstract machine that provides a runtime environment for Java bytecode to be executed.
  4. What is the difference between an abstract class and an interface?
    • An abstract class can have both abstract and concrete methods, while an interface can only have abstract methods (prior to Java 8).
    • A class can extend only one abstract class, but it can implement multiple interfaces.
    • Abstract classes can have instance variables, constructors, and static methods, whereas interfaces cannot.
  5. What are access modifiers in Java?
    • Access modifiers control the visibility of classes, methods, and variables in Java.
    • There are four access modifiers in Java: private, protected, default (package-private), and public.
    • private: Visible only within the same class.
    • protected: Visible within the same package and subclasses.
    • default (no modifier): Visible within the same package.
    • public: Visible to all classes.
  6. What is the difference between == and .equals() method in Java?
    • == compares object references, checking if they refer to the same object in memory.
    • .equals() method is used to compare the actual contents or values of two objects. It’s typically overridden in classes to provide custom comparison logic.
  7. What is the difference between ArrayList and LinkedList?
    • ArrayList is implemented as a resizable array, allowing fast random access but slower insertion and deletion.
    • LinkedList is implemented as a doubly linked list, allowing fast insertion and deletion but slower random access.
  8. What is a constructor in Java?
    • A constructor is a special type of method in Java that is used to initialize objects.
    • It has the same name as the class and does not have a return type.
    • Constructors can be parameterized (taking arguments) or default (no arguments).
  9. What is the final keyword used for in Java?
    • The final keyword can be applied to variables, methods, and classes in Java.
    • When applied to a variable, it indicates that its value cannot be changed (constant).
    • When applied to a method, it indicates that the method cannot be overridden by subclasses.
    • When applied to a class, it indicates that the class cannot be subclassed.
  10. What is exception handling in Java?
    • Exception handling in Java is a mechanism to deal with runtime errors (exceptions) gracefully.
    • It involves using try, catch, finally, and throw keywords.
    • Exceptions are objects representing errors that occur during the execution of a program, and they can be of types checked (compile-time) or unchecked (runtime).