Saturday, 19 October 2019

java interview preparation for Experienced developers and Architect

How JVM Works – JVM Architecture?



variables scope

  • In general, a set of curly brackets { } defines a scope.
  • In Java we can usually access a variable as long as it was defined within the same set of brackets as the code we are writing or within any curly brackets inside of the curly brackets where the variable was defined.
  • Any variable defined in a class outside of any method can be used by all member methods.
  • When a method has the same local variable as a member, this keyword can be used to reference the current class variable.
  • For a variable to be read after the termination of a loop, It must be declared before the body of the loop.



Binary Search in Java


Different ways to create objects in Java


1) Using new Keyword : 
2) Using New Instance :If we know the name of the class & if it has a public default constructor we can create an object –Class.forName. We can use it to create the Object of a Class. Class.forName actually loads the Class in Java but doesn’t create any Object. To Create an Object of the Class you have to use the new Instance Method of the Class.

Class cls = Class.forName("NewInstanceExample");
            NewInstanceExample obj =
                    (NewInstanceExample) cls.newInstance();


3) Using clone() method:
Whenever clone() is called on any object, the JVM actually creates a new object and copies all content of the previous object into it. Creating an object using the clone method does not invoke any constructor.
To use clone() method on an object we need to implement Cloneable and define the clone() method in it.

4) Using deserialization : 
Whenever we serialize and then deserialize an object, JVM creates a separate object. In deserialization, JVM doesn’t use any constructor to create the object.

5)Using newInstance() method of Constructor class :There is one newInstance() method in the java.lang.reflect.Constructor class which we can use to create objects.

// Java program to illustrate creation of Object 
// using newInstance() method of Constructor class 
import java.lang.reflect.*; 

public class ReflectionExample 

private String name; 
ReflectionExample() 


public void setName(String name) 

this.name = name; 

public static void main(String[] args) 

try

Constructor<ReflectionExample> constructor 
= ReflectionExample.class.getDeclaredConstructor(); 
ReflectionExample r = constructor.newInstance(); 
r.setName("Hey"); 
System.out.println(r.name); 

catch (Exception e) 

e.printStackTrace(); 






Threads:

Java Multi threading interview questions-

https://javacmlearning.blogspot.com/2019/10/java-multi-threading-interview-questions.html

what are distributed transactions in microservices?

suppose one microservice has updated db1 and another microservice has db2 and fail and microservice3 has to use combined results of 1 and 2 . so how you have to maintain ACID property here.

the answer is either avoid distributed transection at all.
or 
use 
2phase commit protocol  

saga pattern

event based transections

suppose we have deployed our service using http protocol just wanted to convert this to https call what changes required in server?

First, make sure that you have configured and enabled both the HTTP and HTTPS <Connector> elements in your conf/server.xml file:


<Connector port="8080" protocol="HTTP/1.1" 
               redirectPort="443"/>

<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS"
               keystoreFile="conf/keystore" keystorePass="s00perSeeekrit"/>


For details on how to prepare your conf/keystore file, see http://tomcat.apache.org/tomcat-6.0-doc/ssl-howto.html.
Restart Tomcat and test both of these connectors, making sure that you can access your web application via either connector before you proceed. Next, edit your web app's WEB-INF/web.xml file and add the following inside of your <web-app> container element:

<!-- Require HTTPS for everything except /img (favicon) and /css. -->
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>HTTPSOnly</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <user-data-constraint>
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
    </security-constraint>
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>HTTPSOrHTTP</web-resource-name>
            <url-pattern>*.ico</url-pattern>
            <url-pattern>/img/*</url-pattern>
            <url-pattern>/css/*</url-pattern>
        </web-resource-collection>
        <user-data-constraint>
            <transport-guarantee>NONE</transport-guarantee>
        </user-data-constraint>
    </security-constraint>
This configuration declares that the entire web app is meant to be HTTPS only, and the container should intercept HTTP requests for it and redirect those to the equivalent https:// URL. 

No comments:

Post a Comment

Note: only a member of this blog may post a comment.