1. What is Java?
Ans:
Java is the widely used high level programming language known for building web, mobile and desktop applications. Its platform independence, which allows it to work on any device that supports the JVM, is one of its greatest advantages. This makes it highly flexible and suitable for wide range of applications.
2. Why is Java so popular?
Ans:
Java is popular because it offers several key features that make development easier and more efficient. It is platform-independent, allowing code to run on any system with a JVM. Java supports object-oriented programming which helps organize code better and makes it reusable. It also includes features like multithreading, which allows multiple tasks to run at the same time and automatic memory management through garbage collection.
3. How does Java use Object-Oriented Programming (OOP)?
Ans:
Java is based on object-oriented programming (OOP) principles which provide a structured way to write and manage code. It uses concepts like classes and objects to model real-world entities. Inheritance allows one class to reuse features of another while polymorphism helps a method behave differently based on the input. Encapsulation protects data by keeping it hidden inside a class and abstraction focuses only on essential details, hiding the complex parts.
4. What are access modifiers in Java?
Ans:
Java controls the visibility of variables, classes and functions via access modifiers. The public modifier allows access from any other class in any package. The private modifier restricts access to only within the same class. The protected modifier allows access within same package and to subclasses in other packages. If no modifier is used (default access), the member is accessible only within the same package. These modifiers help in managing the scope and security of code.
5. What’s the difference between abstract classes and interfaces?
Ans:
An abstract class in Java can contain both fully defined methods and methods that are left incomplete for subclasses to implement. It can also have variables and constructors. On the other hand, an interface (especially before Java 8) only includes method declarations and constant variables, with no method bodies. Interfaces are often used when you want to provide a common behavior to different classes as they support multiple inheritance. Abstract classes offer more flexibility for sharing common functionality among closely related classes.
6. How does Java handle exceptions?
Ans:
Java handles exceptions using a try-catch-finally block. The code that might cause an error is placed inside the try block. If an exception occurs, the catch block catches and handles it to prevent the program from crashing. The finally block contains code that always work, whether an exception occurs or not and is commonly used for cleanup tasks like closing files or releasing resources. This structure ensures programs can handle errors smoothly and continue running when possible.
7. What is the difference between checked and unchecked exceptions?
Ans:
In Java, checked exceptions are those that must be handled at compile time, such as IOException. The compiler forces you to use a try-catch block or declare the exception using the throws keyword. Unchecked exceptions like NullPointerException or ArrayIndexOutOfBoundsException, occur during runtime and don’t require mandatory handling. However, its still considered good practice to manage both types to build stable and error-free applications.
8. What is the Java Collections Framework used for?
Ans:
The Java Collections Framework is set of classes and interfaces that helps developers store, manage and process groups of data. It includes commonly used data structures such as List, Set and Map. With the help of these tools, efficiently sorting, searching and editing data is simple. The framework also reduces the need to write complex code from scratch, making data handling simpler and faster in Java programs.
9. What are Java Streams used for?
Ans:
Streams in Java provide a modern way to work with data collections using a functional approach. They allow operations like filtering, mapping and reducing data in clean and readable manner. Unlike traditional loops streams don’t modify the original data source, making them safer to use. They also help in writing more concise and efficient code. Streams are especially useful when the dealing with large datasets or performing multiple operations on data.
10. What is garbage collection in Java?
Ans:
Garbage collection in Java is the automatic process of identifying and removing unused objects from memory. When an object is no longer referenced in the program the Java garbage collector frees up the memory it occupied. This helps improve the application’s performance and prevents memory leaks. Developers don’t need to manually delete objects, which reduces errors and makes memory management easier. Garbage collection is one of the key features that adds reliability to Java applications.