Showing posts with label static. Show all posts
Showing posts with label static. Show all posts

Monday, 17 November 2014

Hong Kong and Singapore Based Firm

Telephonic for a Singapore based firm with clients in Hong Kong.

Spring:

  1. What is AOP?
  2. What is DI?
  3. What is IOC?
  4. How many types of IOC?
  5. How to inject bean using constructor?
  6. How to inject bean using setter?
  7. How many types of beans?
  8. Life cycle of beans?
  9. Example of singleton bean?
  10. Is it thread safe?
  11. When does singleton get instantiated?
  12. When does prototype get instantiated?
Java:
  1. What is dynamic polymorphism?
  2. What is interface?
  3. Why use interface when we have abstract class?
  4. How to restrict a method from being overridden?
  5. Can private methods be overridden? 
  6. Can static methods be overridden?
  7. Can non-static content be referenced from a static method?
  8. What if system.exit() is called in a try block?
  9. StringBuilder vs StringBuffer?
  10. What is volatile keyword?

Wednesday, 11 September 2013

Initialization-on-demand holder idiom

Example Java Implementation

This implementation is a well-performing and concurrent implementation valid in all versions of Java. The original implementation from Bill Pugh (see links below), based on the earlier work of Steve Quirk, has been modified to reduce the scope of LazyHolder.INSTANCE to private and to make the field final.
public class Something {
        private Something() {}
 
        private static class LazyHolder {
                private static final Something INSTANCE = new Something();
        }
 
        public static Something getInstance() {
                return LazyHolder.INSTANCE;
        }
}

How it works

The implementation relies on the well-specified initialization phase of execution within the Java Virtual Machine (JVM); see section 12.4 of Java Language Specification (JLS) for details.
When the class Something is loaded by the JVM, the class goes through initialization. Since the class does not have any static variables to initialize, the initialization completes trivially. The static class definition LazyHolder within it is not initialized until the JVM determines thatLazyHolder must be executed. The static class LazyHolder is only executed when the static method getInstance is invoked on the class Something, and the first time this happens the JVM will load and initialize the LazyHolder class. The initialization of theLazyHolder class results in static variable INSTANCE being initialized by executing the (private) constructor for the outer class Something. Since the class initialization phase is guaranteed by the JLS to be serial, i.e., non-concurrent, no further synchronization is required in the staticgetInstance method during loading and initialization. And since the initialization phase writes the static variable INSTANCE in a serial operation, all subsequent concurrent invocations of the getInstance will return the same correctly initialized INSTANCE without incurring any additional synchronization overhead.

Tuesday, 6 August 2013

Sirion Labs Interview

Sirion Labs
Interview:
Venue: Palm something, Gurgaon. Within 15 minutes ride from Sikandarpur metro station. Auto wala will charge around 50 bucks to land you straight up there.
Date : 3 August 2013
                    
Round I:
First round is Java basics. Well, not that basic. The questions keep getting twisty as you move through the paper. Some of them are plain silly. And yes, there are a couple of questions relating to data structures as well.
In total there are 16 questions.
No marking at all. They will simply discuss the solution to the answers. So be cautious about any guess that you make. In the interview they will discuss any question that you marked as wrong or any such question that turned out to be right in your case, but the majority got it wrong.
Questions of first round (in no particular order):

1.             class A
{
    public static void  X()
  {
          Y();
  }
  public static void Y()
  {
        System.out.println("parent");
  }
}

public class B extends A
{
        public void Y()
        {
                System.out.println("Child");
        }
}
What will the following code snippet print?
B.X() ?
Ans.
Explanation: From the first look it appears that either Parent or Child will be printed. But, there’s a catch. All the methods are static and static methods cannot be overridden as they are properties of class.

2.    try
{
   int a = 5;
   System.out.println("Try");
}
catch(Exception e)
{
    System.out.println("Exception is ="+a);
}
What will be printed on running the above code snippet?
Options:
A)     Try
B)      Exception
C)      Run time error
D)      Compile time error
Ans. D) Compile time error
Explanation: You may spend a lifetime trying to figure out what’s wrong with the above code and still write A with an uneasy feeling that something is definitely amiss, maybe I am just overlooking it. Your gut feeling is correct. Something is amiss. But it’s just silly. Did you notice that the variable ‘a’ is being called in exception block while its scope ends in ‘try’? See, it’s silly. The code will simply give a compile time error.

3.   A very long question. First it lists the definition of in order, pre order and post order traversal. It refers to post order as reverse order. Whoever came up with that? Then it innocently gives the pre order traversal of a tree. ABCDE. As expected, the question asks to come up with the corresponding in order and post order traversal.
Options:
A)     CBDAE, CDBAE
B)     Insufficient data, CDBAE
C)     Insufficient data, CABDE
D)     Insufficient data, insufficient data
Ans.
Explanation: This question can have two possible answers.
In option A if we consider the in order traversal ‘CBDAE’ as part of the question then CDBAE is indeed the correct post order traversal. Hence, A is correct.
Both, B and C are incorrect because we cannot find the post order traversal without knowing both the in order and pre order traversal.
D can also be correct as only pre order traversal cannot give us both the post order and in order traversal.

4.         String a = null;
    String b = "xyz";
    String c = a+b;
    System.out.println("c");
   
What will be the output on running the above code snippet?
Options:
A)     xyz
B)     nullxyz
C)     Complie time error
D)     Run time error

5.   public int method(int n)
    {
        int result = 0;
        n <<= 1;
        while (n > 0) {
            result += (n / 2) % 2;
        }
        return result;
    }
The method is called as method(13977) or some very large odd number. What will the method return in the variable result?
Ans.
Explanation: N<<=1 simply multiplies N with 2. It is equivalent of n*=2.

6.   The entire code for reversing a link list. They just removed the initialization part and the part inside the loop. You have to write that part.

7.  int result =0;
   for (int i =0; i< 10; i++)
   {
       for (int j=0; j<10; j++)
       {
              result+=i+j;
       }
     }
     What will be the value of result?
Ans.
Explanation: The catch in this question is that i+j will be calculated after result+=i is calculated so result will eventually be ∑(10*i) i.e. (10*0+10*1+10*2…….+10*9).

      There were 9 other questions. Most of them, related to inheritance.

Round II
All the questions were related to algorithms. You can choose any language you wish.

1.   There is a 2-D matrix of 0’s and 1’s.  Given a 2D array of 1's and 0's, find the size of the largest block of 0's. For example the following 2D array:
int[][] array = {
                     {1, 0, 1, 0, 0, 0, 1, 0 },
                     {1, 0, 0, 0, 0, 0, 1, 1 },
                     {1, 1, 1, 0, 0, 0, 1, 1 }
                     };
Will return "9", because there is a 3x3 square of 0's, and that is the biggest block of 0's in the 2D space.

2.   Find the largest distance between two nodes in a binary tree.
The diameter of a tree (sometimes called the width) is the number of nodes on the longest path between two leaves in the tree. The diagram below shows two trees each with diameter nine, the leaves that form the ends of a longest path are shaded (note that there is more than one path in each tree of length nine, but no path longer than nine nodes).
The diameter of a tree T is the largest of the following quantities:
* the diameter of T’s left sub tree
* the diameter of T’s right sub tree
* the longest path between leaves that goes through the root of T (this can be computed from the heights of the sub trees of T)

Interview:
1.   Implicit objects in JSP?
2.   What happens if destroy() is called on a servlet object?