Java Interview Questions | Page 6

Question 51:

If a thread goes to sleep does it hold the lock?

Yes when a thread goes to sleep it does not release the lock.

Question 52:

Can a thread hold multiple locks at the same time?

Yes. A thread can hold multiple locks at the same time. Once a thread acquires a lock and enters into the synchronized method / block, it may call another synchronized method and acquire a lock on another object.

Question 53:

How to make a List (ArrayList,Vector,LinkedList) read only?

A list implemenation can be made read only using Collections.unmodifiableList(list). This method returns a new list. If a user tries to perform add operation on the new list; UnSupportedOperationException is thrown.

Question 54:

What are advantages of iterating a collection using iterator?

For loop does not allow the updation in the array(add or remove operation) inside the loop whereas Iterator does. Also Iterator can be used where there is no clue what type of collections will be used because all collections have iterator.

Question 55:

Why character array is better than String for String password?

In Java Strings are immutable and if one stores password in String it will be available in memory until garbage collector clears it. As the developer does not have control over Garbage Collector and string are created in String pool, there is high chance it will remain in pool for long duration.
Moreover any one who has access to memory dump can find the password as clear text.
Since String are immutable there is no way contents of String can be changed because any change will produce new String while in char[] you can set it to blank or zero.

Question 56:

What is the use of join() method?

In Java, join() method is used to pause the current thread execution until the specified thread is dead.

Question 57:

What is the difference between wait and sleep method?

  • Wait method is called from synchronized context only while sleep can be called without synchronized block.
  • Wait is called on object instance; while sleep is called on current thread.
  • Waiting thread can be awaken by calling notify/notifyAll whereas sleeping thread can't.
  • Wait is normally done on condition while sleep is just to put your thread on sleep.(Producer-Consumer)
  • Wait release lock on object while waiting whereas sleep doesn't release lock while waiting.
Question 58:

What is difference between yield and sleep methods?

Yield method pauses the currently executing thread temporarily for giving a chance to remaining thread of same priority to execute whereas sleep causes currently executing thread to sleep for the specific nuber of m/s or n/s.

Question 59:

How ArrayList can be synchronized?

ArrayList can be synchronized using :-

Collection.synchronizedList(List list);
Question 60:

What is difference between List and Set?

  • List can contain duplicate values wheras Set can't have duplicate values.
  • Set(except LinkedHashSet) does not ensure the retrieval of data in same order it was inserted whereas List does.
Share interview questions


comments powered by Disqus


Social Media