1. Can you explain what Java is?
Ans:
Java is high-level object-oriented programming language that runs on the Java Virtual Machine (JVM). This makes it platform-independent, so programs can work on any device. Java is secure, reliable and used for web, mobile, and desktop applications.
2. What are Java's major features?
Ans:
Java is platform-independent, object-oriented, and easy to learn. It supports multithreading, automatic memory management and high performance. Its robustness, portability and security make it ideal for building scalable applications.
3. How does Java implement object-oriented programming?
Ans:
Java uses OOP to organize code into reusable parts. It includes classes and objects to model real world entities, inheritance for reusing code, polymorphism for flexibility, encapsulation to protect data and abstraction to simplify complex programs.
4. What are the different access modifiers in Java?
Ans:
Java has public, private, protected, and default access levels. Public allows access from anywhere, private restricts to the same class, protected allows access in the package and subclasses, and default allows access only within the same package.
5. How is an abstract class different from an interface?
Ans:
An abstract class can have methods with and without implementation, variables, and constructors. An interface is used to define a contract for classes and allows multiple inheritance. From Java 8, interfaces can also have default and static methods.
6. How does Java handle errors with exceptions?
Ans:
Java use try, catch and finally blocks to handle exceptions. Code that may throw an error goes in the try block, catch handles the error and finally runs important code regardless of errors. This prevents crashes and ensures safe execution.
7. What is the difference between checked and unchecked exceptions?
Ans:
Checked exceptions such as IOException, need to be handled or declared and are validated during compilation. Unchecked exceptions occur at runtime, like NullPointerException and are not checked by the compiler. This helps in writing more reliable programs.
8. What is the Java Collections Framework?
Ans:
A collection of classes and interfaces called the Collections Framework are used to effectively manage collections of things. It includes List, Set, Map and implementations like ArrayList and HashMap. It makes storing, retrieving and manipulating data easier.
9. What is a Stream in Java?
Ans:
A Stream is a modern way to process collections of data. It allows operations such filtering, mapping and reducing without changing the original collection. Streams make code concise and support efficient data handling.
10. What is garbage collection in Java?
Ans:
Garbage collection automatically removes objects that are no longer needed. It frees memory, prevents leaks and improves performance. This ensures efficient memory management without manual intervention.