Java Interview Questions | Page 14

Question 131:

Consider a directory D:\modernpathshala\io and this directory contains 10 text files

If you execute below code snippet, what will happen?

File file = new File("D:\\modernpathshala\\io");

delete() method is used to delete the file specified by this object. If the target file to be deleted is a directory, it must be empty for deletion to succeed else the directory is not deleted and the method returns false.

Question 132:

How will you check whether a file is writable in Java?

You can check whether the application has access to write on a file using canWrite() method of File class as shown below - 

File file = new File("D:\\modernpathshala\\io\\MyProgram.txt")
boolean hasWriteAccess = file.canWrite();
System.out.println("Write Access = "+hasWriteAccess);
Question 133:

Explain the usage of File and RandomAccessFile class.


What is the difference between the File and RandomAccessFile class?

Suppose you have a file of 100MB but you want to access data after 50MB of data. This can be achieved using RandomAccessFile. This class provides seek() method through which you can move to specific position of any file. This is not possible using File class.

Question 134:

Given thread t1, t2 and t3 for any object. Write a code which ensures t1 will complete execution first and then t2 will complete execution and then t3 will complete execution.

You can achieve sequence of execution of any thrread using join() method of Thread class as shown below:

class DemoRunnable implements Runnable {

	public void run() {
		System.out.println("Thread name :" + Thread.currentThread().getName());


public class JoinExample {

	public static void main(String[] args) {
       DemoRunnable demo = new DemoRunnable();
		Thread t1 = new Thread(demo, "T1");
		Thread t2 = new Thread(demo, "T2");
		Thread t3 = new Thread(demo, "T3");


		// start second thread once first thread has completed it's execution
		try {
		} catch (InterruptedException e) {


		// start third thread once second thread has completed it's execution
		try {
		} catch (InterruptedException e) {




Thread name :T1
Thread name :T2
Thread name :T3

From above output, it is clear that first T1 completed its execution and then T2 followed by T3.

Question 135:

What will be the output of below code snippet?

public class IteratorTest {

	public static void main(String[] args) {
		List lst = new ArrayList();
		Iterator itr = lst.iterator();
		while (itr.hasNext()) {

		while (itr.hasNext()) {

The output of above code snippet will be - 


When you try to use hasNext() in the second loop it returns false immediately, so the code inside the loop won't be executed. 

The iterator interface provides three methods:

  • hasNext()
  • next()
  • remove()

So there is no way to reset the iterator.

Question 136:

What is difference between HashMap and ConcurrentHashMap?

Question 137:

What is the complexity of contains() method in List?

Let us check the contains() method code of ArrayList in Java.

public boolean contains(Object paramObject) {
		return (indexOf(paramObject) >= 0);

	public int indexOf(Object paramObject) {
		int i;
		if (paramObject == null)
			for (i = 0; i < this.size; ++i)
				if (this.elementData[i] == null)
					return i;
				else {
					for (i = 0; i < this.size; ++i)
						if (paramObject.equals(this.elementData[i]))
							return i;
		return -1;

From the above, it is clear that the run time complexity of contains method is O(n)

Question 138:

What is the complexity of contains() method in Set in Java?

Question 139:

What is the complexity of contains() method in Map in Java?

Question 140:

What is difference between Iterator and Enumerator in java?

Share interview questions


comments powered by Disqus


Social Media