Sunday, November 30, 2014

Java Interview Questions

Removed
Here few questions that can be asked in job interviews about Java.

Java interview question for experts


Actual Java Version:

Java 21 has arrived in a production release with 15 features including virtual threads, a generational Z garbage collector, and a key encapsulation mechanism API.


Explain Encapsulation:

Encapsulation in java is the process of wrapping code and data inside a single unit, and make this unit accessible or not. This helps to protect the code and give access to use it only through getters and setters that give limits to change variables and data.


Encapsulation access modifiers

In Java, the terms "public," "protected," and "private" provide varied degrees of visibility and encapsulation. In Java:

  • "public" makes a member accessible from any class, 
  • "protected" permits access inside the same package and by subclasses,
  • "private" restricts access to only within the declaring class
  • the default "package-private" gives access only within the package

Abstract class

An abstract class is that which can have one or more than one abstract methods. Abstract class is used to provide abstraction by hiding implementation and leave it on its child class.

  •  Abstract methods are those which don't have implementation/body rather only has its declaration. 
  • it necessarily have abstract keyword.
  • we can't instantiate our abstract class.
  • all variables inside abstract class must be static.

Explain Static in java:

static is a keyword which makes any variable or method a class level variable or class level method ,means we can access it by our class name without making object of class (without instantiating the class).

Collections in java:

List interface: List interface extends the Collection interface, and it is an ordered collection of objects. It contains duplicate elements.

Set interface: Set (java.util.Set) interface is a collection which cannot contain duplicate elements.

Queue interface: Queue (java.util.Queue) interface defines queue data structure, which stores the elements in the form FIFO

Dequeue interface: it is a double-ended-queue

Map interface: A Map (java.util.Map) represents a key, value pair storage of elements. Map interface does not implement the Collection interface


Collections in java


Lambda in java

Lambda expressions are a concise way to represent anonymous functions. They provide implementation of functional interface. Lambda expression allow hence to pass a function as argument and return values from methods.


lambda expressions are a concise way to represent anonymous functions. They allow us to pass functions as arguments to methods and return them as values from methods. An interface which has only one abstract method is called functional interface. Java provides an annotation @FunctionalInterface, which is used to declare an interface as functional interface.

  1. (argument-list) -> {body}


Java functional interface: annotation @FunctionalInterface

A functional interface contains only a single abstract method which is not implemented. It can contains default and static method that have implementation

Functional interfaces are significant because they are the only types that can serve as the context for a lambda expression or method reference in Java 8. Java provide an annotation for them; @functionalInterface.

Here some example of Default functional interfaces in java

java lambda and function interface


Example of function interface:

  • consumer: A Consumer is a functional interface that accepts a single input and returns no output. (biconsumer)
  • supplier: This Functional Interface is used in all contexts where there is no input but an output is expected.
  • function: represents function which take arguments and return result. unaryoperator extend function but the input and oytput is the same type. (unaryOperator work on 1 operator, binaryoperator)
  • predicate: used for conditionel check. represents a boolean value function. Can be used with filter() operator (specialized predicate, intpredicate, longpredicate..)


Streams in java

Streams are wrappers around a data source, allowing us to operate with that data source and making bulk processing convenient and fast.

A stream does not store data and, in that sense, is not a data structure. It also never modifies the underlying data source.

A terminal operation means that once the operation is performed, the stream pipeline is considered consumed and can no longer be used

Intermediate operations such as filter() return a new stream on which further processing can be done. Terminal operations, such as forEach(), mark the stream as consumed, after which point it can no longer be used further.

1 comment :

Articles les plus consultés