1. What is Java?
Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle). It is platform-independent, meaning code written in Java can run on any device that has a Java Virtual Machine (JVM).
2. What are the main features of Java?
- Platform Independence
- Object-Oriented
- Simplicity
- Secure
- Portable
- Robust and Reliable
- Multithreaded
- Architecture-neutral
3. What is JVM, JRE, and JDK?
- JVM (Java Virtual Machine): Executes Java bytecode.
- JRE (Java Runtime Environment): JVM + libraries; used to run Java applications.
- JDK (Java Development Kit): JRE + development tools (like compiler, debugger).
Object-Oriented Programming (OOP) Concepts
4. What are the pillars of OOP in Java?
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
5. What is encapsulation?
Encapsulation is the process of wrapping data (variables) and code (methods) together as a single unit, and restricting access to some of the object's components using access modifiers.
6. What is inheritance?
Inheritance is a mechanism where one class (child/subclass) acquires the properties and behavior of another class (parent/superclass).
Java Syntax and Basic Concepts
7. What are Java data types?
Primitive data types: byte, short, int, long, float, double, char, boolean.
Non-primitive data types: Classes, Interfaces, Arrays, etc.
8. What is a constructor?
A constructor is a special method used to initialize objects. It has the same name as the class and no return type.
9. What is method overloading?
Method overloading is defining multiple methods with the same name but different parameters within the same class.
Advanced Java Questions
10. What is polymorphism?
Polymorphism allows objects of different classes to be treated as instances of a common superclass. It is achieved through method overriding and overloading.
11. What is an abstract class?
An abstract class cannot be instantiated and can contain abstract methods (without implementation) and concrete methods.
12. What is an interface?
An interface is a contract that defines methods without implementations. Classes implement interfaces to provide behavior.
13. What are exceptions in Java?
Exceptions are events that disrupt normal program flow. Java provides try-catch blocks to handle exceptions.
Concurrency and Multi-threading
14. How do you create a thread in Java?
- Extending the
Threadclass - Implementing the
Runnableinterface
15. What is synchronization?
Synchronization is a mechanism to control access to shared resources by multiple threads to prevent data inconsistency.
Java Collections Framework
16. Name some common collections in Java.
- List (ArrayList, LinkedList)
- Set (HashSet, TreeSet)
- Map (HashMap, TreeMap)
17. What is the difference between ArrayList and LinkedList?
- ArrayList: Resizable array, fast random access, slow insertion/deletion.
- LinkedList: Doubly linked list, fast insertion/deletion, slower random access.