Sunday, November 30, 2014

Java Interview Questions

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.

Tuesday, November 25, 2014

Configure Maven to Deploy on Nexus

Today we will give the tip to deploy your war on Nexus.



So, in order to be able to deploy you war on a remote Repository using Maven, you need to add some configuration to your pom.xml. Here the lines to add :


<distributionManagement>
 <repository>
  <id>releases</id>
  <name>Releases</name>
  <url>http://NEXUS_ADDRESS:8081/nexus/content/repositories/releases/</url>
 </repository>
 <snapshotRepository>
  <id>snapshots</id>
  <name>Snapshots</name>
  <url>http://NEXUS_ADDRESS:8081/nexus/content/repositories/snapshots/</url>
 </snapshotRepository>
</distributionManagement>

You need to make sure that you have the right to deploy on the repository otherwise you may have an error like this :

 Access denied to http://NEXUS_ADDRESS:8081/nexus/content/repositories/snapshots/my_project/my_project/0.0.1-SNAPSHOT/my_projectm2m-0.0.1-20141125.092913-1.war. Error code 401, Unauthorized -> [Help 1]

So if you have access to the server using login/Password, you can provide that information in the .m2/setting.xml file :




<server>
    <id>all</id>
    <username>user</username>
    <password>password</password>
</server>

If you like the post, and it helps you, don't forget to share 

Tuesday, November 4, 2014

JBoss 7 : Set JAVA_OPTS in JBoss standalone.sh

I was working with a JBoss EAP 6 server (based on JBoss As7). This latest is installed on Linux server.

As the allocated memory is not enough, I wanted to add more space. To do so, you think first of modifying the JAVA_OPTS in standalone.sh. But this will not be taken in account.

The solution is in fact to change these JAVA_OPTS parameters in standalone.conf located in jboss-eap-6.2/bin/.


Don't forget to share ...  

Monday, November 3, 2014

Set Authentication WebService and Set Authentication Cookies in SoapUi

After development step, we need to test the Web Services. Using SoapUi, we can give the generated WSDL address and get all the exposed WebMethods.

I developed an application where authentication is needed. So the called method is authenticate which will generate a JSessionId (Cookie) :


@Resource 
private WebServiceContext wsContext;

 
@WebMethod
public User authenticate(@WebParam(name = "userName") String userName, @WebParam(name = "password") String password){
   
 User userToconnect = null;

 HttpServletRequest req = (HttpServletRequest) wsContext.getMessageContext().get(MessageContext.SERVLET_REQUEST);

 userToconnect = webService.authenticate(userName, password);
  
 req.getSession().setAttribute("User", userToconnect);
    
 return userToconnect;  
}

In next method, we will need to get the user. This one will no more be sent by the authenticated user, but it will be retrieved directly from the WebServiceContext.


public User getUser(WebServiceContext wsContext) throws Exception{

 User user = getUserFromSession(wsContext);
 if(user== null){
  throw new  Exception("User not Authenticated ", UamErrorCode.INVALID_PARAMETER);
 }
 return user.getEnterpriseName();
}


Calling the authenticate method using SoapUi, we will get a Set-Cookie in the header.



Then, in next call, user need to specify the given JsessionId. So the Authentication Cookies should be specified in SoapUi for next requests. This can be done using headers. Clic on add, Copy the JSeesionId generated by the autehnticate (copy all the row, paste, then remove the Set-Cookie). Then create a Header called Cookie, and Set the JSESSIONID.



Thant's all, now your method will work.

Don't forget to share with friends ...

Articles les plus consultés