1. Can you explain what Java is?
Ans:
Java is popular high-level, object-oriented programming language for creating web, mobile and computer applications. One of Java’s main strengths is that it runs on any device that has a Java Virtual Machine (JVM), which makes it platform-independent. Java is known for its simplicity, security and flexibility, making it a popular choice in software development.
2. What are some key features that make Java popular?
Ans:
Java has several important features that make it stand out. It is platform-independent, meaning code written in Java can run on any system with JVM. It follows object-oriented principles, making code modular and reusable. Java is simple, secure and supports multithreading, allowing multiple operations to run at the same time. Its also robust, portable and includes automatic memory management through garbage collection.
3. How does Java follow the Object-Oriented Programming (OOP) model?
Ans:
Java is built on object-oriented programming concepts, which help structure programs in an organized way. These concepts include classes and objects for defining and creating components, inheritance for reusing existing code, polymorphism for handling multiple behaviors with one method, encapsulation for protecting data and abstraction for focusing on essential features while hiding the details.
4. What are the different access levels in Java?
Ans:
Java provides access modifiers to control where classes, variables, and methods can be used. The public modifier allows access from any other class. private limits access to within the same class only. protected makes elements accessible within the same package and in subclasses. If no modifier is used (default), the element is accessible only within its package.
5. How is an abstract class different from an interface in Java?
Ans:
An abstract class in Java can contain both complete (concrete) and incomplete (abstract) methods, and it can have variables and constructors. In contrast an interface (before Java 8) only contains method declarations and constant values. Interfaces support multiple inheritance while abstract classes offer more flexibility for sharing common code among related classes.
6. How are exceptions handled in Java?
Ans:
Java handles errors using a try-catch-finally structure. The code that might cause an error is written inside the try block. The catch block is where an exception is caught and processed if it happens. The finally block contains code that runs no matter what happens whether or not an exception is thrown ensuring that cleanup actions like closing files always happen.
7. What is the difference between checked and unchecked exceptions?
Ans:
Checked exceptions are those that compiler checks during compile time, like IOException and they must be handled using try-catch or declared in the method signature. Unchecked exceptions, like NullPointerException, occur at runtime and do not require mandatory handling though it's still good practice to manage them.
8. What is the Java Collections Framework used for?
Ans:
Developers can store, manage and analyze groupings of data with the aid of the Java Collections Framework, a collection of classes and interfaces. It includes tools like List, Set and Map which are used to work with different types of data structures. This framework makes it easier to sort, search, and modify data efficiently.
9. What is the purpose of using Streams in Java?
Ans:
Streams in Java provide a modern and efficient way to process collections of data using a functional approach. They allow developers to perform tasks like filtering, mapping and reducing data without changing the original data source. Streams help write cleaner and more readable code for data handling.
10. Can you explain Java’s garbage collection process?
Ans:
Garbage collection in Java is automatic process that removes the unused objects from memory to free up space and improve performance. When an object is no longer referenced or needed Java’s garbage collector identifies it and clears it from memory helping to prevent memory leaks and ensuring efficient use of system resources.