Showing posts with label java question. Show all posts
Showing posts with label java question. Show all posts

Saturday, June 20, 2020

Java Interview @ Polaris

Hi friends,

In this post, I'm sharing interview questions asked in Polaris.


Question 1:

public class Quiz23{

    public static void main(String[] args){

        int x = 0;
        int[] nums = {1,2,3,5};

        for(int i : nums){

            switch(i){

                case 1:
                    x += i;
                case 5:
                    x += i;
                default:
                    x += i;
                case 2:
                    x  += i;

            }
            System.out.println(x);
        }
    }
}

What will be the output of above code?

Answer:

27

Explanation:

For case i = 1:  All the cases will run.
For case i = 2: only case 2 will run.
For case i = 3: Default and case 2 will run.
For case i = 5: case , default and case 2 will run.



Question 2:

What is the difference between Map and FlatMap?

Answer:

Map: Transforms the elements into something else. It accepts a function to apply to each element and returns a new Stream of values returned by the passed function.

It takes function object as the parameter e.g.: Function, ToIntFunction, ToIntDoubleFunction, ToLongFunction.


FlatMap: Combination of Map and Flat operations.So, we first apply map operation on each element and then flattens the result.

So, if function used by Map is returning a single value, then map is ok. But, if function used by Map operation is returning a stream of list or stream of stream, then we need to use flatmap to get stream of values.

For example:

If we have a stream of String containing {"12", "34"} and a method getPermutations() which returns a list of permutations of given string.

When we apply getPermutation() into each string of Stream using map , we get something like [["12","21"], ["34","43"]], but if we use flatMap, we get a stream of strings e.g.: ["12","21","34","43"].

Another example:

List evens = Arrays.asList(2,4,6);
List odds = Arras.asList(3,5,7);
List primes = Arrays.asList(2,3,5,7,11);

List numbers = Stream.of(evens, odds, primes).flatMap(list -> list.stream()).collect(Collectors.toList());

System.out.println("flattened list : "+numbers);

Output: flattened list : [2,4,6,3,5,7,2,3,5,7,11]


 
Question 3:

What is the difference between passing int array and String array to Stream.of()?

Answer:

Stream.of(int[]) gives Stream<int[]>
Stream.of(String[]) gives Stream<String>

So, when using Stream.of() with int[] , we get Stream<int[]> and then for getting ints from Stream, we use flatMapToInt(i -> Arrays.Stream(i)) to get IntStream and then we can either use map() or forEach().

e.g.:

int[] arr = {1,2,3,4};
Stream<int[]> streamArr = Stream.of(arr);
IntStream intStream = streamArr.flatMapToInt(i -> Arrays.Stream(i));
intStream.forEach(System.out :: println);



Question 4:

What is a Boxed Stream?

Answer:

If we want to convert stream of objects to collection:

List<String> strings = Stream.of("how", "to", "do", "in", "java").collect(Collectors.toList());

The same process doesn't work on streams of primitives, however.

//Compilation Error:
IntStream.of(1,2,3,4,5).collect(Collectors.toList());

To convert a stream of primitives, we must first box the elements in their wrapper class and then collect them. This type of stream is called boxed stream.

Example of IntStream to List of Integers:

List<Integer> ints = IntStream.of(1,2,3,4,5).boxed().collect(Collectors.toList());

System.out.println(ints);

Output: [1,2,3,4,5]

 

Question 5:

List Spring Core and Stereotype annotations.

Answer:

Spring Core Annotations:
  • @Qualifier
  • @Autowired
  • @Configuration
  • @ComponentScan
  • @Required
  • @Bean
  • @Lazy
  • @Value

Spring framework Stereotype annotations:
  • @Component
  • @Controller
  • @Service
  • @Repository


Question 6:

What is difference  between @Controller and @RestController?

Answer:

@Controller creates a Map of model object and finds a view for that.
@RestController simply returns the object and object data is directly written into HTTP response as JSON or XML.

@RestController = @Controller + @ResponseBody
@RestController was added in  Spring 4.



Question 7:

Why Spring introduced @RestController when this job could be done by using @Controller and @ResponseBody?

Answer:

The functioning or output of @Controller and @ResponseBody is the default behavior of RESTFUL web services, that's why Spring introduced @RestController which combined the behavior of @Controller and @ResponseBody.  



Question 8:

Tell something about Swagger tools which you have used.

Answer:

Swagger Editor:  Can edit OpenAPI specifications.

Swagger UI : A collection of HTML/CSS/Javascript assets that dynamically generate beautiful documentation.

Swagger Codegen: Allows generation of API client libraries.

Swagger Parser: Library for parsing OpenAPI definitions from java.

Swagger Core: Java related libraries for creating, consuming and working with OpenAPI definitions.

Swagger Inspector: API testing tool

SwaggerHub : Built for teams working with OpenAPI.








That's all for this post.
Thanks for reading!!

Sunday, May 24, 2020

Java Interview @ Altran

Hi friends,

In this post I'm sharing Java interview questions asked in Altran.
You may also find my other interview posts very useful.



Java Interview Questions asked in Altran:

Question 1:

How does HTTPS work?


Answer:

In HTTPS, every message is encrypted or decrypted by use of public/private keys.

So, we need to trust that public key cryptography and signature works.

  • Any message encrypted with Google's public key can only be decrypted with Google's private key.
  • Anyone with access to Google's public key can verify that a message (signature) could only have been created by someone with access to Google's private key.









Question 2:

What is a Certificate Authority? How is a certificate signed?


Answer:

Suppose youtube.com web server uses HTTP currently. And now youtube wants to secure communication using HTTPS.

Also there is Google CA which is considered as a trusted certificate authority.

As any party envolved in public key cryptography , the google certificate authority has a private key and a public key.

As youtube.com wants to communicate using HTTPS, they also need to create a new pair: public key and private key.

Now youtube.com web server creates a Certificate signing request with this key pair.





 

So most of the browsers [when they are delivered] has a list of these certificates issued by known Certificate Authorities [Google, Symantec, Thwate].

So, when we try to open youtube.com, then as youtube.com uses HTTPS, it sends certificate to the browser. As browser knows the Google's public key [as it is having that certificate with Google's public key], so it can use that key to know that this certificate is actually signed by trusted certificate authority [Google].

Means. now that public key can be used to create and encrypt a secret key which will then be used in symmetric encryption.




Question 3:

Explain the difference between HTTP, HTTPS, SSL and TLS?

Answer:

HTTP: Hyper Text Transfer Protocol

Using this protocol, data is transferred in clear text.

HTTPS:  Secure Hypertext Transfer Protocol
 
Means, HTTP with a security feature.

Encrypts the data that is being retrieved by HTTP.  So, it uses encryption algorithms to encrypt the data. 

HTTPS uses SSL [Secure Socket Layer] protocol to protect the data.


SSL: This protocol is used to ensure security on the internet. Uses public key encryption to secure data.


TLS: Transport Layer Security

It is the latest industry standard cryptographic protocol. It is a successor to SSL. 
And it is based on same specifications. 
And like SSL, it also authenticates the server, client and encrypts the data.




Question 4:

What are the collection types in hibernate?


Answer:

Below is the list of collection types in hibernate:

  • Bag
  • List
  • Map
  • Array
  • Set


Question 5:

What is hibernate proxy?

Answer:

Mapping of classes can be made into a proxy instead of a table.
A proxy is returned when actually a load() is called on a session. The proxy contains actual method to load the data.

The proxy is created by default by hibernate for mapping a class to a file.



Question 6:

What is the difference between load() and get() methods in hibernate?

Answer:

Hibernate's session interface provides several overloaded load() [It will not hit database] methods for loading entities. Each load() method requires the object's primary key as an identifier and it is mandatory to provide it.

In addition to the ID, hibernate also needs to know which class or entity name to use to find the object with that ID.

In case of get() method, we will get return value as NULL if identifier is absent.
But in case of load() method, we will get a runtime exception [ObjectNotFoundException].

When we call load() on hibernate's session object, then hibernate doesn't make a call to database. It creates and returns a proxy object. Now, when some state is fetched from the proxy object, then hibernate issues the appropriate SQL statement to database and builds the real persistent object.

So, when actual data is requested using proxy object  and if no data exist for the identifier [id], then ObjectNotFoundException is thrown.


On the other hand, when we call get() method on hibernate's session object, then hibernate immediately issues a SQL statement to the database to fetch associated data  [usually a row in database] to rebuild the requested persistent object.
So, if no data is found for the requested identifier [id], the method will return null.
 



That's all for this post.
Thanks for reading!!





CAP Theorem and external configuration in microservices

 Hi friends, In this post, I will explain about CAP Theorem and setting external configurations in microservices. Question 1: What is CAP Th...