Core Java Most Asked Real Time Interview Questions

Questions

  1. Can an interface have a constructor? Why or why not?
  2. What is the significance of the default keyword in Java 8 interfaces?
  3. Can a class implement multiple interfaces in Java? If yes, explain how.
  4. Can an interface extend another interface? If so, what is the use case?
  5. What happens if a class implements two interfaces with conflicting default methods?
  6. Can you explain the difference between public, private, protected, and package-private access modifiers?
  7. What is the purpose of the protected access modifier? Can you access a protected method from another package?
  8. What is the default access level for a class, method, or variable if no access modifier is specified?
  9. Can you access a private variable from a subclass?
  10. What is a functional interface? Can you give an example?
  11. Explain the concept of Lambda Expressions in Java 8. How do they improve code readability?
  12. What is the Stream API in Java 8, and how is it different from a collection?
  13. What is the difference between map() and flatMap() in Java 8 Streams?
  14. How do you use the Optional class in Java 8 to avoid NullPointerException?
  15. What are the major improvements introduced in java.time package in Java 8?
  16. Can you create an instance of a class with only static methods? How would you call the methods?
  17. What is the difference between a static method and a non-static method?
  18. Can you access a non-static variable from a static method? Why or why not?
  19. What is a static block in Java, and when is it executed?
  20. How would you call a static method in another class?
  21. Can a static variable be overridden by a subclass? Why or why not?

Questions & Answers

  1. Can an interface have a constructor? Why or why not?
  • Answer: No, an interface cannot have a constructor because it is not designed to be instantiated. Interfaces are meant to define a contract that implementing classes adhere to. Constructors are associated with creating object instances, which is not applicable for interfaces.

 

  1. What is the significance of the default keyword in Java 8 interfaces?
  • Answer: The default keyword allows interfaces to have method implementations. This was introduced in Java 8 to provide backward compatibility for interfaces when new methods are added. Classes implementing the interface can either use the default implementation or override it.

 

  1. Can a class implement multiple interfaces in Java? If yes, explain how.
  • Answer: Yes, a class can implement multiple interfaces in Java. It does so by using the implements keyword followed by a comma-separated list of interface names. Example:

class MyClass implements Interface1, Interface2 {

    // Implement methods of both interfaces

}

 

  1. Can an interface extend another interface? If so, what is the use case?
  • Answer: Yes, an interface can extend another interface using the extends keyword. This allows the child interface to inherit the methods of the parent interface, enabling modular design and reusability. Example:

interface ParentInterface {

    void method1();

}

interface ChildInterface extends ParentInterface {

    void method2();

}

 

  1. What happens if a class implements two interfaces with conflicting default methods?
  • Answer: If a class implements two interfaces with conflicting default methods, it must explicitly override the conflicting method to resolve the ambiguity. Example:

class MyClass implements Interface1, Interface2 {

    @Override

    public void defaultMethod() {

        // Custom implementation to resolve conflict

    }

}

 

  1. Can you explain the difference between public, private, protected, and package-private access modifiers?
  • Answer:
    • public: Accessible from anywhere.
    • private: Accessible only within the same class.
    • protected: Accessible within the same package and by subclasses in other packages.
    • (Default) Package-private: Accessible only within the same package.

 

  1. What is the purpose of the protected access modifier? Can you access a protected method from another package?
  • Answer: protected allows visibility to subclasses and within the same package. A protected method can be accessed from another package only through inheritance. Example:

class Parent {

    protected void display() {

        System.out.println(“Protected method”);

    }

}

class Child extends Parent {

    public void access() {

        display(); // Accessible via inheritance

    }

}

 

  1. What is the default access level for a class, method, or variable if no access modifier is specified?
  • Answer: The default access level is package-private, meaning it is accessible only within the same package.

 

  1. Can you access a private variable from a subclass?
  • Answer: No, private variables are not accessible from a subclass. However, they can be accessed indirectly through public or protected getter and setter methods.

 

  1. What is a functional interface? Can you give an example?
  • Answer: A functional interface is an interface with exactly one abstract method. It can be used as a target for lambda expressions. Example:

@FunctionalInterface

interface MyFunctionalInterface {

    void perform();

}

 

  1. Explain the concept of Lambda Expressions in Java 8. How do they improve code readability?
  • Answer: Lambda expressions provide a concise way to represent anonymous functions. They improve code readability by reducing boilerplate code for anonymous classes, especially in functional programming scenarios. Example:

(int a, int b) -> a + b;

 

  1. What is the Stream API in Java 8, and how is it different from a collection?
  • Answer: The Stream API is a sequence of elements supporting functional-style operations like filtering, mapping, and reducing. Unlike collections, streams do not store elements; they process data lazily and can be parallelized for better performance.

Key Difference:

  • Collections: Store and manage data in memory.
  • Streams: Focus on computation and transformation of data in a pipeline.

 

  1. What is the difference between map() and flatMap() in Java 8 Streams?
  • map(): Transforms each element in the stream and wraps the result in a stream of individual objects.
    • Example: Convert a list of strings to their lengths.
  • flatMap(): Transforms each element into a stream and flattens the resulting streams into a single stream.
    • Example: Flatten a list of lists into a single list.
  • Key Difference: flatMap() removes nested structures, whereas map() retains them.

 

  1. How do you use the Optional class in Java 8 to avoid NullPointerException?
  • Answer: The Optional class wraps a value that may or may not be null. It provides methods like isPresent(), ifPresent(), and orElse() to handle values safely.
    • Example:

Optional<String> optional = Optional.ofNullable(value);

optional.ifPresent(System.out::println); // Prints value if not null

String result = optional.orElse(“Default Value”); // Provides default if null

 

  1. What are the major improvements introduced in java.time package in Java 8?
  • Answer: The java.time package provides a modern API for date and time operations. Key improvements include:
    • Immutable and thread-safe classes (LocalDate, LocalTime, ZonedDateTime).
    • Better handling of time zones.
    • Parsing and formatting dates using DateTimeFormatter.
    • More intuitive APIs for operations like adding/subtracting dates.

 

  1. Can you create an instance of a class with only static methods? How would you call the methods?
  • Answer: While you can technically create an instance of such a class, it is unnecessary. Static methods are called using the class name directly.
    • Example:

MyClass.staticMethod();

 

  1. What is the difference between a static method and a non-static method?
  • Answer:
    • Static Method:
      • Belongs to the class, not an instance.
      • Can be called using the class name.
      • Cannot access non-static members directly.
    • Non-Static Method:
      • Belongs to an instance.
      • Can access both static and non-static members.

 

  1. Can you access a non-static variable from a static method? Why or why not?
  • Answer: No, you cannot directly access a non-static variable from a static method because static methods are associated with the class, not an instance. Non-static variables require an object instance to access.

 

  1. What is a static block in Java, and when is it executed?
  • Answer: A static block is a block of code that is executed when the class is loaded into memory. It is typically used to initialize static variables.
    • Example:

static {

    System.out.println(“Static block executed”);

}

 

  1. How would you call a static method in another class?
  • Answer: You call a static method in another class using the class name followed by the method name.
    • Example:

AnotherClass.staticMethod();

 

  1. Can a static variable be overridden by a subclass? Why or why not?
  • Answer: No, static variables cannot be overridden because they are associated with the class rather than any instance. However, they can be hidden by declaring a static variable with the same name in the subclass.
  • Example of hiding:

class Parent {

    static int value = 10;

}

class Child extends Parent {

    static int value = 20;

}

System.out.println(Parent.value); // 10

System.out.println(Child.value);  // 20