1. What is Java?
Answer: Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle). It is designed to be platform-independent through the use of the Java Virtual Machine (JVM).
2. What are the main features of Java?
Answer: Key features of Java include:
- Object-Oriented: Supports concepts like inheritance, encapsulation, and polymorphism.
- Platform-Independent: Write once, run anywhere (WORA) due to JVM.
- Automatic Memory Management: Garbage collection handles memory allocation and deallocation.
- Multithreading: Supports concurrent programming.
3. What is the difference between JDK, JRE, and JVM?
Answer:
- JDK (Java Development Kit): A software development kit that includes tools for developing Java applications, including the JRE and development tools (like the Java compiler).
- JRE (Java Runtime Environment): Provides libraries and the JVM to run Java applications. It does not include development tools.
- JVM (Java Virtual Machine): The runtime environment that executes Java bytecode. It is platform-dependent.
4. What is a class and an object in Java?
Answer:
- Class: A blueprint for creating objects. It defines properties (attributes) and methods (functions) that the objects created from the class can have.
- Object: An instance of a class. It represents a specific implementation of the class and can hold data and perform operations defined by the class.
5. What is inheritance in Java?
Answer: Inheritance is a mechanism where one class (subclass) can inherit fields and methods from another class (superclass). This promotes code reuse and establishes a relationship between classes.
6. What are interfaces in Java?
Answer: An interface is a reference type in Java, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Interfaces cannot have instance fields and are used to achieve abstraction and multiple inheritance.
7. What is the difference between an abstract class and an interface?
Answer:
- Abstract Class: Can have both abstract (no body) and concrete (with body) methods. It can have state (instance variables) and constructors.
- Interface: Only abstract methods (prior to Java 8, which introduced default and static methods). It cannot have instance variables or constructors.
8. What is the purpose of the main
method in Java?
Answer: The main
method is the entry point for any standalone Java application. It has the signature public static void main(String[] args)
and is where the program execution begins.
9. What are exceptions in Java?
Answer: Exceptions are events that occur during the execution of a program that disrupt the normal flow of instructions. Java provides a robust exception handling framework through try
, catch
, and finally
blocks to handle errors gracefully.
10. What is the difference between ==
and equals()
in Java?
Answer:
==
checks for reference equality, meaning it checks if two references point to the same object in memory.equals()
checks for value equality, meaning it checks if two objects are logically equivalent, which can be overridden in user-defined classes.
11. What is a constructor in Java?
Answer: A constructor is a special method used to initialize objects. It is called when an object of a class is created and has the same name as the class. Constructors can be overloaded.
12. What are collections in Java?
Answer: Collections are frameworks that provide a set of classes and interfaces for storing and manipulating groups of objects. The main interfaces include List
, Set
, Map
, and Queue
. They help manage data efficiently.