Tuesday, August 11, 2020

Java Technical Architect interview @ Wipro

 Hi friends,


In this post,I am sharing interview questions asked at level of Java Technical Architect in Wipro.


Question 1:

How to join three threads in java? Suppose we have three threads T1, T2, T3.

We want that T2 should run after T1 and T3 should run after T2.


Answer:

 public class ThreadDemo{

    private static class ParallelTask implements Runnable{

        private Thread predecessor;

        @Override

        public void run(){

            if(predecessor != null){

                try{

                    predecessor.join();    

                }

                catch(InterruptedException ie){

                    ie.printStackTrace();

                }

            }

        }

        public void setPredecessor(Thread t){

            this.predecessor = t;

        }

    }


    public static void  main(String[] args){

        ParallelTask task1 = new ParallelTask();

        ParallelTask task2 = new ParallelTask();

        ParallelTask task3 = new ParallelTask();


        final Thread t1 = new Thread(task1, "Thread-1");

        final Thread t2 = new Thread(task2, "Thread-2");

        final Thread t3 = new Thread(task3, "Thread-3");

        task2.setPredecessor(t1);

        task3.setPredecessor(t2);


        //Now start all the threads.

        t1.start();

        t2.start();

        t3.start();

    }

    }



Question 2:

When a class is initialized?

Answer:

There are multiple ways in which a class is initialized.

  • An instance of class is created using either new operator or using reflection using Class.forName() method.
  • A static method of class in invoked.
  • A static member of class is accessed , which is not a constant variable.


How class is initialized in java?

1). Classes are initialized from top to bottom so field declared on top initialized before field declared in bottom.

2). Super class is initialized before subclass or derived class in java.

3). If class initialization is triggered due to access of static field , only class which has declared static field is initialized and it doesn't trigger initialization of super class or sub class even if static field is referenced by type of subclass , sub interface or by implementation class of interface.

4). interface initialization in java doesn't cause super interfaces to be initialized.

5). static fields are initialized during static initialization of class while non-static fields are initialized when instance of class is created. It means static fields are initialized before non static fields in java.

6). non static fields are initialized by constructors in java. Subclass constructor implicitly call super class constructor before doing any initialization, which guarantees that non-static or instance variables of super class is initialized before subclass.


public class Fruit{

    public static String field = "field";

    static{

        System.out.println("static block of fruit");

    }

    {

        System.out.println("Non-static block of fruit");

    }

}


public class Apple extends Fruit{

    static{

        System.out.println("static block of apple");

    }

    {

        System.out.println("Non-static block of apple");

        GreenApple a = null; // Subclass of Apple

        System.out.println("comparing = "+ (this == a));

    }


}

main() method:

Apple apple =  new Apple();

O/P:

static block of Fruit

static block of Apple

Non-static block of Fruit

Non-static block of Apple


Now, if I have access static field of Fruit class using Apple class, then 

O/P: static block of Fruit  


Question 3:

What are the ways to make an object eligible for Garbage collection?

Answer:

  • Nullifying the reference variable.
  • Re-assigning the reference variable.
  • Object created inside method



Question 4:

What are the ways for JVM to run garbage collector? 

Answer:

We have to ways:

  • using System.gc();
  • Using Runtime.getRuntime().gc()
Both ways are equivalent . And there is no guarantee that any of the above two methods will definitely run garbage collector.




Question 5:

Which component calls finalize() method?

Answer:

The finalize() method gets called by garbage collector not JVM. Although garbage collector is one of the module of JVM.



Question 6:

What is ClassLoader in java?

Answer:

ClassLoader in java is a class which is used to load class files in java.

Java code is compiled into class file by javac compiler and JVM executes java program, by executing byte codes written in class file.

ClassLoader is responsible for loading class files from file system, network or any other source. There are three default class loader used in java : Bootstrap, Extension and System or Application class loader.

Java class loaders are used to load classes at runtime.
Classloader in java works on three principles:
  • Delegation
  • Visibility
  • Uniqueness 


Delegation principle forward request of class loading to parent class loader and only loads the class , if parent is not able to find or load class.


Visibility principle allows child class loader to see all the classes loaded by parent ClassLoader, but parent class loader can not see classes loaded by child.

 

Uniqueness principle allows to load a class exactly once, which is basically achieved by delegation and ensures that child ClassLoader doesn't reload the class already loaded by parent.


Note: It is possible to write our own ClassLoader and violate the Delegation and Uniqueness principle. But, it is not beneficial.


Question 7:

How to load class explicitly in java?

Answer:

Using Class.forName(classname);

Using Class.forName(classname, boolean , classloader);


Class is loaded by calling loadClass() method of java.lang.ClassLoader class which calls findClass() method to locate bytecodes for corresponding class. If findClass() doesn’t find the class , then it throws java.lang.ClassNotFoundException and if it finds, it calls defineClass() to convert bytecodes into a .class instance which is returned to the caller.



That's all for this post.

Thanks for reading!!

No comments:

Post a Comment

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...