Interview Questions & Answers

Java

Using the super keyword along with the interface name.

Example:-

interface Vehicle {
    default void print() {
        System.out.println("I am a vehicle!");
    }
}
class Car implements Vehicle {
    public void print() {
        Vehicle.super.print();
    }
}

A Static Method is a Utility method or Helper method, which is associated with a class (or interface). It is not associated with any object.

We need Static Methods because of the following reasons:

  • We can keep Helper or Utility methods specific to an interface in the same interface rather than in a separate Utility class.
  • We do not need separate Utility Classes like Collections, Arrays, etc to keep Utility methods.
  • Clear separation of Responsibilities. That is we do not need one Utility class to keep all Utility methods of Collection API like Collections etc.
  • Easy to extend the API.
  • Easy to Maintain the API.

A default method is a method with an implementation – which can be found in an interface.
We can use a default method to add new functionality to an interface while maintaining backward compatibility with classes that are already implementing the interface:
public interface Vehicle {
    String getBrand();

    String speedUp();

    String slowDown();

    default String turnAlarmOn() {
        return "Turning the vehice alarm on.";
    }

    default String turnAlarmOff() {
        return "Turning the vehicle alarm off.";
    }
}


Usually, when a new abstract method is added to an interface, all implementing classes will break until they implement the new abstract method. In Java 8, this problem has been solved by the use of the default method.
For example, the Collection interface does not have a forEach method declaration. Thus, adding such a method would simply break the whole collections API.
Java 8 introduces the default method so that the Collection interface can have a default implementation of the forEach method without requiring the classes implementing this interface to implement the same.

  • Null checks are not required.
  • No more NullPointerException at run-time.
  • We can develop clean and neat APIs.
  • No more Boilerplate code

Optional is a container object which is used to contain not-null objects. An optional object is used to represent null with an absent value.
The Optional class has various utility methods to facilitate code to handle values as ‘available’ or ‘not available’ instead of checking null values.
The purpose of the Optional class is to provide a type-level solution for representing optional values instead of using null references.

Collection API:-

  • It’s available since Java 1.2
  • It is used to store Data (A set of Objects).
  • We can use both spliterator and Iterator to iterate elements.
  • It is used to store an unlimited number of elements.
  • Typically, it uses the External Iteration concept to iterate Elements such as Iterator.
  • Collection Object is constructed Eagerly.
  • We add elements to the Collection object only after it is computed completely.

Stream API:-

  • It is introduced in Java SE 8
  • It is used to compute data (Computation on a set of Objects).
  • We can’t use Spliterator or Iterator to iterate elements. We can use forEach to perform an action for each element of this stream.
  • Stream API is used to process the elements of a Collection.
  • Stream API uses internal iteration to iterate Elements, using the forEach method.
  • Stream Object is constructed Lazily.
  • We can add elements to Stream Object without any prior computation. That means Stream objects are computed on-demand.

The stream represents a sequence of objects from a source such as a collection, which supports aggregate operations.
You can use Stream to filter, collect, print, and convert from one data structure to another, etc.

Stream does not store elements. It simply conveys elements from a source such as a data structure, an array, or an I/O channel, through a pipeline of computational operations.

Stream is functional in nature. Operations performed on a stream do not modify its source. For example, filtering a Stream obtained from a collection produces a new Stream without the filtered elements, rather than removing elements from the source collection.
Creating Empty String:
Stream<String> stream = Stream.empty();
stream.forEach(System.out::println);

  • Reference to a static method. For example:
ContainingClass::staticMethodName
  • Reference to an instance method of a particular object. For example:
containingObject::instanceMethodName
  • Reference to an instance method of an arbitrary object of a particular type. For example:
ContainingType::methodName
  • Reference to a constructor. for example:
ClassName::new