Comprehensive Guide: Top 50 Java Interview Questions and Example Answers
1. What is Java?
Answer: Java is a high-level, class-based, object-oriented programming language designed to have as few implementation dependencies as possible.
2. What are the features of Java?
Answer: Some key features of Java include:
- Object-Oriented
- Platform-Independent
- Simple and Secure
- Robust
- Multithreading
3. What is JVM, JRE, and JDK?
Answer:
- JVM (Java Virtual Machine): Executes Java bytecode.
- JRE (Java Runtime Environment): Provides libraries for running Java applications.
- JDK (Java Development Kit): Contains tools for Java development, including JRE and compiler.
4. What is a class in Java?
Answer: A class is a blueprint for creating objects in Java. Example:
class Dog { String name; void bark() { System.out.println("Woof!"); } }
5. What is an object in Java?
Answer: An object is an instance of a class. Example:
Dog dog = new Dog();
6. What is Inheritance in Java?
Answer: Inheritance allows a class to inherit properties and methods from another class. Example:
class Animal {} class Dog extends Animal {}
7. What is Polymorphism in Java?
Answer: Polymorphism allows objects to be treated as instances of their parent class. Example:
Animal a = new Dog();
8. What is Encapsulation in Java?
Answer: Encapsulation is the bundling of data and methods into a single unit, the class, and restricting access to some of the object's components.
9. What is Abstraction in Java?
Answer: Abstraction is hiding complex implementation details and showing only the necessary features of an object. Example:
abstract class Animal { abstract void sound(); }
10. What are interfaces in Java?
Answer: Interfaces are abstract types used to specify methods that a class must implement. Example:
interface Animal { void sound(); }
11. What is the difference between Abstract Class and Interface?
Answer:
- An abstract class can have method implementations, whereas an interface cannot.
- A class can implement multiple interfaces but can only extend one abstract class.
12. What is the final keyword in Java?
Answer: The final keyword can be applied to variables, methods, and classes, indicating that they cannot be modified, overridden, or inherited.
13. What is a static method in Java?
Answer: A static method belongs to the class rather than the instance of the class. Example:
class Example { static void display() { System.out.println("Static method"); } }
14. What is the this keyword in Java?
Answer: The this keyword refers to the current object in a method or constructor.
15. What is the super keyword in Java?
Answer: The super keyword is used to refer to the parent class object and to access parent class methods and constructors.
16. What is method overloading in Java?
Answer: Method overloading is having multiple methods with the same name but different parameter lists. Example:
class MathOperations { int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } }
17. What is method overriding in Java?
Answer: Method overriding occurs when a subclass provides a specific implementation of a method declared in its superclass. Example:
class Animal { void sound() { System.out.println("Animal sound"); } } class Dog extends Animal { void sound() { System.out.println("Bark"); } }
18. What is a constructor in Java?
Answer: A constructor is a block of code used to initialize objects. It has the same name as the class and no return type.
19. What is the default constructor?
Answer: A default constructor is provided by Java if no constructors are explicitly defined by the programmer.
20. What are the access modifiers in Java?
Answer:
- Private: Only accessible within the same class.
- Default: Accessible within the same package.
- Protected: Accessible within the same package and subclasses.
- Public: Accessible from anywhere.
21. What is the difference between == and equals()?
Answer: == compares reference equality, while equals() compares content.
22. What is a package in Java?
Answer: A package is a namespace that organizes related classes and interfaces. Example:
package com.example;
23. What is exception handling in Java?
Answer: Exception handling is the process of managing runtime errors using try-catch blocks. Example:
try { int result = 10 / 0; } catch (ArithmeticException e) { System.out.println("Cannot divide by zero"); }
24. What is a checked exception in Java?
Answer: Checked exceptions are exceptions that are checked at compile-time. Example: IOException.
25. What is an unchecked exception?
Answer: Unchecked exceptions are exceptions that are checked at runtime. Example: NullPointerException.
26. What is the difference between throw and throws?
Answer:
- throw is used to explicitly throw an exception.
- throws is used to declare exceptions in a method signature.
27. What is the difference between an error and an exception?
Answer: Errors are serious problems that an application should not try to handle, while exceptions are conditions that a program can catch.
28. What are Java collections?
Answer: Java collections provide a way to store and manage groups of objects. Example: ArrayList, HashMap, etc.
29. What is the difference between ArrayList and LinkedList?
Answer: ArrayList is backed by an array, while LinkedList is implemented as a doubly-linked list.
30. What is a HashMap in Java?
Answer: HashMap is a collection that stores key-value pairs where each key is unique. Example:
HashMap<String, Integer> map = new HashMap<>(); map.put("one", 1);
31. What is a Set in Java?
Answer: A Set is a collection that does not allow duplicate elements.
32. What is the difference between HashSet and TreeSet?
Answer: HashSet is unordered, while TreeSet maintains a sorted order.
33. What is multithreading in Java?
Answer: Multithreading is the concurrent execution of two or more threads. Example:
class MyThread extends Thread { public void run() { System.out.println("Thread is running"); } }
34. What is synchronization in Java?
Answer: Synchronization ensures that only one thread accesses a resource at a time.
35. What is the volatile keyword in Java?
Answer: The volatile keyword ensures that a variable is read from the main memory and not from a thread's local cache.
36. What is garbage collection in Java?
Answer: Garbage collection is the process of automatically reclaiming memory by removing unused objects.
37. What is the purpose of finalize() method in Java?
Answer: The finalize() method is invoked before an object is garbage collected.
38. What is a lambda expression in Java?
Answer: Lambda expressions provide a concise way to express behavior. Example:
(int x, int y) -> x + y
39. What is Stream API in Java?
Answer: The Stream API provides functional programming features to process collections of objects.
40. What is the Optional class in Java?
Answer: Optional is a container object used to avoid null pointer exceptions. Example:
Optional<String> value = Optional.of("Hello");
41. What is reflection in Java?
Answer: Reflection is the ability to inspect and manipulate classes and objects at runtime.
42. What is the Singleton pattern in Java?
Answer: The Singleton pattern restricts the instantiation of a class to one object. Example:
class Singleton { private static Singleton instance; private Singleton() {} public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } }
43. What is the Factory pattern in Java?
Answer: The Factory pattern provides a way to create objects without specifying the exact class of the object. Example:
class AnimalFactory { public Animal createAnimal(String type) { if (type.equals("Dog")) { return new Dog(); } return null; } }
44. What is Dependency Injection in Java?
Answer: Dependency Injection is a design pattern that allows objects to be injected into a class, rather than being created by the class.
45. What is the difference between String, StringBuilder, and StringBuffer?
Answer:
- String is immutable.
- StringBuilder is mutable and non-synchronized.
- StringBuffer is mutable and synchronized.
46. What is a Functional Interface in Java?
Answer: A functional interface is an interface with a single abstract method. Example:
@FunctionalInterface interface MyFunction { void apply(); }
47. What is the transient keyword in Java?
Answer: The transient keyword is used to indicate that a variable should not be serialized.
48. What is the synchronized keyword in Java?
Answer: The synchronized keyword ensures that a method or block is accessed by only one thread at a time.
49. What is the difference between wait() and sleep() in Java?
Answer:
- wait() releases the lock on the object.
- sleep() pauses the thread but does not release the lock.
50. What is a Stream in Java?
Answer: Streams allow functional-style operations on collections. Example:
List<String> list = Arrays.asList("A", "B", "C"); list.stream().filter(s -> s.equals("A")).forEach(System.out::println);
This set of questions covers both beginner and advanced concepts in Java to prepare for an interview.