Java Interview Questions


Java Interview Questions for Beginners

  1. Is Java an object-oriented programming language?

    Yes, Java is an object-oriented programming (OOP) language. It supports key OOP principles such as:

    • Encapsulation: Bundling data and methods that operate on the data within classes, restricting direct access to some components.

    • Inheritance: Allowing classes to inherit properties and methods from other classes, promoting code reuse.

    • Polymorphism: Enabling objects to be treated as instances of their parent class, allowing for method overriding and overloading.

    • Abstraction: Providing a way to define abstract classes and interfaces, separating implementation from interface.

    However, it is important to note that Java is not purely object-oriented due to the inclusion of primitive data types (like int, char, etc.) and static methods and variables, which are not tied to instances of classes.

  2. Is Java Platform Independent if then how?

    Yes, Java is platform-independent due to its "Write Once, Run Anywhere" (WORA) capability. When Java code is compiled, it transforms into bytecode, which is an intermediate form stored in `.class` files. ,

    This bytecode can be executed on any device that has a Java Virtual Machine (JVM) installed, regardless of the underlying operating system. The JVM interprets or compiles the bytecode into machine code suitable for the specific platform, allowing the same Java application to run seamlessly on Windows, macOS, Linux, and others without modification.
    This architecture ensures that Java applications are highly portable and versatile across different environments.

  3. What is the difference between JDK, JRE, and JVM?

    The JDK (Java Development Kit) is a software development kit used to develop Java applications.
    The JRE (Java Runtime Environment) is part of the JDK and provides the libraries, Java Virtual Machine (JVM), and other components needed to run applications written in Java. The JVM (Java Virtual Machine) is an abstract computing machine that enables a computer to run Java programs by converting bytecode into machine code.

  4. What are the main features of Java?

    Java is known for its features such as platform independence, object-oriented programming, automatic memory management (garbage collection), multithreading support, and a rich API. It also emphasizes security and performance.

  5. What is a Java class?

    A Java class is a blueprint from which individual objects are created. It contains fields (attributes) and methods (functions) to define the behavior of the objects created from it.

  6. What is inheritance in Java?

    Inheritance is a mechanism where one class (subclass) inherits the attributes and methods of another class (superclass). It promotes code reusability and establishes a relationship between classes.

    Types of Inheritance

    • Single Inheritance: A subclass inherits from one superclass.

    • Multilevel Inheritance: A subclass is derived from another subclass, creating a chain of inheritance.

    • Hierarchical Inheritance: Multiple subclasses inherit from a single superclass.

    • Multiple Inheritance: Java does not support this directly with classes but allows it through interfaces.

    • Hybrid Inheritance: A combination of two or more types of inheritance, achievable through interfaces.

    Code Examples

    Single Inheritance:
    class Animal {
        void eat() {
            System.out.println("Eating...");
        }
    }
    
    class Dog extends Animal {
        void bark() {
            System.out.println("Barking...");
        }
            
    Multilevel Inheritance:
    class Animal {
                void eat() {
                    System.out.println("Eating...");
                }
            }
            
            class Dog extends Animal {
                void bark() {
                    System.out.println("Barking...");
                }
            }
            
            class Puppy extends Dog {
                void weep() {
                    System.out.println("Weeping...");
                }
                    
    Hierarchical Inheritance:
    
            class Animal {
                void eat() {
                    System.out.println("Eating...");
                }
            }
            
            class Dog extends Animal {
                void bark() {
                    System.out.println("Barking...");
                }
            }
            
            class Cat extends Animal {
                void meow() {
                    System.out.println("Meowing...");
                }
                    
    Multiple Inheritance (through Interfaces):
     interface CanRun {
        void run();
    }
    
    interface CanBark {
        void bark();
    }
    
    class Dog implements CanRun, CanBark {
        public void run() {
            System.out.println("Running...");
        }
        
        public void bark() {
            System.out.println("Barking...");
        }
            
    Hybrid Inheritance:

    Java does not support hybrid inheritance directly through classes, but it can be achieved using interfaces.

  7. What are interfaces in Java?

    Interfaces in Java are abstract types that allow you to specify methods that a class must implement. They define a contract for classes, ensuring that certain methods are implemented, while allowing different classes to implement those methods in their own way.

    Key Features of Interfaces:

    • Method Declaration: Interfaces can contain method signatures, but no method bodies (until Java 8, which introduced default methods).

    • Multiple Inheritance: A class can implement multiple interfaces, providing a way to achieve multiple inheritance in Java.

    • Constants: Interfaces can contain constants (static final variables), which are implicitly public, static, and final.

    • Polymorphism: Interfaces allow for polymorphic behavior, as classes can be treated as their interface type.

    • Default and Static Methods: Since Java 8, interfaces can have default and static methods, allowing code reuse in interfaces.

    Example:

     interface Animal {
        void eat();
        void sound();
    }
    
    class Dog implements Animal {
        public void eat() {
            System.out.println("Dog is eating.");
        }
    
        public void sound() {
            System.out.println("Bark");
        }
    }
    
    class Cat implements Animal {
        public void eat() {
            System.out.println("Cat is eating.");
        }
    
        public void sound() {
            System.out.println("Meow");
        }
    }
            

    Usage:

    Interfaces are used to achieve abstraction and to define a common behavior across different classes. They are widely used in frameworks and APIs.

  8. What is polymorphism?

    Polymorphism is the ability of a single function or method to work in different ways depending on the context. It can be achieved through method overloading and method overriding.

  9. What is encapsulation?

    Encapsulation is the principle of wrapping data (attributes) and methods (functions) within a single unit, restricting direct access to some of an object's components.

  10. What is the difference between `==` and `equals()` in Java?

    The `==` operator checks for reference equality, while `equals()` checks for value equality.

  11. What is a constructor in Java?

    A constructor is a special method called when an object is instantiated. It has the same name as the class and does not have a return type.

  12. What is the purpose of the `final` keyword in Java?

    The `final` keyword makes a variable a constant, prevents method overriding, or prevents inheritance of a class.

  13. What is the Java Collections Framework?

    The Java Collections Framework is a unified architecture for representing and manipulating collections of objects, including interfaces like `List`, `Set`, and `Map`.

  14. What is the difference between `ArrayList` and `LinkedList`?

    `ArrayList` is a resizable array implementation allowing fast random access, while `LinkedList` is a doubly-linked list allowing more efficient insertions and deletions.

  15. What is a `Map` in Java?

    A `Map` maps keys to values and does not allow duplicate keys. Common implementations include `HashMap`, `TreeMap`, and `LinkedHashMap`.

  16. What is the Java Collections Framework?

    The Java Collections Framework is a unified architecture for representing and manipulating collections of objects. It includes several key interfaces, each designed for specific types of collections.

    Key Interfaces in the Java Collections Framework:

    1. List

    The List interface represents an ordered collection (also known as a sequence) that allows duplicate elements. It provides methods to manipulate the size of the list and to access elements by their index.

     List<String> myList = new ArrayList<>();
            myList.add("Apple");
            myList.add("Banana");
            myList.add("Apple"); // Duplicates allowed
            String firstItem = myList.get(0); // "Apple"
                
    2. Set

    The Set interface represents a collection that does not allow duplicate elements. It models the mathematical set abstraction and provides operations for adding, removing, and checking elements.

     Set<String> mySet = new HashSet<>();
            mySet.add("Apple");
            mySet.add("Banana");
            mySet.add("Apple"); // Duplicates ignored
            boolean hasApple = mySet.contains("Apple"); // true
                
    3. Map

    The Map interface represents a collection of key-value pairs. It does not allow duplicate keys, and each key maps to exactly one value. Maps provide methods for inserting, removing, and accessing values based on keys.

     Map<String, Integer> myMap = new HashMap<>();
            myMap.put("Apple", 1);
            myMap.put("Banana", 2);
            int appleCount = myMap.get("Apple"); // 1
                
    4. Queue

    The Queue interface represents a collection designed for holding elements prior to processing. It provides methods for adding, removing, and inspecting elements, typically following the first-in, first-out (FIFO) principle.

    Queue<String> myQueue = new LinkedList<>();
                myQueue.add("First");
                myQueue.add("Second");
                String nextItem = myQueue.poll(); // "First"
                    
    5. Deque

    The Deque interface extends the Queue interface and represents a double-ended queue that allows elements to be added or removed from both ends.

      Deque<String> myDeque = new ArrayDeque<>();
            myDeque.addFirst("First");
            myDeque.addLast("Second");
            String firstItem = myDeque.removeFirst(); // "First"
                
    6. SortedSet

    The SortedSet interface extends Set and provides a total ordering of its elements. It allows you to access the first and last elements and view subsets of the collection.

      SortedSet<Integer> sortedSet = new TreeSet<>();
        sortedSet.add(3);
        sortedSet.add(1);
        sortedSet.add(2);
        Integer first = sortedSet.first(); // 1
            
    7. SortedMap

    The SortedMap interface extends Map and provides a total ordering on its keys. It allows you to access the first and last keys and view key-value pairs in sorted order.

     SortedMap<String, Integer> sortedMap = new TreeMap<>();
            sortedMap.put("Banana", 2);
            sortedMap.put("Apple", 1);
            String firstKey = sortedMap.firstKey(); // "Apple"
                
  17. How does HashMap work internally in Java?

    A HashMap in Java is a part of the Java Collections Framework that implements the Map interface. It stores key-value pairs and allows for fast retrieval based on keys. The internal workings of a HashMap can be broken down into several key components:

    1. Hashing

    When you add a key-value pair to a HashMap, the key is processed through a hashing function. This function computes a hash code (an integer) that determines the index in the underlying array where the value will be stored.

    int hashCode = key.hashCode();
            int index = hashCode % arrayLength; // Index in the array
                

    2. Buckets

    The underlying structure of a HashMap is an array of "buckets." Each bucket can store multiple entries, usually in a linked list or a tree structure (in Java 8 and later). If two keys generate the same hash code (a situation known as a "collision"), they will be stored in the same bucket.

    3. Handling Collisions

    When a collision occurs, the HashMap uses the following methods to handle it:

    • Linked List: In the case of fewer collisions, entries are stored in a linked list within the same bucket.
    • Tree Structure: If the number of entries in a bucket exceeds a certain threshold (usually 8), the linked list is converted into a balanced binary tree for faster lookups.

    4. Load Factor and Rehashing

    The load factor is a measure that determines when to increase the size of the HashMap. The default load factor is 0.75, which means when 75% of the buckets are filled, the HashMap will be resized (rehashing). This involves creating a new, larger array and rehashing all existing entries into the new array.

    5. Performance

    The average time complexity for basic operations (get, put, remove) is O(1) when there are few collisions. However, in the worst-case scenario (when many collisions occur), the time complexity can degrade to O(n) if all entries are stored in a single bucket.

    6. Example

    Here is a simple example of using a HashMap in Java:

      HashMap<String, Integer> map = new HashMap<>();
            map.put("Apple", 1);
            map.put("Banana", 2);
            map.put("Orange", 3);
            int value = map.get("Banana"); // Returns 2
                      
                            
  18. What are the access modifiers in Java?

    Java has four access modifiers: `public`, `protected`, `private`, and default (no modifier), which control visibility.

  19. What is method overloading?

    Method overloading occurs when multiple methods in the same class have the same name but different parameters.

  20. What is method overriding?

    Method overriding occurs when a subclass provides a specific implementation for a method already defined in its superclass.

  21. What are lambda expressions in Java?

    Lambda expressions provide a clear and concise way to represent functional interfaces, allowing you to write anonymous methods.

  22. What is the significance of the `stream` API?

    The `stream` API allows for functional-style operations on collections, enabling efficient processing of data with operations such as filtering, mapping, and reducing.

  23. What is the `Optional` class?

    The `Optional` class is a container object used to contain not-null objects, providing methods to avoid null checks and handle potential null values gracefully.

  24. What is a functional interface?

    A functional interface is an interface that contains exactly one abstract method and can be used as the assignment target for lambda expressions.

  25. What is garbage collection in Java?

    Garbage collection is the process by which Java automatically manages memory by reclaiming memory allocated to objects that are no longer referenced.

  26. What is the Java `main` method?

    The `main` method is the entry point of any Java application, defined as `public static void main(String[] args)`.

  27. What is an abstract class?

    An abstract class is a class that cannot be instantiated and may contain abstract methods. It serves as a base for subclasses.

  28. What is the `volatile` keyword in Java?

    The `volatile` keyword indicates that a variable's value will be modified by different threads, ensuring visibility across threads.

  29. What is the difference between `String`, `StringBuilder`, and `StringBuffer`?

    `String` is immutable; `StringBuilder` is mutable and not synchronized; `StringBuffer` is mutable and synchronized.

  30. What is a String in Java?

    A String in Java is a sequence of characters that is treated as a single object. Strings are immutable, meaning once created, their values cannot be changed.

  31. How is a String created in Java?

    In Java, a String can be created in two primary ways:

    • String Literals: The most common way to create a String is by using string literals. For example, you can create a String like this:
      String str = "Hello";
      When you use string literals, Java creates a String object in the String pool, which is a special area of memory that stores String literals for efficient memory use and faster access.

    • Using the `new` Keyword: Alternatively, you can create a String using the new keyword. For example:
      String str = new String("Hello");
      This approach creates a new String object in the heap memory, even if an identical String exists in the String pool. This method is less commonly used because it can lead to unnecessary memory usage.

    Both methods will create a String containing the same sequence of characters, but they differ in terms of memory allocation and performance. In general, it's recommended to use string literals for most cases due to their simplicity and efficiency.

  32. What is the difference between `String`, `StringBuilder`, and `StringBuffer`?

    `String` is immutable, meaning it cannot be changed. `StringBuilder` is mutable and not synchronized, making it faster for single-threaded use. `StringBuffer` is also mutable but synchronized, making it thread-safe but slower than `StringBuilder`.

  33. How do you compare two strings in Java?

    To compare two strings, use the `equals()` method for value comparison and `==` for reference comparison. For case-insensitive comparison, use `equalsIgnoreCase()`.

  34. How can you convert a String to an integer in Java?

    You can convert a String to an integer using the `Integer.parseInt(String s)` method or `Integer.valueOf(String s)` method.

  35. What is the purpose of the `String.intern()` method?

    The `intern()` method returns a canonical representation of the string object. It ensures that all identical strings share the same memory location in the string pool, saving memory.

  36. How do you extract a substring from a String?

    You can extract a substring using the `substring(int beginIndex, int endIndex)` method, where `beginIndex` is inclusive and `endIndex` is exclusive.

  37. What are the common methods available in the String class?

    Common methods include `length()`, `charAt(int index)`, `indexOf(String str)`, `toUpperCase()`, `toLowerCase()`, `trim()`, `replace()`, and `split(String regex)`.

  38. What are the common methods of the String class in Java?

    1. substring()

    The substring() method is used to extract a part of a String.

     String str = "Hello, World!";
            String sub = str.substring(7); // "World!"
            String sub2 = str.substring(0, 5); // "Hello"
                    

    2. indexOf()

    The indexOf() method returns the index of the first occurrence of a specified character or substring.

     String str = "Hello, World!";
            int index = str.indexOf("o"); // 4
            int index2 = str.indexOf("World"); // 7
                    

    3. replace()

    The replace() method is used to replace all occurrences of a specified character or substring with another character or substring.

      String str = "Hello, World!";
            String replaced = str.replace("World", "Java"); // "Hello, Java!"
                    

    4. toUpperCase() and toLowerCase()

    The toUpperCase() and toLowerCase() methods convert the entire String to upper or lower case, respectively.

    String str = "Hello, World!";
            String upper = str.toUpperCase(); // "HELLO, WORLD!"
            String lower = str.toLowerCase(); // "hello, world!"
                    

    5. trim()

    The trim() method removes leading and trailing whitespace from the String.

     String str = "   Hello, World!   ";
        String trimmed = str.trim(); // "Hello, World!"
                

    6. charAt()

    The charAt() method returns the character at a specified index.

     String str = "Hello";
            char ch = str.charAt(1); // 'e'
                    

    7. length()

    The length() method returns the number of characters in the String.

      String str = "Hello, World!";
            int length = str.length(); // 13
                    

    8. split()

    The split() method splits the String into an array of substrings based on a specified delimiter.

     String str = "apple,banana,cherry";
          String[] fruits = str.split(","); // ["apple", "banana", "cherry"]
                    
  39. What is String concatenation, and how is it performed?

    String concatenation is the process of combining two or more strings. It can be performed using the `+` operator or the `concat(String str)` method. For multiple concatenations, using `StringBuilder` is recommended for performance.

  40. How can you check if a String contains a specific sequence of characters?

    You can use the `contains(CharSequence sequence)` method to check if a String contains a specific sequence of characters.

  41. What is the purpose of the `String.format()` method?

    The `String.format()` method allows you to create formatted strings using placeholders, making it easy to format numbers, dates, and other data types into a String.

  42. How do you convert a character array to a String?

    You can convert a character array to a String using the `String` constructor: `String str = new String(charArray);` or by using `String.valueOf(charArray);`.

  43. What are lambda expressions in Java 8?

    Lambda expressions are a way to provide clear and concise syntax for writing anonymous methods. They enable you to express instances of single-method interfaces (functional interfaces) in a more readable way.

     List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
                names.forEach(name -> System.out.println(name));
                        
  44. What is the Stream API in Java 8?

    The Stream API allows you to process sequences of elements (like collections) in a functional style. It provides methods for filtering, mapping, and reducing data.

     List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
        List<String> filteredNames = names.stream()
                                             .filter(name -> name.startsWith("A"))
                                             .collect(Collectors.toList());
                
  45. What is a functional interface?

    A functional interface is an interface that contains exactly one abstract method. They can be used as the assignment target for a lambda expression.

     @FunctionalInterface
                interface MyFunctionalInterface {
                    void myMethod();
                         }
            
                     MyFunctionalInterface myFunc = () -> System.out.println("Hello, World!");
                     myFunc.myMethod();
                    
  46. What are default methods in interfaces?

    Default methods allow you to add new methods to interfaces without affecting the classes that implement them. They can be defined using the default keyword.

    interface MyInterface {
    
             default void myDefaultMethod() {
            System.out.println("This is a default method.");
                          }
                     }
                     class MyClass implements MyInterface {}
                            
                     MyClass obj = new MyClass();
                     obj.myDefaultMethod();             
                           
             
  47. What is method reference in Java 8?

    Method references provide a way to refer to methods without executing them. They are a shorthand notation of a lambda expression to call a method.

     List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
            names.forEach(System.out::println);
                    
  48. How does the Optional class work in Java 8?

    The Optional class is a container that may or may not contain a non-null value. It helps to avoid NullPointerExceptions and provides methods for handling absent values gracefully.

      Optional<String> optionalName = Optional.ofNullable(getName());
            optionalName.ifPresent(name -> System.out.println(name));
                    
  49. What is the purpose of the new Date and Time API in Java 8?

    Java 8 introduced a new Date and Time API to address the shortcomings of the old java.util.Date and java.util.Calendar classes. It provides a more comprehensive and user-friendly way to work with dates and times.

    
                            LocalDate today = LocalDate.now();
                            System.out.println("Today's date: " + today);
                                    
  50. How can you use the Collectors class in Java 8?

    The Collectors class provides various implementations of the Collector interface to facilitate the reduction of data in streams.

     List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
            String concatenatedNames = names.stream()
                                              .collect(Collectors.joining(", "));
            System.out.println(concatenatedNames);
                    
  51. What is the difference between String, StringBuilder, and StringBuffer?

    String is immutable, while StringBuilder and StringBuffer are mutable. StringBuilder is not synchronized and is faster, while StringBuffer is synchronized and thread-safe.

     String str = "Hello";
                StringBuilder stringBuilder = new StringBuilder("Hello");
                stringBuilder.append(" World!");
                StringBuffer stringBuffer = new StringBuffer("Hello");
                stringBuffer.append(" World!");
                System.out.println(str); // Hello
                System.out.println(stringBuilder); // Hello World!
                System.out.println(stringBuffer); // Hello World!
                        
  52. How do you reverse a string in Java?

    You can reverse a string using StringBuilder or by converting it to a char array and swapping the characters.

    
                String original = "Hello";
                String reversed = new StringBuilder(original).reverse().toString();
                System.out.println(reversed); // olleH
                        
  53. How do you check if a string contains another string?

    You can use the contains method of the String class.

    
            String str = "Hello, World!";
            boolean contains = str.contains("World");
            System.out.println(contains); // true
                    
  54. How do you compare two strings in Java?

    Use equals for content comparison and compareTo for lexicographical comparison.

        String str1 = "Hello";
        String str2 = "hello";
        boolean isEqual = str1.equals(str2);
        boolean isLexicographicallyLess = str1.compareTo(str2) < 0;
        System.out.println(isEqual); // false
        System.out.println(isLexicographicallyLess); // true
                
  55. How can you convert a string to an integer?

    You can use the Integer.parseInt() method to convert a string to an integer.

       String numberString = "123";
        int number = Integer.parseInt(numberString);
        System.out.println(number); // 123
                
  56. What is the purpose of the String.split() method?

    The split() method is used to split a string into an array of substrings based on a specified delimiter.

     String str = "Java,Python,C++,JavaScript";
            String[] languages = str.split(",");
            for (String language : languages) {
                System.out.println(language);
            }
            // Output:
            // Java
            // Python
            // C++
            // JavaScript
                    
  57. How can you remove whitespace from a string?

    You can use the trim() method to remove leading and trailing whitespace from a string.

    String str = "   Hello World!   ";
            String trimmed = str.trim();
            System.out.println(trimmed); // "Hello World!"