Thursday, June 17, 2010

Java Interview Questions II

Here are a few more java questions I have been asked in interviews recently:
  1. Garbage Collection:
    • How does Garbage Collection work?
    • Where does garbage collection start from?
    • When do static fields get garbage collected?
    • When does a thread get garbage collected?
  2. What is a potential problem with the following code? How can it be resolved?
    public void swap(Account a, Account b){
     synchronized (a) {
      synchronized (b) {
       double d = a.getBalance();
       double e = b.getBalance();
       a.setBalance(e);
       b.setBalance(d);
      }
     }
    }
    
    Answer: A deadlock will occur if one thread calls swap(a,b) and another calls swap(b,a) at the same time. The easiest way to resolve this issue is to remove the two synchronized blocks and make the method synchronized instead.
  3. How many occurences of "NO" does the following program print?
    
    public class Main {
    
      public static void test(String s, String t) {
        if (s == t) {
          System.out.println("YES");
        }
        else {
          System.out.println("NO");
        }
        if (s.equals(t)) {
          System.out.println("YES");
        }
        else {
          System.out.println("NO");
        }
      }
    
      public static void main(String[] args) {
        String s = new String("HELLO");
        String t = new String("HELLO");
        test(s, t); // no, yes
    
        s = "HELLO";
        t = "HELLO";
        test(s, t); // yes, yes
    
        s = s + "!";
        t = t + "!";
        test(s, t); // no, yes
    
        s = "HELLO" + "!";
        t = "HELLO" + "!";
        test(s, t); // yes, yes
      }
    }
    
  4. What does the following code print?
    String s = "Welcome!";
    s.substring(3, 7);
    System.out.println(s);
    
    Answer: Welcome!
  5. What are the possible outputs of the following program? What changes can you make to print out "HelloI HelloII HelloIII HelloIV" in order?
    public static void main(String[] args) {
     Thread t1 = new Thread(new Runnable(){
      public void run() {
       System.out.println("HelloI");
       System.out.println("HelloII");
      }
     });
     Thread t2 = new Thread(new Runnable(){
      public void run() {
       System.out.println("HelloIII");
       System.out.println("HelloIV");
      }
     });
     t1.start();
     t2.start();
    }
    
    Answer: Possible outputs are:
    HelloI HelloII HelloIII HelloIV
    HelloI HelloIII HelloIV HelloII
    HelloI HelloIII HelloII HelloIV
    HelloIII HelloIV HelloI HelloII
    HelloIII HelloI HelloII HelloIV
    HelloIII HelloI HelloIV HelloII
    
    To print them in order add t1.join() after t1.start().
  6. You have a list of integers. Write an algorithm to find the maximum of the differences of each element with those ahead of it in the list.
  7. What does the following code print?
    public class A {
    
      private String s = "A1";
    
      public A(){
        dump();
        s = "A2";
        dump();
      }
    
      public void dump(){
        System.out.println(s);
      }
    
      public static void main(String[] args) {
        A a = new B();
      }
    }
    
    class B extends A {
    
      private String s = "B1";
    
      public B(){
        dump();
        s = "B2";
        dump();
      }
    
      public void dump(){
        System.out.println(s);
      }
    }
    
    Answer:
    null
    null
    B1
    B2
  8. How would you increment a counter in a threadsafe way?
  9. How does AtomicInteger work? Does it use any internal synchronisation?
  10. What does the following code print?
    int i = -100;
    int a = ~i + 1;
    a >>= 2;
    System.out.println(a);
    
    Answer: 25
  11. Write a unix command to find all files containing the string "ErrorMessage".

    Answer: find . -type f -exec grep -l "ErrorMessage" {} \;

More Interview Posts

4 comments:

  1. hi friends,
    nice questions.thanks for ur efforts i have seen a good website for java programming interview questions and answers.you can download it in pdf.
    cheers.hope you love it and all the best for your interview preparation.
    here is the link
    java programming interview questions and answers

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. hi friends if you want learn more about java you can goto this site www.madarapunaveen.blogspot.com

    ReplyDelete
  4. Good questions!!!

    I have also added around 2000 Core Java and J2EE questions in

    Java Interview Questions

    Have a look at them and reply.

    ReplyDelete