JAVA Interview Questions
Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle Corporation) in the mid-1990s. It was designed to be platform-independent, meaning that Java programs can run on any device or operating system that has a Java Virtual Machine (JVM) installed.
Here are 10 important Java interview questions along with their answers:
- What is the difference between JDK, JRE, and JVM?
- JDK (Java Development Kit): It is a software development kit used for developing Java applications. It includes JRE, an interpreter/loader (Java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc), and other tools.
- JRE (Java Runtime Environment): It is an environment in which Java bytecode can be executed. It includes JVM and libraries necessary to run Java applications, but does not contain development tools.
- JVM (Java Virtual Machine): It is an abstract computing machine that enables a computer to run Java programs. It interprets compiled Java bytecode for execution.
- What is the difference between abstract classes and interfaces in Java?
- Abstract class: It cannot be instantiated and may contain both abstract and concrete methods. It can have constructors and member variables. Subclasses extend abstract classes using the “extends” keyword.
- Interface: It contains only abstract methods and constants. It cannot have constructors or member variables. Classes implement interfaces using the “implements” keyword.
- What is the significance of the keyword “static” in Java?
- The “static” keyword denotes that a member (method or variable) belongs to the class rather than to any specific instance. It can be accessed without creating an instance of the class.
- How does garbage collection work in Java?
- Garbage collection in Java automatically deallocates memory for objects that are no longer reachable or in use by the program. The JVM periodically identifies unreferenced objects and reclaims their memory.
- Explain the difference between checked and unchecked exceptions in Java.
- Checked exceptions: These are checked at compile-time, meaning the compiler forces the programmer to handle them either by catching them or by declaring them in the method signature using the “throws” clause.
- Unchecked exceptions: These are not checked at compile-time, and the compiler does not force the programmer to handle them. They usually represent programming errors or unexpected conditions at runtime.
- Describe the use of the “final” keyword in Java.
- The “final” keyword can be applied to variables, methods, and classes in Java.
- When applied to variables, it indicates that the variable’s value cannot be changed after initialization.
- When applied to methods, it indicates that the method cannot be overridden by subclasses.
- When applied to classes, it indicates that the class cannot be subclassed.
- Explain the purpose of the “super” keyword in Java.
- The “super” keyword in Java is used to refer to the superclass (parent class) of the current object. It is used to access superclass methods, constructors, and member variables.
- What are the different access modifiers in Java and how do they differ?
- Java provides four access modifiers: “public,” “protected,” “default” (no modifier), and “private.”
- They control the visibility of classes, methods, and variables.
- “Public” members are accessible from any other class.
- “Protected” members are accessible within the same package and by subclasses.
- Default (no modifier) members are accessible only within the same package.
- “Private” members are accessible only within the same class.
- What is the purpose of the “equals” and “hashCode” methods in Java?
- The “equals” method is used to compare the equality of two objects. It is typically overridden to provide custom equality logic for user-defined classes.
- The “hashCode” method returns a hash code value for the object, which is used in hash-based data structures like HashMap and HashSet for efficient retrieval and storage.
- How does Java support multithreading?
- Java supports multithreading through its built-in Thread class and the Runnable interface.
- Threads can be created either by extending the Thread class or implementing the Runnable interface and passing it to a Thread object.
- Java provides synchronization mechanisms like synchronized blocks and methods to ensure thread safety in concurrent programming.
These questions cover fundamental concepts of Java and are often asked in interviews to assess a candidate’s understanding of the language.