-
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?
- 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 callsswap(a,b)
and another callsswap(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. - 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 } }
- What does the following code print?
String s = "Welcome!"; s.substring(3, 7); System.out.println(s);
Answer: Welcome! - 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 addt1.join()
aftert1.start()
. - 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.
- 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
- How would you increment a counter in a threadsafe way?
- How does
AtomicInteger
work? Does it use any internal synchronisation? -
What does the following code print?
int i = -100; int a = ~i + 1; a >>= 2; System.out.println(a);
Answer: 25 - Write a unix command to find all files containing the string "ErrorMessage".
Answer:
find . -type f -exec grep -l "ErrorMessage" {} \;
Thursday, June 17, 2010
Java Interview Questions II
Here are a few more java questions I have been asked in interviews recently:
Subscribe to:
Post Comments (Atom)
hi friends,
ReplyDeletenice 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
This comment has been removed by the author.
ReplyDeletehi friends if you want learn more about java you can goto this site www.madarapunaveen.blogspot.com
ReplyDelete