1. What is Java?
Ans:
Java is a high-level, object-oriented programming language used to build applications that run on any device with a Java Virtual Machine (JVM). It’s secure, platform-independent, and widely used across web, desktop, and mobile platforms.
2. What are Java's primary features?
Ans:
- Object-Oriented
- Simple and Secure
- Portable and Robust
- High Performance and Multithreaded
- Automatic Garbage Collection
3. Describe Java's object-oriented programming concept.
Ans:
Java follows the OOP paradigm, using principles like Class, Object, Inheritance, Polymorphism, Encapsulation, and Abstraction to organize code into reusable and structured components.
4. What are Java’s access modifiers?
Ans:
- public: Accessible from any class
- private: Accessible within the same class only
- protected: Accessible in the same package and subclasses
- (default): Accessible only within the same package
5. How is an abstract class different from an interface?
Ans:
Interfaces define only abstract methods (before Java 8) and constants, allowing multiple inheritance. Abstract classes can include both abstract and concrete methods, constructors, and instance variables.
6. How does exception handling work in Java?
Ans:
Java handles exceptions using try-catch-finally blocks. The try block contains risky code, catch handles exceptions if they occur, and finally runs code regardless of an exception.
7. What’s the difference between checked and unchecked exceptions?
Ans:
- Checked exceptions (e.g., IOException) are validated at compile time
- Unchecked exceptions (e.g., NullPointerException) are caught during runtime
8. What is the Java Collections Framework?
Ans:
It’s a set of interfaces and classes (like List, Set, and Map) that offer ready-to-use data structures and algorithms for storing and manipulating data efficiently.
9. What is a Stream in Java?
Ans:
Streams in Java provide a functional approach to processing collections. They allow operations like filtering, mapping, and reducing without altering the original data source.
10. What is garbage collection in Java?
Ans:
Garbage Collection is Java’s automatic process of freeing memory by removing objects that are no longer in use or referenced, helping to manage memory efficiently.